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

Author Topic: Box2d body and sfml sprite drift out further away from window origin  (Read 4838 times)

0 Members and 1 Guest are viewing this topic.

ripps

  • Newbie
  • *
  • Posts: 10
    • View Profile
Hello

I'm having an issue at the minute with a prototype I'm working on with box2d.

I create a dynamic box and a sprite, these are to act as one object with the sprite for visuals and the box for collisions etc. pretty standard stuff.

The issue is, the further away from the windows origin (top left) the box goes, the further offset the sprite is from the box. So in the top left corner the sprites texture is inside the body's box then in the bottom right corner the textures totally out of the box.

The sprites origin is set as the centre.

In my update function I set the position of the sprite using;
Sprite.setPosition(Body->getPosition().x * 30.f, Body->getPosition().y * 30.f)

Sorry for semi-pseudocode but I just rage quit my laptop and don't want it back in until I have some ideas to try out.

I've hit up Google and read box2d and sfml docs with no joy to why these 2 components would drift out like this.

Thanks for your help.

EDIT:

Code provided below.
entity creation

//body creation
b2BodyDef bodyDef;
bodyDef.position = b2Vec2(pos.x/SCALE, pos.y/SCALE);
bodyDef.type = b2_dynamicBody;
bodyDef.fixedRotation = true;
b2Body* body = world.CreateBody(&bodyDef);

b2PolygonShape shape;
shape.SetAsBox(((size.x)/2)/SCALE, ((size.y)/2)/SCALE);
b2FixtureDef fixtureDef;
fixtureDef.density = 4.f;
fixtureDef.friction = 3.f;
fixtureDef.shape = &shape;
body->CreateFixture(&fixtureDef);

//sprite creation
texture.loadFromFile(filename));
sprite.setTexture(texture);
sprite.setOrigin(_size / 2.f);
sprite.setPosition(spawnPos);

update function

b2Vec2 pos = body->GetPosition();

sprite.setPosition(SCALE * pos.x, SCALE * pos.y);
 

And some screens

Close to origin


Furthest point from origin


Collisions between bodies working fine
« Last Edit: October 05, 2014, 09:19:50 pm by ripps »

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Box2d body and sfml sprite drift out further away from window origin
« Reply #1 on: October 04, 2014, 08:28:07 am »
It's tough to say without more code, but how is it you can tell where the box is in relation to the sprite?

You chose a scale of 1 physics unit = 30 pixels, which is fine.  Are you sure you are using this 1:30 conversion everywhere?  For example when sizing the physics shape?  Or testing the position of the physics shape?

ripps

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Box2d body and sfml sprite drift out further away from window origin
« Reply #2 on: October 04, 2014, 09:14:03 am »
Hey, thanks for replying.

I'll post some code up when I get to my computer shortly now.

Yeah the 30 pixel conversion scale is consistent across all interaction between sfml and box2d.

To move the box I apply an impulse to the body then update the sprites position as stated in my op.

It's just strange how at 0,0 the sprite is perfectly inside the box but the further away the more offset it is. There's definately a direct link. I'll post some screens aswell or maybe a vine.

Thanks for baring with me.

ripps

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Box2d body and sfml sprite drift out further away from window origin
« Reply #3 on: October 05, 2014, 09:22:05 pm »
Apologies for the gap in posting.

I have edited my op with some screens and code.


didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Box2d body and sfml sprite drift out further away from window origin
« Reply #4 on: October 06, 2014, 01:28:03 am »
Are you using any views and does the current view exactly match the window size? Keep in mind that the bounding boxes could be drawn wrong instead of only the sprite.

ripps

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Box2d body and sfml sprite drift out further away from window origin
« Reply #5 on: October 06, 2014, 10:12:59 am »
Are you using any views and does the current view exactly match the window size? Keep in mind that the bounding boxes could be drawn wrong instead of only the sprite.

No views at the moment, just drawing to and viewing the window directly. I have an evolutionary style with these things lol.

The bounding boxes as in the box2d body boxes? These are drawn with box2ds debug draw and all the collisions work with them so I don't think its this. Unless i'm misunderstanding you.

I have been using 30.f as my conversion figure, I changed this to 32.f and the effect is drastically reduced but its still definately present.

Using the screens provided above, the characters feet don't quite touch the ground, maybe by a pixel or 2.

Admittedly its alot better than it was but I'd still like to eliminate it totally.

Thanks for reading.

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: Box2d body and sfml sprite drift out further away from window origin
« Reply #6 on: October 06, 2014, 07:28:34 pm »
Have you figured out which object is in the correct position and vice versa? That might help you narrow it down.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Box2d body and sfml sprite drift out further away from window origin
« Reply #7 on: October 07, 2014, 05:06:19 am »
The bounding boxes as in the box2d body boxes? These are drawn with box2ds debug draw and all the collisions work with them so I don't think its this.
Hmm, I should take a look at that since I wrote the bounding box displaying myself :) This might be your problem though. The box2d debug draw might have a conversion unit defined somewhere. If your own conversion unit does not match theirs, both will differ more and more the further you are away from the center.

Using the screens provided above, the characters feet don't quite touch the ground, maybe by a pixel or 2.
You mean the small space in between the 2 bounding boxes in your third image from your OP? That's because of how Box2D works. Page 17 of their manual. They call it polygon skin.

ripps

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Box2d body and sfml sprite drift out further away from window origin
« Reply #8 on: October 07, 2014, 07:51:14 pm »
Well I was convinced it wasn't that but now that you say the debug draw has a conversion rate I seem to remember seeing something.

I'll report back shortly.

ripps

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Box2d body and sfml sprite drift out further away from window origin
« Reply #9 on: October 07, 2014, 09:05:47 pm »
The bounding boxes as in the box2d body boxes? These are drawn with box2ds debug draw and all the collisions work with them so I don't think its this.
Hmm, I should take a look at that since I wrote the bounding box displaying myself :) This might be your problem though. The box2d debug draw might have a conversion unit defined somewhere. If your own conversion unit does not match theirs, both will differ more and more the further you are away from the center.

Using the screens provided above, the characters feet don't quite touch the ground, maybe by a pixel or 2.
You mean the small space in between the 2 bounding boxes in your third image from your OP? That's because of how Box2D works. Page 17 of their manual. They call it polygon skin.

This was it! Thanks alot  ;D

And thanks for the link about the polygon skin too.

 

anything