Finding Rotational Vectors and Angles
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.
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.
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.
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.
Will that update both axis of rotating (moving the gun up and down as well as side to side)?
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.
By Z vector, do you mean the strafe vector (the one running side to side directly to the right and left of the player)?
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.
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.
is this first person or third?
There was a long silence...
'I claim them all,' said the Savage at last.
first person
It's not magic, it's Ruby.
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.
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.
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| finding a training center - Heeeelp | Glauter | 1 | 2,493 |
Dec 26, 2011 12:30 PM Last Post: zenkimoto |
|
| Finding an object by name | markhula | 2 | 3,948 |
Mar 29, 2011 08:08 AM Last Post: markhula |
|
| Finding the closest point to (x1,y1) in array of [x1,y1,z1,x2... | mikey | 17 | 7,989 |
Aug 28, 2009 08:12 AM Last Post: mikey |
|
| Angles and Matrices and Whatnot | merrill541 | 5 | 2,951 |
Jun 2, 2009 04:13 AM Last Post: unknown |
|
| CoreData, Vectors, and relationships | viscount dirigible | 4 | 2,781 |
Nov 14, 2008 04:41 AM Last Post: viscount dirigible |
|

