Jump to content
  • Advertisement
Sign in to follow this  
vicer1234

sfml and Box2d coordinates

This topic is 2665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI,
I am using box2d and sfml for my 2d platformer game. But I have doubt about the way the co-ordinates of box2d and sfml are handled.
The way i am trying to draw a body is as shown :

If the body is defined using

b2BodyDef groundBodyDef;

groundBodyDef.position.Set(90.0f/RATIO, 15000.0f/RATIO); //RATIO = 30 //1meter = 30 pixels

where the position of the groundBodyDef in pixels will be ( 3.0, 500.0)


then if the b2BodyDef position is used to draw the sfml::shape::Rectangle like ( getting the position from groundBodyDef)

sf::Shape::Rectangle(groundBodyDef.position.x, groundBodyDef.position.y, groundBodyDef.position.x + 680, groundBodyDef.position.y - 10, sf::Color(127, 127, 0, 255));


does it is the right way of drawing ?? where the box2d shape is drawn correctly??


All suggestions are welcome

Share this post


Link to post
Share on other sites
Advertisement
In a way i want to know how is bsBodyDef's ( position.x, position.y) and B2PolygonShape's (.SetAsBox()) are related.

Like

b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f/30.0, 15000.0f/30.0);

and

// Define the ground box shape.
b2PolygonShape groundBox;

// The extents are the half-widths of the box.
groundBox.SetAsBox(9000.0f/30.0, 30.0f/30.0);

// Add the ground fixture to the ground body.
groundBody->CreateFixture(&groundBox, 0.0f);

Share this post


Link to post
Share on other sites

In a way i want to know how is bsBodyDef's ( position.x, position.y) and B2PolygonShape's (.SetAsBox()) are related.


SetAsBox() makes it so that the body's center is at the center of the rectangle. This could be guessed by the parameters named "half"-something, which would not make much sense otherwise, but the Box2D documentation also confirms.

I don't know SFML but I don't think you are drawing correctly. An sf::Shape seems to be drawable at any position, so to me the logical thing would be to mirror box2D structure, that is create the shape this way:
sf::Shape::Rectangle(-halfWidth, -halfHeight, halfWidth, halfHeight)
and then simply draw the shape at the body's position.

Also, I would suggest you reason in meters because they are the "real world" thing, and then convert to pixels only when drawing, instead of doing things the other way around. This does not change much, but will help you in case you need to do manual tweaks to the physics, or things like that.

Share this post


Link to post
Share on other sites
To draw a vertical wall with box2d and sfml

is this logic correct


b2BodyDef B_wall;

B_wall.position.Set(600.0, 480.0);
wall = Shape::Line(sf::Vector2f(B_wall.position.x, 40.0), sf::Vector2f(B_wall.position.x, 480.0), 2.0, sf::Color(127, 0, 0));
b2Body* B_wallBody = world->CreateBody(&B_wall);
b2PolygonShape B_wallShape;
B_wallShape.SetAsBox(1.0, 300.0);


Is sfml Line placed with respect to the box2d body ????

Share this post


Link to post
Share on other sites

Is sfml Line placed with respect to the box2d body ????


I would say it depends on how you draw that line later. You construct some shape but don't "place" (draw) it anywhere, in that code.
But then again, I don't know SFML. If no one else answers, what about reading some documentation to find out, or even just try to run some code !

Share this post


Link to post
Share on other sites
My doubt is

b2BodyDef B_wall;

B_wall.position.Set(600.0, 480.0);
B_wallShape.SetAsBox(1.0, 300.0);

is the body correctly placed with the shape????

Share this post


Link to post
Share on other sites

is the body correctly placed with the shape????


The rectangle's center will be at the body's center, if this is what you ask.

You should just try things, because you don't seem to enjoy reading docs :)
Or did you try, and get unexpected results ? In this case, you should describe all that to us (your attempts, and the problem).

Share this post


Link to post
Share on other sites

You should just try things, because you don't seem to enjoy reading docs :)
Or did you try, and get unexpected results ? In this case, you should describe all that to us (your attempts, and the problem).
[/quote]

Hi,
I tried experimenting but i am getting unexpected results.
When i try to draw a vertical wall with the above described method of forming a body and setting a box around it.
It happens to show a collision at wrong position, way before the wall is shown, as it happens to be that the
position i am defining is incorrect.

What i am trying is to move a block horizontally to check the collision against this wall.

I am aware of SetAsBox placing the rectangle at the body's center...But it gives unexpected behaviour, so thats why confused whether the logic
is correct or not.
I am attaching the image of the collision and also the source code...
The small box as shown in the picture shows where the collision is happening and the moving box stops in there. But in actual case it should stop in near the wall on the right hand side

Share this post


Link to post
Share on other sites
sf::Shape box = sf::Shape::Rectangle(bodyDef.position.x, bodyDef.position.y, bodyDef.position.x + 5, bodyDef.position.y - 10, sf::Color(127, 0, 0, 255));
...
box.SetPosition(position.x, position.y);


For x coordinates for example, you define the shape as starting at bodyDef.position.x
But then you draw it translated by another position.x.

The straightforward way, if you want to draw at (position.x, position.y), is to have matching shapes:

b2Blub.SetAsBox(foo, bar);
sfBlub = sf::Shape::Rectangle(-foo/2, -bar/2, foo/2, bar/2, ...);


By the way, I find your code hard to read (at least quickly), with all the globals that are used in different functions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!