0
votes

In libGDX Box2D i have a DynamicBody "body" and StaticBody "anchor" and its joint from "pivot joint point" and body can turning around "pivot point" and working perfect (position 2).

enter image description here

Body body = Box2DUtil.addRectangle(BodyDef.BodyType.DynamicBody);  
Body anchor = Box2DUtil.addRectangle(BodyDef.BodyType.StaticBody);

RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(anchor, body, anchor.getWorldCenter());
world.createJoint(revoluteJointDef);

But i want rotate the "body" to spesific angle, when simulation is running and gravity=10 etc. the body is turning to bottom its OK. When i click a button i want turn body to "myNewAngle" position As it is shown in the picture (position 1)

float myNewAngle = 0;
body.setType(BodyDef.BodyType.StaticBody);//for not effected from gravity
body.setTransform(body.getPosition(), lastAngle * MathUtils.degreesToRadians);

My problem is my "body" is turning around its origin (position 3) i want turn it aroud "pivot point" i set "body.setTransform(anchor.getPosition()" but its not worked. I can't do it with "motor" too.

How can i turn "body" around "pivot point" dynamical?

1
You need to change the frame of reference to the fixed frame, and then do the rotation there. What is it that you want to rotate? The angle of the body, or its whole position? - pingul
@pingul i want to change the angle of the body which has "pivot point", but i want to turn body around the "pivot point" so what code should be? - MarsPeople

1 Answers

0
votes

You need to rotate it's position in relation to the pivot point. Lets say we have the points fixed, body, and these has some world space coordinate .worldCoordinate. You rotate body around fixed v degrees by (pseudo-code)

pos = body.worldCoordinate() - fixed.worldCoordinate();
rotated_pos = rotateVector(pos, v);
body.setWorldCoordinate(rotated_pos);