PDA

View Full Version : Rotating weapon on camera movement

PascalM123
10-08-2010, 02:23 AM
Dear forum,

I have a problem with rotating a Mesh (generally a weapon) with a first person camera. I have a Camera class with the following properties:

class FirstPersonCamera {
public:
// Orientation of the camera
Vector3 pos;
Vector3 forward;
Vector3 right;
Vector3 up;

/* other properties */

// Constructor
FirstPersonCamera() {}

// Destructor
~FirstPersonCamera() {}

// Translation functions.
void walk_forward();
void walk_backward();
void strafe_left();
void strafe_right();

// Rotation functions.
void rotate(int dx, int dy);

// Add a weapon to the arsenal.
void add_weapon(Actor m) { weapons.push_back(m); }

protected:
// An array of weapons.
std::vector<Actor> weapons;
};
The weapon arsenal attached to the camera is an Actor. An actor consists of 2 parts, a mesh (which has a set of faces and each face has a normal, vertices and opt. texcoords) and a collider. The collider has a triple (x,y,z) which is the center of the actor and every vertex of the mesh is a triple (x,y,z) which is a point relative to the center of the actor. So if the center of the Actor is at point (0,10,0) and a vertex in the mesh is (1,1,1), the vertex in 3d space would b at point (1,11,1).

For the rotation I use quaternions and the rotation for the camera only is implemented like this:

forward.normalize();
right = forward.cross_product(up);
Quaternion qx(PI * sensitivity * dx, up);
qx.rotate(forward);
Quaternion qy(PI * sensitivity * dy, right);
qy.rotate(forward);
This is just a method to rotate the camera based on a difference in x and y in 2d pixels.
Whenever I translate the camera (walk forward/backward or strafe left/right) I add or subtract either the forward-vector or the right-vector, based on the direction.

I'm sorry for the long introduction, but I did this to give a solid idea about my implementation. The above code works fine for the camera itself, but a FPS would not be worth a bit without a weapon. This means that I want to attach a weapon to the camera class and whenever I translate or rotate, the weapon does the same.

The implementation I have so far tries to translate and rotate every attached weapon whenever the camera translates or rotates. For translation, it's not that hard to implement, I just translate the center of each weapon just like I translate the position of the camera (and this works).

The problem is within the rotation. In order to keep the mesh as a whole at the same place when rotating, the center of the mesh has to be rotated just like the forward-vector in the above code sample. To do this, The center of the mesh needs to be subtracted with the position of the camera. Then you rotate the center of the mesh with the same quaternions as the forward-vector ans after that you add the center of the mesh with the position of the camera. This implementation works as well, but the problem is that even though the center of the actor rotates properly, the orientation of the whole mesh does not change, i.e. the the weapon still points in the same direction.

I have tried several things, like adding the center of the mesh to each vertex, subtract the position of the camera, rotate with the same quaternions and after that add the position of the camera and then subtract with the rotated center of the mesh. I really did think that that would work, but it didn't.

I'm sorry for the long long talk, but does anybody know a solution to this problem or another implementation?

Regards,
PascalM123