I am using Gray Olson's bullet wrapper to incorporate physics in my Cocos3d project. I am trying to "attach" a CC3MeshNode to a btRigidBody. I am having troubles getting the rotation of the CC3MeshNode to match up with the rotation of the btRigidBody. Here is my code:
-(void) initializeWorld
CC3PODResourceNode* podRezNode = [CC3PODResourceNode nodeFromResourceFile: @"DieCube.pod"];
dieMesh = (CC3MeshNode*)[podRezNode getNodeNamed: @"Cube"];
dieMesh.isTouchEnabled = YES;
[self addChild:dieMesh];
btCollisionShape *dieShape = new btBoxShape(btVector3(1.0, 1.0, 1.0));
btDefaultMotionState *fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
btScalar mass = 1;
btVector3 fallInertia(0, 0, 0);
dieShape->calculateLocalInertia(mass, fallInertia);
btRigidBody::btRigidBodyConstructionInfo dieRigidBodyCI(mass,fallMotionState,dieShape,fallInertia);
dieRigidBodyCI.m_restitution = 0.1;
dieRigidBody = new btRigidBody(dieRigidBodyCI);
dieRigidBody->setDamping(0.3, 0.8);
dynamicsWorld->addRigidBody(dieRigidBody);
[[CCScheduler sharedScheduler] scheduleSelector:@selector(updatePh ysics:) forTarget:self interval:0 paused:NO];
reset = NO;
left = true;
steps = 0;
-(void) updatePhysics:(ccTime)dt
dynamicsWorld->stepSimulation(1/60.f, 10);
dynamicsWorld->debugDrawWorld();
btTransform trans;
dieRigidBody->getMotionState()->getWorldTransform(trans);
btVector3 dieRBPos = trans.getOrigin();
float velX = dieRigidBody->getLinearVelocity().x();
float velY = dieRigidBody->getLinearVelocity().y();
float velL = dieRigidBody->getLinearVelocity().length();
if (dieRigidBody->getLinearVelocity().length() > -0.1 && dieRigidBody->getLinearVelocity().length() < 0.1 && !reset) {
LogInfo(@"Linear X is %f, linear Y is %f, linear length is %f", velX, velY, velL);
if(left) {
dieRigidBody->applyImpulse(btVector3(-3,0,4), btVector3(dieRBPos.getX(), dieRBPos.getY(), dieRBPos.getZ()));
left = false;
reset = true;
} else {
dieRigidBody->applyImpulse(btVector3(3,0,4), btVector3(dieRBPos.getX(), dieRBPos.getY(), dieRBPos.getZ()));
left = true;
reset = true;
}
}
dieMesh.location = cc3v(trans.getOrigin().getX(), trans.getOrigin().getY(), trans.getOrigin().getZ());
btQuaternion quat = trans.getRotation();
btVector3 rotAxis = quat.getAxis();
// dieMesh.rotation
I can get the location of dieMesh to match up with dieRigidBody, but I am not sure how to update the rotation of the dieMesh to match that of the dieRigidBody. Anybody have an idea?