Box2D rotates an object, how?

How can I rotate an object in Box2D ? I tried ..

 private static final double DEGREES_TO_RADIANS = (double)(Math.PI/180); float angle = (float) (45*DEGREES_TO_RADIANS); object.body.setTransform(object.body.getPosition(), angle); 

.. but does not work.

+4
source share
5 answers

use instead a position in the world center, for example

 private static final double DEGREES_TO_RADIANS = (double)(Math.PI/180); float angle = (float) (45*DEGREES_TO_RADIANS); object.body.setTransform(object.body.getWorldCenter(), angle); 
+2
source

First, the object must be dynamic or kinematic in order to be able to rotate, in addition, use SetAngularVelocity() to achieve rotation.

+2
source

If you want to rotate the object at an angle, you use the setTransform method, for example

b2body->SetTransform( playerBody_->GetPosition(), angleInRadian );

And if you want to constantly rotate the body, use the SetAngularVelocity method, for example

b2body->SetAngularVelocity(<float32>)

Remember that a b2body object must be dynamic or kinematic so that it can be rotated.

+2
source

I think you can use force or momentum rather than using setTransform methord directly. Example:

 body->ApplyForce( b2Vec2(force,0), body->GetWorldPoint( b2Vec2(1,1) ) ); 

this code returns the body of rote.

+1
source

The idea is to rotate a corner, the simplest method that I have found myself is to use:

 float rotation = MathUtils.PI; // target rotation float c = 1; //speed of rotation float q = rotation-groundBody.getAngle(); groundBody.setAngularVelocity(c*q); 

the body will spin faster at the beginning and slower at the end, but you can use the Interpolation function to achieve the desired rotation speed.

0
source

All Articles