Jump to content
  • Advertisement
Sign in to follow this  
BiiXteR

Radians to degree conversion problems [Box2D] [SFML]

This topic is 805 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

So, I read here (2nd post written by Imbue) the following : 

 

 

 

Let me give you a hint that would have saved me some time: SFML defines rotation as degrees going clockwise, Box2D uses radians going counterclockwise.

 

However, the post is from 2009 and I can't find any other information on what the two uses.

 

But if I do need to use a conversion, am i doing it correctly? (cause, this leads to some weird behaviors on my bodies)

_sprite.SetRotation(_body->GetAngle() * RADIAN_IN_DEGREES);

Where RADIAN_IN_DEGREES = 57.3f

 

 

 

This is what the bodies do when I have conversion "enabled" : 

 

1dd884f365eba21a8f18d3c5a3cd30b2.gif

 

And without the conversion there the bodies barely rotate at all : 

 

028a0abc50a3588e6368db6dea17bf31.gif

Share this post


Link to post
Share on other sites
Advertisement

As the 2 rotations are values going in different directions, you need to take that into account when doing the conversion.

 

E.g 90 degrees anti clockwise is the same as 270 going clockwise.

 

So the conversion here would be:

_sprite.SetRotation((2 - _body->GetAngle()) * RADIAN_IN_DEGREES);

Or 

_sprite.SetRotation(360 - (_body->GetAngle() * RADIAN_IN_DEGREES));
Edited by yaustar

Share this post


Link to post
Share on other sites

 

However, the post is from 2009 and I can't find any other information on what the two uses.



How hard did you look?


http://www.sfml-dev.org/tutorials/2.0/graphics-transform.php

The rotation is the orientation of the entity in the 2D world. It is defined in degrees, in clockwise order (because the Y axis is pointing down in SFML).



http://box2d.org/manual.pdf

Box2D uses radians for angles.

 

 

For some reason I always check everywhere except for the docs. :/

 

 

 

As the 2 rotations are values going in different directions, you need to take that into account when doing the conversion.

 

E.g 90 degrees anti clockwise is the same as 270 going clockwise.

 

So the conversion here would be:

_sprite.SetRotation((2 - _body->GetAngle()) * RADIAN_IN_DEGREES);

Or 

_sprite.SetRotation(360 - (_body->GetAngle() * RADIAN_IN_DEGREES));

 

None of the conversions work, the upper one results in the sprites being rotated when they spawn, and in the collisions being off.

The other one results in the same thing as my original problem.

Share this post


Link to post
Share on other sites

I screwed up the first conversion. Should have been:

_sprite.SetRotation(((2* RADIAN) - _body->GetAngle()) * RADIAN_IN_DEGREES);

That will give the same result as:

_sprite.SetRotation(360 - (_body->GetAngle() * RADIAN_IN_DEGREES));

I have no idea where I got the 2 from.

 

What is your actual problem? The conversion I've listed should give you different results to what you've used above.

 

Edit: Oh, I see it now, the sprite looks like it is offset from the physics body (the gaps right?) Is your sprite pivot point in the middle?

Edited by yaustar

Share this post


Link to post
Share on other sites

I screwed up the first conversion. Should have been:

_sprite.SetRotation(((2* RADIAN) - _body->GetAngle()) * RADIAN_IN_DEGREES);

That will give the same result as:

_sprite.SetRotation(360 - (_body->GetAngle() * RADIAN_IN_DEGREES));

I have no idea where I got the 2 from.

 

What is your actual problem? The conversion I've listed should give you different results to what you've used above.

 

Edit: Oh, I see it now, the sprite looks like it is offset from the physics body (the gaps right?) Is your sprite pivot point in the middle?

 

The pivot points are in the top left of the sprite.

I have no idea how to fix this problem though.

Box2D handles all the physics and I gave it the middle of my sprite when i initialized the body.

Share this post


Link to post
Share on other sites

Awesome, rotation and collision is working perfectly now! Thank you

Good stuff, glad you got it working 

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!