Wednesday, October 19, 2016

xna - How do I calculate an offset from an Object based on the Objects Rotation rather than the World Space


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



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...