简体   繁体   中英

Box2D rotate an object, how?

How I can rotate an object in Box2D ? 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 not working.

If you want to rotate the object to an angle then you use setTransform method like

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

And if you want to rotate the body continuously then use SetAngularVelocity method like

b2body->SetAngularVelocity(<float32>)

Remember b2body object must be a dynamic or kinematic to be able to be rotated.

use the world center instead position, like this

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

首先,对象必须是动态的运动的才能旋转,此外还使用SetAngularVelocity()实现旋转。

I think you can use force or impulses,not use setTransform methord directly. example:

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

this code let body rote.

The idea is to rotate to an angle, the easiest method I found by 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 rotate quicker at the beginning and slower at the end but you can use Interpolation function to achieve desired speed of rotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM