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

Author Topic: Box2D + SFML question  (Read 1484 times)

0 Members and 2 Guests are viewing this topic.

nyaa

  • Newbie
  • *
  • Posts: 2
    • View Profile
Box2D + SFML question
« on: December 22, 2013, 01:18:45 pm »
I'm trying to integrate Box2D into my little playground project. But I can't seem to figure out how to fix this bug:

http://i.imgur.com/iKdEa3W.png

I seem to be doing everything correctly, but why is there a gap between my static and dynamic body?

Here's my code.

No need to fix my horrible code, just point me in the right direction. ;)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Box2D + SFML question
« Reply #1 on: December 22, 2013, 01:34:46 pm »
Unless I am mistaken...

world.Step(60.0f, 8, 3);

You need to measure the elapsed time for each frame and pass it to your world. Passing 60 seconds when maybe only 1/60th of a second has passed is totally wrong.  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

nyaa

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Box2D + SFML question
« Reply #2 on: December 22, 2013, 01:40:31 pm »
Unless I am mistaken...

world.Step(60.0f, 8, 3);

You need to measure the elapsed time for each frame and pass it to your world. Passing 60 seconds when maybe only 1/60th of a second has passed is totally wrong.  ;)

Ah of course. Thanks for noticing this. I really meant to type "1.0f / 60.0f" instead of just "60.0f". (And I will introduce variable time step later on.)

Anyway, I seem to have figured out the problem. It seems that in Box2D a body's origin is the center of the said body, while in SFML the origin is top left corner. Setting the origin of SFML shape to center of the shape seems to fix the gap problem. ;D
« Last Edit: December 22, 2013, 01:58:54 pm by nyaa »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Box2D + SFML question
« Reply #3 on: December 22, 2013, 09:28:42 pm »
Yep, that's the problem.
Also SFML uses degrees, box2D uses radians.

 

anything