Shefchenko
Shefchenko

Reputation: 113

Rotate kinematic bodies in box2d around some point?

Is there any way to rotate kinematic bodies around point different from body's center without using of body.setTransform() method? Because when I use setTransform() physics behavior become strange. I use andengine.

Upvotes: 0

Views: 1370

Answers (2)

Uroš Trstenjak
Uroš Trstenjak

Reputation: 903

You could use equations for x and y coordinates of a circle. If you use for example code below, every touch on scene would result in angle decrease for 0.1, and this will result in rotation of body around point(xCenter, yCenter) with radius of r.

private float p2m = PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {

    angle -= 0.1;

    Vector2 vector = Vector2Pool.obtain(xCenter/p2m + (r/p2m) * Math.cos(angle), (yCenter/p2m + (r/p2m) * Math.sin(angle));
    body.setTransform(vector, angle);
    Vector2Pool.recycle(vector);
    return false;
}

Upvotes: 0

Plastic Sturgeon
Plastic Sturgeon

Reputation: 12527

Does the point it rotates around change? If no, you could offset the body's shapes from the body center. Then when the body rotates, it will rotate around that point.

Another idea would be to use a joint. (*pointed out below)

Upvotes: 1

Related Questions