Rotating an object around another object

Casperika
Posts: 2
Joined: Thu Jun 18, 2015 4:21 pm

Rotating an object around another object

Post by Casperika »

Hi
I'd like to make a rigidbody rotate around another rigidbody. So far i've managed to make the rigidbody rotate around its own axis, but i want it to rotate around the other object's axis/position

this is the code until now:

Code: Select all

btTransform starTransform;
        starTransform = bulletPhysics->->getRigidBody()->getWorldTransform();
        btQuaternion rot = starTransform.getRotation();
        float angleDiff = 50;
        btQuaternion diffRot = btQuaternion(btVector3(0, 1, 0), angleDiff);
        rot = diffRot * rot;
        starTransform.setRotation(rot);
        bulletPhysics->getRigidBody()->setWorldTransform(starTransform);
thanks in advance.
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: Rotating an object around another object

Post by johnsonalpha »

I wouldnt recommend setting the transform i would instead use a force to move it around the object like a planet in orbit
Casperika
Posts: 2
Joined: Thu Jun 18, 2015 4:21 pm

Re: Rotating an object around another object

Post by Casperika »

Howi've tried already, but i can't set the axis of rotation. How would one do that exactly?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Rotating an object around another object

Post by drleviathan »

So you want bodyA to rotate around bodyB. You can slam bodyA's position relative to its "parent" bodyB using a method very similar to my advice in this thread.

If you want to avoid slamming positions and couple bodyA to bodyB with forces and velocities as johnsonalpha suggested then you could use one of the various constraints that are available: btHingeConstraint or maybe btFixedConstraint.

Alternatively, assuming you know the target transform you want bodyA to achieve you could write a custom action that computes the forces that point in the right direction and applies them each simulation substep. The action would necessarily be a bit "softer" than a constraint -- like a spring.