2
\$\begingroup\$

I have a Matrix of a player & I want to create a second location a set distance away from the first location but I want the second matrix to be set a set distance based on the rotation of the first matrix e.g. A player looking in a certain direction fires a gun, I want the bullet to start it's existence out side of the players mesh but in the direction the player is facing.

I've uploaded a video of what's happening: http://www.youtube.com/watch?v=9aThUZ94Edo

Ideally I would like to just apply the players rotation Matrix to the Offset amount to get the proper offset Matrix, based in the world space co-ordinates to then be able to apply back to the object.

It's obvious at the end of the video that it's wrong when I am able to shoot the player model from his bullet :-)

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Assuming the player is facing in its Matrix's Forward direction, this will do what your asking:

Matrix secondMatrix = firstMatrix;
SecondMatrix.Translation += (secondMatrix.Forward * zOffset) + (secondMatrix.Up * yOffset);

Where zOffset represents the distance in front of the model that you want the bullet to start out from, & yOffset represents the height above gun center you want the bullet to start out from.

The 2ndMatrix takes on the rotation of the 1stMatrix but adds location offsets using the local space vectors to achieve your set distance. You could add barrel elevation rotation to the 2ndMatrix if you wanted.

\$\endgroup\$
1
2
\$\begingroup\$

I'm assuming that your matrices are in row-major form, if not, read column for row.

It's supposed to be a general answer for all orientation-matrices, rather than only for XNA, giving some background about matrices and what data they actually store.

The matrix has the following information:

  • 1st Row: The Right-Vector
  • 2nd Row: The Up-Vector
  • 3rd Row: The Forward-Vector

Now if you want to shoot a bullet in the direction the character is facing, just use the 3rd row (the Forward Vector), scale it by the start-distance from the character and add that to the character's position:

bullet_start_pos = player_start_pos + orientation[2] * start_distance;

orientation[2] is the vector in the 3rd row of the orientation-matrix

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .