Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 :-)

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
This set me in the right direction. I wrote a tutorial to help anyone else who might stumble in to this issue: dsebj.evolvingsoftware.com/?p=203 –  Sebastian Gray Jul 20 '11 at 7:06
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.