Code: Select all
vector3df forward = (playerData->camera->getTarget()
- playerData->camera->getPosition()).normalize();
new LynxCube("assets/t351sml.jpg",
(forward * 25) + playerData->camera->getPosition(),
orientVector(forward, playerData->camera->getUpVector()));
Now, this is the constructor being called there:
Code: Select all
LynxCube::LynxCube(const path& texture, vector3df position,
vector3df rotation) {
LynxGlobal::LynxGlobalPointers* globalPointers =
LynxGlobal::getGlobalPointers();
graphic = globalPointers->smgr->addCubeSceneNode();
if (graphic) {
graphic->setMaterialTexture(0, globalPointers->driver->getTexture(texture));
graphic->setPosition(position);
graphic->setRotation(rotation);
//TODO implement destructor
btQuaternion quat;
quat.setEulerZYX(rotation.Y, rotation.X, rotation.Z);
btDefaultMotionState* motionState = new btDefaultMotionState(
btTransform(quat, btVector3(position.Z, position.Y, position.X))); //here is where I try to initialize it with a rotation
btScalar mass = 5;
int size = 5;
btCollisionShape* shape = new btBoxShape(btVector3(size, size, size));
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, motionState,
shape);
rigidBody = new btRigidBody(fallRigidBodyCI);
globalPointers->dynamicsWorld->addRigidBody(rigidBody);
LynxGlobal::getTickables()->push_back(this);
}
}
Here is how I get, on every frame, the rotation from the rigid body and apply to the graphic:
Code: Select all
void LynxCube::tick(float deltaTime) {
if (!deltaTime) {
return;
}
btTransform trans;
rigidBody->getMotionState()->getWorldTransform(trans);
matrix4 mtrans;
memcpy(mtrans.pointer(), (void*) &trans, sizeof(float) * 16);
graphic->setPosition(vector3df(mtrans[14], mtrans[13], mtrans[12]));
vector3df newRot = mtrans.getRotationDegrees();
graphic->setRotation(vector3df(newRot.Z, newRot.Y, newRot.X));
}
Any ideas? I tried several methods to apply that rotation and nothing made the placed cubes to face the camera as they should.
Edit: figured it out using opengl matrix conversions.
This went on the constructor:
Code: Select all
graphic->setPosition(position);
graphic->setRotation(rotation); //pitch yaw roll
graphic->updateAbsolutePosition();
btTransform trans;
trans.setIdentity();
trans.setFromOpenGLMatrix(graphic->getAbsoluteTransformation().pointer());
btDefaultMotionState* motionState = new btDefaultMotionState(trans);
btScalar mass = 5;
int size = 5;
btVector3 localInertia;
btCollisionShape* shape = new btBoxShape(btVector3(size, size, size));
shape->calculateLocalInertia(mass, localInertia);
btRigidBody::btRigidBodyConstructionInfo constructionInfo(mass, motionState,
shape, localInertia);
Code: Select all
matrix4 mtrans;
trans.getOpenGLMatrix(mtrans.pointer());
graphic->setPosition(mtrans.getTranslation());
graphic->setRotation(mtrans.getRotationDegrees());