Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SetOrigin on shapes  (Read 3215 times)

0 Members and 1 Guest are viewing this topic.

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
SetOrigin on shapes
« on: March 04, 2012, 06:21:27 pm »
Suppose i have rectangle shape with size 100x30 and i use SetOrigin 50 , 15, does calling SetPosition moves shape based on Origin or top left corner of rectangle shape (0,0) ?

I have some weird offsets when draw debug data with Box2D.

Thanks for your time.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SetOrigin on shapes
« Reply #1 on: March 04, 2012, 06:40:24 pm »
The position is based on the Origin property, like all other transform components. It's written in the doc ;)
Laurent Gomila - SFML developer

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
SetOrigin on shapes
« Reply #2 on: March 04, 2012, 07:18:55 pm »
For some reason i get some offset when drawing shapes:

yellow is the shape, red are debug draw.


I made smallest example for you to try:
VS2010
Box2D 2.2.1
SMFL 2.0
Code: [Select]

#include <iostream>

// SMFL
#include <SFML/Graphics.hpp>
#pragma comment(lib, "sfml-graphics-d.lib")
#pragma comment(lib, "sfml-window-d.lib")
#pragma comment(lib, "sfml-system-d.lib")

// Box2D
#include <Box2D/Box2D.h>
#pragma comment(lib, "Box2D.lib")

#define B2_PIXEL_RATIO 30.0f

class MyDebugDraw : public b2Draw
{
private:
sf::RenderWindow* wnd;
public:
MyDebugDraw(sf::RenderWindow* w) : wnd(w) {}
void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{}
void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
sf::Vertex* vert = new sf::Vertex[vertexCount];
for(int32 i = 0; i < vertexCount; ++i)
{
vert[i].Position.x = vertices[i].x * B2_PIXEL_RATIO;
vert[i].Position.y = vertices[i].y * B2_PIXEL_RATIO;
vert[i].Color.r    = 255;
vert[i].Color.g    = 0;
vert[i].Color.b    = 0;
vert[i].Color.a    = 255;
vert[i].TexCoords.x = 0.0f;
vert[i].TexCoords.y = 0.0f;
}
wnd->Draw(vert, vertexCount, sf::Lines);
delete [] vert;
}
void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{}
void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{}
void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
}
void DrawTransform(const b2Transform& xf)
{}
};

int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
sf::Vector2f winSize = window.GetDefaultView().GetSize();

sf::Vector2f shapeSize(100.0f, 30.0f);
sf::Vector2f shapePosition(winSize.x * 0.5f, winSize.y * 0.5f);

// Rect body
sf::RectangleShape rcShape;
rcShape.SetSize(shapeSize);
rcShape.SetOrigin(shapeSize * 0.5f);
rcShape.SetPosition(shapePosition);
rcShape.SetFillColor(sf::Color(255,255,0,255));

b2World  phWorld(b2Vec2(0.0f, 0.98f));
MyDebugDraw debugDraw(&window);
phWorld.SetDebugDraw(&debugDraw);
debugDraw.SetFlags(b2Draw::e_shapeBit);

b2BodyDef shapeBodyDef;
shapeBodyDef.type     = b2_dynamicBody;
shapeBodyDef.position = b2Vec2(shapePosition.x / B2_PIXEL_RATIO, shapePosition.y / B2_PIXEL_RATIO);
b2Body* shapeBody     = phWorld.CreateBody(&shapeBodyDef);

b2Vec2 vertices[4];
vertices[0].Set(0.0f, 0.0f);
vertices[1].Set(shapeSize.x / B2_PIXEL_RATIO, 0.0f);
vertices[2].Set(shapeSize.x / B2_PIXEL_RATIO, shapeSize.y / B2_PIXEL_RATIO);
vertices[3].Set(0.0f, shapeSize.y / B2_PIXEL_RATIO);
int32 count = 4;

b2PolygonShape rcPolyShape;
rcPolyShape.Set(vertices, 4);

b2FixtureDef rcShapeFixDef;
rcShapeFixDef.shape       = &rcPolyShape;
rcShapeFixDef.density     = 20.0f;
rcShapeFixDef.friction    = 0.4f;
rcShapeFixDef.restitution = 0.1f;
shapeBody->CreateFixture(&rcShapeFixDef);

// Surround screen with static edges
b2FixtureDef groundBoxDef;
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body* groundBody = phWorld.CreateBody(&groundBodyDef);

b2Vec2 vs[5];
vs[0].Set(winSize.x / B2_PIXEL_RATIO, 0.0f);
vs[1].Set(winSize.x / B2_PIXEL_RATIO, winSize.y / B2_PIXEL_RATIO);
vs[2].Set(0.0f, winSize.y / B2_PIXEL_RATIO);
vs[3].Set(0.0f, 0.0f);
vs[4].Set(winSize.x / B2_PIXEL_RATIO, 0.0f);
b2ChainShape wallShape;
wallShape.CreateChain(vs, 5);
groundBoxDef.shape = &wallShape;
groundBody->CreateFixture(&groundBoxDef);

static sf::Clock clock;
while(window.IsOpen())
{
sf::Event event;
while (window.PollEvent(event))
{
if (event.Type == sf::Event::Closed)
window.Close();
}

float timeStep = (float)clock.GetElapsedTime().AsMilliseconds() / 1000.0f;
phWorld.Step(timeStep, 10, 10);

float x = shapeBody->GetPosition().x * B2_PIXEL_RATIO;
float y = shapeBody->GetPosition().y * B2_PIXEL_RATIO;
rcShape.SetPosition(x, y);

window.Clear();
window.Draw(rcShape);
phWorld.DrawDebugData();
window.Display();
}
return EXIT_SUCCESS;
}



I can't see whats wrong.

Thanks for your time.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SetOrigin on shapes
« Reply #3 on: March 04, 2012, 08:53:45 pm »
Quote
Code: [Select]
shapeBodyDef.position = b2Vec2(shapePosition.x / B2_PIXEL_RATIO, shapePosition.y / B2_PIXEL_RATIO);

How does Box2D interprets this position? Is it the top-left, bottom-left or center position?

What you get would be consistent with a top-left position.
Laurent Gomila - SFML developer

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
SetOrigin on shapes
« Reply #4 on: March 04, 2012, 09:18:33 pm »
I guess it is top-left as you said. I thought it would be same as center of mass (witch is by default centre of shape).  :oops:
Even better, no need for SetOrigin then.