Getting a gun to move with a player
Hi. Im working on an FPS. and ive stumbled onto my first roadblock. i need the gun to rotate with my player, and i cant figure out how. here are some 2d images to explain:
(this is a top down view, for showing purposes. the real game will be like a normal first person shooter.)
here is my scenario. suppose my player and gun is setup like this:
http://img398.imageshack.us/img398/6479/gun1zj8.png
now, my player simply rotates, and his position stays the same.
http://img394.imageshack.us/img394/3342/gun2zl8.png
as you can see, even though the player doesnt move, the location of the gun does move. what i need is a formula for figuring out the location and rotation of the gun, depending on the location and rotation of the player. thats where im stuck. Can anyone help me out?
Thanks.
If I'm not much mistaken, it should be something like the following:
Given:
offset - the distance ahead of the player that you want the gun to remain.
angle - the player's angle, taken in this case from the y-axis.
gun_x - the gun's x coordinate.
gun_y - the gun's y coordinate.
x and y here are the axes aligned with the floor.
The formulae should be:
gun_x = offset*sin(angle)
gun_y = offset*cos(angle)
Unless I'm missing something...
Given:
offset - the distance ahead of the player that you want the gun to remain.
angle - the player's angle, taken in this case from the y-axis.
gun_x - the gun's x coordinate.
gun_y - the gun's y coordinate.
x and y here are the axes aligned with the floor.
The formulae should be:
gun_x = offset*sin(angle)
gun_y = offset*cos(angle)
Unless I'm missing something...
To generalize Thaumaturge's answer a bit, I think what you're looking for is a transformation that combines the weapon's local-space transform with the player's world-space transform. (Among other things, this will give you greater flexibility with respect to the weapon's placement - for example, it could be positioned forward, up, and to the right relative to the player).
To do this, you'll want to construct a local-space transform for the weapon that will remain constant throughout the game (for now we'll leave aside animation effects such as bobbing or re-adjusting the weapon position).
I don't know what conventions you're using for model orientation and so forth, but most likely this transform will just be a translation that positions the weapon where you want it. Remember, this is in the player's local space, so if e.g. the player is looking down the z axis and y is up, the weapon translation might be (5,10,5).
Then, every frame, you combine this transform with the player's world transform to yield the weapon's world transform. Using column vectors:
This method offers considerable flexibility, and will make it easier to e.g. add weapon animations and other effects at a later time.
To do this, you'll want to construct a local-space transform for the weapon that will remain constant throughout the game (for now we'll leave aside animation effects such as bobbing or re-adjusting the weapon position).
I don't know what conventions you're using for model orientation and so forth, but most likely this transform will just be a translation that positions the weapon where you want it. Remember, this is in the player's local space, so if e.g. the player is looking down the z axis and y is up, the weapon translation might be (5,10,5).
Then, every frame, you combine this transform with the player's world transform to yield the weapon's world transform. Using column vectors:
matrix44 weapon_world_transform = player_world_transform * weapon_local_transform;render_weapon(weapon_world_transform);
For row vectors, you'd reverse the order of multiplication.This method offers considerable flexibility, and will make it easier to e.g. add weapon animations and other effects at a later time.
I have same problem, and alot of the code samples you find online that show things like a quarternion first person shooter camera don't include samples for how to handle a standard first person shooter gun or projectile with the orientation/rotation for the first person shooter. I worked it out once a long time ago but now are stuck as I can't find the code when I had it working before so it's a stumbling block for me as well now.
Quote:Original post by Knight Chat XI think what I posted above probably applies to the problems you're describing as well, so you might give it a read.
I have same problem, and alot of the code samples you find online that show things like a quarternion first person shooter camera don't include samples for how to handle a standard first person shooter gun or projectile with the orientation/rotation for the first person shooter. I worked it out once a long time ago but now are stuck as I can't find the code when I had it working before so it's a stumbling block for me as well now.
Quote:Original post by jyk
To generalize Thaumaturge's answer a bit, I think what you're looking for is a transformation that combines the weapon's local-space transform with the player's world-space transform. (Among other things, this will give you greater flexibility with respect to the weapon's placement - for example, it could be positioned forward, up, and to the right relative to the player).
To do this, you'll want to construct a local-space transform for the weapon that will remain constant throughout the game (for now we'll leave aside animation effects such as bobbing or re-adjusting the weapon position).
I don't know what conventions you're using for model orientation and so forth, but most likely this transform will just be a translation that positions the weapon where you want it. Remember, this is in the player's local space, so if e.g. the player is looking down the z axis and y is up, the weapon translation might be (5,10,5).
Then, every frame, you combine this transform with the player's world transform to yield the weapon's world transform. Using column vectors:For row vectors, you'd reverse the order of multiplication.matrix44 weapon_world_transform = player_world_transform * weapon_local_transform;render_weapon(weapon_world_transform);
This method offers considerable flexibility, and will make it easier to e.g. add weapon animations and other effects at a later time.
woah, you lost me. what exactly do you mean by local space transform? is it a vector? and whats the players world transform?
thanks!
Quote:Original post by Grimfate126What graphics API are you using? What math library? It'll be easier to give concrete examples if we have this information available.
woah, you lost me. what exactly do you mean by local space transform? is it a vector? and whats the players world transform?
thanks!
Meanwhile, in 3D programming a transform is typically represented using a 4x4 matrix. If you're using a graphics API such as OpenGL or DirectX, then you're using 4x4 matrices (whether you know it or not).
Transforms can be combined to produce, well, combined transforms. Applying such combined transform has the effect, more or less, of applying the transforms that make it up 'in sequence'.
In our case we're interested in performing two transforms in sequence: first, we want to move the weapon to an appropriate position relative to the player in local space, and then we want to move it to the appropriate position and orientation in world space. This is done by multiplying together the matrix representing the weapon transform with the matrix representing the player transform.
If you can post what API you're using, I can probably provide a more specific example.
Quote:Original post by jykQuote:Original post by Grimfate126What graphics API are you using? What math library? It'll be easier to give concrete examples if we have this information available.
woah, you lost me. what exactly do you mean by local space transform? is it a vector? and whats the players world transform?
thanks!
Meanwhile, in 3D programming a transform is typically represented using a 4x4 matrix. If you're using a graphics API such as OpenGL or DirectX, then you're using 4x4 matrices (whether you know it or not).
Transforms can be combined to produce, well, combined transforms. Applying such combined transform has the effect, more or less, of applying the transforms that make it up 'in sequence'.
In our case we're interested in performing two transforms in sequence: first, we want to move the weapon to an appropriate position relative to the player in local space, and then we want to move it to the appropriate position and orientation in world space. This is done by multiplying together the matrix representing the weapon transform with the matrix representing the player transform.
If you can post what API you're using, I can probably provide a more specific example.
im developing for the PSP console. im using the PSPGU. It is very similar to OpenGl, so you can explain in openGl "terms". and im using libm for math.
Quote:Original post by Grimfate126Ok, here's a quick example using OpenGL function calls:
im developing for the PSP console. im using the PSPGU. It is very similar to OpenGl, so you can explain in openGl "terms". and im using libm for math.
glMatrixMode(GL_MODELVIEW);// Set up player transform and render player model:glTranslatef(player.pos.x, player.pos.y, player.pos.z);glRotatef(player.yaw, 0.f, 1.f, 0.f);RenderPlayer();// Set up weapon transform and render weapon model (note// that the weapon position is given in *local space*// relative to the player, and barring animation effects// should remain constant):glTranslate(weapon.pos.x, weapon.pos.y, weapon.pos.z);RenderWeapon();
Things I've skipped over include:1. Setting up the view transform
2. Handling pitch (elevation)
3. Pushing and popping the matrix stack where appropriate
Also, if we're just talking about a first-person point of view here, you could just set up a separate transform for the weapon and render it in the player's local space (the above method will work for first-person if the view matrix is set up correctly, but it's geared more towards a third-person view).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement