## Finding Rotational Vectors and Angles

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I'm trying to guide my gun mesh to follow my crosshair as I look around. I can't seem to find a good way (or any way really) to accomplish this.

Basically I have three vectors for the player: strafe (points directly to player's right), position, and direction (points towards crosshair). My camera (which is contained by the player), has two functions for getting camera rotation: GetAngleVertical() and GetAngleHorizontal() (both return float values). How can I find (if I can find) a single rotational vector and angle so as to move the gun exactly where it needs to point from what I have here using one call to glRotatef()? (note that the getanglehorizontal and vertical return scaled down versions of actually mouse change (mousedelta/1000) so I'm not sure how that might affect the gun's rotation)

Thanks for all help as usual.
tigakub
Unregistered

Post: #2
Take the cross product of the camera's Z vector, and the vector from the target point to the camera.

This will give you the axis of rotation for the glRotate.

Then use the cosine rule to calculate the angle from the dot product of the same two vectors.
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
Will that update both axis of rotating (moving the gun up and down as well as side to side)?
tigakub
Unregistered

Post: #4
It should. But it's assumed that the gun starts out pointing down the z axis each time. If you're maintaining relative coordinates, that shouldn't be a problem.
Sage
Posts: 1,066
Joined: 2004.07
Post: #5
By Z vector, do you mean the strafe vector (the one running side to side directly to the right and left of the player)?
Member
Posts: 184
Joined: 2004.07
Post: #6
Another way to think of this: suppose you want the gun to always be in the same place with respect to screen space (this is essentially what you want, I assume.) You have some matrix M (in OpenGL, the modelview matrix) that you transform each of your objects with in order to put them in screen space. Now, in screen space, you are looking down the negative z axis (again in OpenGL), so if you want your gun always pointing down this axis in screen space, you can use the INVERSE of the modelview matrix to transform the gun into world space (and then when you transform the gun again it will point down the z axis again.)

Note that you can skip a step here- again, let's suppose your gun model points down the negative z axis (and is oriented so it is aligned with the positive y axis.) Now, you have again a matrix that you transform all your objects with to put them in screen space. After you draw all your other objects, load the IDENTITY MATRIX, and then render your gun mesh. The gun mesh will then always appear in the same place in screen-space. This is probably the easiest solution- you don't move the gun at all; instead, you are always rendering it in the same place with respect to screen space, which is always looking at your crosshairs.
Member
Posts: 153
Joined: 2004.12
Post: #7
is this first person or third?

There was a long silence...
'I claim them all,' said the Savage at last.
Member
Posts: 567
Joined: 2004.07
Post: #8
first person

It's not magic, it's Ruby.
Sage
Posts: 1,066
Joined: 2004.07
Post: #9
Currently first person but we (as in the two people involved in this game) are considering using a third person view. We're not sure which one we'd like better because we haven't well set out the design of the game. We're considering having vehicles and that makes us think third person. We might go with a very quick paced (but large scale) shooter with environments similar to GTA. Fairly large urban areas. Seemed like a fun idea. Then again my partner and others who help design have suggested a third person action/rpg as well so I'm just trying to learn things I may use some day.

I thought about the suggested ways of drawing the gun but I'd like to actually rotate it so that when the player chooses to drop the gun, it's already pointing the correct way. Plus the matrix idea just sounded too complex for me at this time. Thanks for the ideas.
tigakub
Unregistered

Post: #10
Nick Wrote:By Z vector, do you mean the strafe vector (the one running side to side directly to the right and left of the player)?

No, it's the vector pointing directly into the screen. But you could get the complement of the angle (90 - angle) by using the strafe vector (i.e. the x-axis vector). But since the gun should be pointing in the general direction of the z-axis anyway, it just makes more sense to use the z-axis vector.

About rotating the gun -- it's probably more efficient to calculate the orientation of the gun on the rare occassion it's dropped, than to calculate its position relative to the player every frame.

Even with a 3rd person view, it's much more efficient to render the gun within the same transformation hierarchy as the player, than to maintain separate transformation hierarchies for player and gun.