I am creating a 3D space shooter game using OpenGL and Bullet Physics. I have stumbled upon a problem that might be easy for some but is a real challenge for me. So any help is deeply appreciated.
I have a callback function that is passing in the mouse position on the screen as a vector2. I want to take the mouse input and convert it to rotations for the player object (a spaceship). It should rotate to the right when I move the mouse to the right, to the left when I move to the left, up when I move down and down when I move up. All the moves should be relative to the current rotation of the object so if, for example, it is rotated 90 degrees to the right (roll) then when I move the mouse down , the front part of the spaceship should move towards the screen.
I tried to use quaternion rotations to avoid using torques for simplicity and as a first approach with the following code :
Code: Select all
void SpaceShip::onHover(const glm::vec2& pos)
{
static float yaw = 0.f;
static float roll = 0.f;
static float pitch = 0.f;
static float xOffset = 0.f;
static float yOffset = 0.f;
static float move = 0.005f;
static float restore = 0.001f;
xOffset = (prevPos.x == pos.x) ? 0.f : (prevPos.x > pos.x) ? -move : move;
yOffset = (prevPos.y == pos.y) ? 0.f : (prevPos.y > pos.y) ? move : -move;
roll += (xOffset * sensitivity);
pitch += (yOffset * sensitivity);
this->PhysicsSetRotation(yaw, pitch, roll);
prevPos = pos;
}
Code: Select all
btTransform transform = body->getCenterOfMassTransform();
btQuaternion rotation;
rotation.setEuler(glm::radians(yaw), glm::radians(pitch), glm::radians(roll));
transform.setRotation(rotation);
body->setCenterOfMassTransform(transform);
So I think I should change the yaw also depending on the roll but I don't know what value should I put there.
Another thing that I would like to ask is for some suggestions to do the rotation with torque and how to calculate this. If my description is not clear I can add some video or GIF to show what is happening.
Thanks in advance.