# 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.

## 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);


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

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

##### 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

##### Share on other sites

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 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

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 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 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 on other sites

So that explains why it looks wrong as the rotation is done around the corner of the sprite. There's a function in SFML to set the origin of the sprite called setOrigin. You just need to make it match the physics which for a box, I be guess it will be in the center.

Edited by yaustar

##### Share on other sites

So that explains why it looks wrong as the rotation is done around the corner of the sprite. There's a function in SFML to set the origin of the sprite called setOrigin. You just need to make it match the physics which for a box, I be guess it will be in the center.

Awesome, rotation and collision is working perfectly now!

Thank you :)

##### Share on other sites

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

Good stuff, glad you got it working

1. 1
2. 2
Rutin
20
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633761
• Total Posts
3013725
×