set rigidbody transform according to scenenode

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
hahajiang
Posts: 7
Joined: Tue May 12, 2015 2:49 am

set rigidbody transform according to scenenode

Post by hahajiang »

I'm using bullet to deal with physics, now I got a scenenode's position and rotation, here is what I do to transform the corresponding rigidbody

Code: Select all

 
btQuaternion q;
EulerXYZToQuaternion(node->getRotation(), q);   //I got this function in other's Bullet wrapper
q.normalize();
btTransform transform;
transform.setIdentity();
core::vector3df pos = node->getPosition();
transform.setOrigin(btVector3(pos.X, pos.Y, pos.Z));
transform.setRotation(q);     //I'm wondering about this, did I set it right?
rigidbody->setWorldTransform(transform);
 

Code: Select all

 
SIMD_FORCE_INLINE void EulerXYZToQuaternion(const irr::core::vector3df& euler, btQuaternion &quat)
{
  btScalar _heading=euler.Z*0.5;
  btScalar _attitude=euler.Y*0.5;
  btScalar _bank=euler.X*0.5;
  btScalar c1 = cos(_heading);
  btScalar s1 = sin(_heading);
  btScalar c2 = cos(_attitude);
  btScalar s2 = sin(_attitude);
  btScalar c3 = cos(_bank);
  btScalar s3 = sin(_bank);
  double c1c2 = c1*c2;
  double s1s2 = s1*s2;
  //w
  quat.setW((btScalar) (c1c2*c3 + s1s2*s3));
  //x
  quat.setX((btScalar) (c1c2*s3 - s1s2*c3));
  //y
  quat.setY((btScalar) (c1*s2*c3 + s1*c2*s3));
  //z
  quat.setZ((btScalar) (s1*c2*c3 - c1*s2*s3));
}
 
It looks like the rotation of the rigidbody's transform is not that right, could someone show me the right way to this?
hahajiang
Posts: 7
Joined: Tue May 12, 2015 2:49 am

Re: set rigidbody transform according to scenenode

Post by hahajiang »

Finally I got this solved :D Paste here if someone need it.
The problem I met at first:
I'm using followSplineAnimator to make my car node follow the defined waypoints, and this works well. But when I add bullet into my project, I just can't change the rigidbody's(which is bind to the car node) rotation correctly.

The solution:

Code: Select all

 
btQuaternion q;
q.setEuler(deltadeg, 0, 0)   //here, the stealer method use radian, and the deltadeg is the radian between forward vector and the direction vector you want to turn to, ex. forward = vector3df(sin(rotation.y), 0, cos(rotation.y)), and directionVec = nextPos - currentPos, then you got the deltadeg
btTransform transform;
transform.setIdentity();
transform.setOrigin(btVector3(nextPos.X, nextPos.Y, nextPos.Z));
transform.setRotation(q);     
rigidbody->setWorldTransform(transform);    // now the rigidbody's transform can sync with the node's
 
if you are using a car with separated wheels ,then you need to update your wheels' transform.
Post Reply