Before integrating the Bullet Physics, the camera worked fine. But instead of using my own simple struct for transform data, I use rigid body structs provided by the physics library now.
There are two issue with the first person camera at the moment. First, it is stuck in the ground and when I try to move it, it slips back to it's position. Second, When I get the camera out of the ground, which sometimes happens, The applied rotation isn't in the way I move the mouse. When I move the mouse straight, the camera is moved in circles.
This is how the rigid body it set up.
float mass = 1.0f;
btCollisionShape* shape = new btBoxShape(btVector3(0.5f, 0.5f, 0.5f));
btDefaultMotionState* state = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, mass), btVector3(0, 0, 0)));
btVector3 inertia;
shape->calculateLocalInertia(mass, inertia);
Body = new btRigidBody(mass, state, shape, inertia);
This is how I get and set rotations and positions of rigid bodies.
vec3 Position()
{
btVector3 pos = Body->getWorldTransform().getOrigin();
return vec3(pos.getX(), pos.getY(), pos.getZ());
}
void Position(vec3 Position)
{
btTransform transform = Body->getCenterOfMassTransform();
transform.setOrigin(btVector3(Position.x, Position.y, Position.z));
Body->setCenterOfMassTransform(transform);
}
vec3 Rotation()
{
vec3 rot;
Body->getWorldTransform().getBasis().getEulerZYX(rot.z, rot.y, rot.x);
return rot;
}
void Rotation(vec3 Rotation)
{
btTransform transform = Shape->getCenterOfMassTransform();
transform.setRotation(btQuaternion(Rotation.y, Rotation.x, Rotation.z));
Shape->setCenterOfMassTransform(transform);
}
And this is how I calculate the view matrix of the camera.
const float pi = glm::pi<float>();
if (Rotation().x < -pi) Rotation(vec3(Rotation().x + pi*2, Rotation().y, Rotation().z));
else if (Rotation().x > pi) Rotation(vec3(Rotation().x - pi*2, Rotation().y, Rotation().z));
const float margin = 0.2f;
if (Rotation().y < -pi/2+margin) Rotation(vec3(Rotation().x, -pi/2+margin, Rotation().z));
else if (Rotation().y > pi/2-margin) Rotation(vec3(Rotation().x, pi/2-margin, Rotation().z));
vec3 lookat(
sinf(Rotation().x) * cosf(Rotation().y),
sinf(Rotation().y),
cosf(Rotation().x) * cosf(Rotation().y)
);
cam->View = lookAt(Position(), Position() + lookat, vec3(0, 1, 0));
How can I move and rotate a rigid body to use it as camera?