making a body rotate to a specified angle

General Box2D issues or C++ specific issues
aakinade
Posts: 1
Joined: Sun Mar 19, 2017 6:16 pm

making a body rotate to a specified angle

Postby aakinade » Sun Mar 19, 2017 6:53 pm

So for my game I am trying to make a catapult. to give the effect of a catapult the body has to quickly rotate. what I'm trying to do is make the body rotate 45 degrees and let gravity do it's thing by putting it down on the ground again.

i tried using setangular velocity but that doesnt seem to do the trick. does anyone have suggestion?

CrowbarBarian
Posts: 7
Joined: Sun Jun 25, 2017 9:05 pm

Re: making a body rotate to a specified angle

Postby CrowbarBarian » Tue Jun 27, 2017 3:46 pm

Well, if it's not too late, my suggestion would be to use a b2RevoluteJoint and accompanying b2RevoluteJointDef to set up a motor joint. Enable joint limits and set them to your desired angles of minimum and maximum revolution. Then in your game loop whenever you want the catapult to fire, enable the motor using joint->EnableMotor(true) and set the joint rotation velocity using joint->SetMotorSpeed() to something appropriate. Then use joint->GetJointAngle() to check if the angle has reached where you want it to, and if it has, just do joint->EnableMotor(false). This should allow the catapult to return under gravity power to its starting position.

I tried a little setup in the testbed and it seemed to work OK. Just remember to set the threshold angle you check for to be a little less than the max constraint angle to allow for rounding errors and to have the catapult actually relax after firing.


Return to “Bugs, Requests, and Feedback”



Who is online

Users browsing this forum: No registered users and 5 guests