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

Author Topic: Box2D and SFML integration problems  (Read 1742 times)

0 Members and 3 Guests are viewing this topic.

deathAwaits

  • Newbie
  • *
  • Posts: 3
    • View Profile
Box2D and SFML integration problems
« on: April 26, 2016, 09:06:38 pm »
Hello.

Today I've downloaded Box2D and wanted to test it out a little bit, just to see how it works with SFML.
I wrote a really, really simple code just to see if everything works properly, but I'm getting strange results.
My box, that should be dynamic, is dynamic indeeed, but it behaves strangely.
When I add a solid ground to my world, the dynamic box flies upwards (instead downwards) to something about y=-275, which I dont understand.

When I remove the solid ground, the simulation is quite all right, the box falls just as it should fall.
Anyway, here's the code:

(click to show/hide)

Does anyone know a solution? Because I bet I did something wrong.

Thank you.
« Last Edit: April 27, 2016, 05:29:20 pm by deathAwaits »

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Box2D and SFML integration problems
« Reply #1 on: April 26, 2016, 09:21:32 pm »
Hi,

That's because The Box2D Y coordinate is pointing upward and the Y coordinate of SFML is pointing downward. So, you have to invert the position given by Box2D to apply it to your SFML drawables (same thing with the angle, you have to invert it and convert it from radians (Box2D) to degrees (SFML) ).

deathAwaits

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Box2D and SFML integration problems
« Reply #2 on: April 26, 2016, 11:36:28 pm »
#EDIT

Still can't get it to work properly, tho.

I don't get it, when I set a 0.0f y gravity, my dynamic object still moves.
Another weird thing... If I remove the static body (ground) the object behaves normally.
It falls as it should, but for an infinite amout of time, which is understandable with no ground being there.

Changing the gravity y from let's say 1.0f to -1.0f doesn't change anything at all.
First 25 y positions of my moving object are:
http://screenshot.sh/mMdFPYd7mpWwp (gravity is -1.0f)
http://screenshot.sh/mGx7UA0DJj7Fv (gravity is 1.0f)

But I had put the ground on -10.0f (so I guess thats 10 meters = 640 pixes in my SFML world).
I included the inversion of y axis betweend box2D and SFML coordinate system,
but the falling never stops.


If someone could change the code to make it work, I'd be really happy.
Or even some pointers at what I'm doing wrong.

Code here:

(click to show/hide)
« Last Edit: April 27, 2016, 10:39:06 pm by deathAwaits »

deathAwaits

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Box2D and SFML integration problems
« Reply #3 on: May 04, 2016, 11:21:10 pm »
I still didn't figure it out, I didn't have much time anyway.
Is there anyone willing to help me a little?