# Calculate the coordinates of an offset in 3D space knowing the initial rotation and final rotation of the object

Working on a game mod in FiveM in which I am representing bullet impacts with drawn lines in the game world along their flight path. I am getting a normalized vector and then calculating the offset from the entity, and this works well for rigid-body entities like cars but for peds with skeletons not so much. I am trying to shift and attach the impact to the damaged entity bone, but am struggling to calculate the offset given the initial rotation/position, and then the current rotation/position. I looked at quaternions, and angle of rotation, but am not sure how to translate that into a function to calculate the final coord using the originally calculated offset.

Anyone able to point me in the direction of the math I need to solve this? Essentially I am just looking for the final 3D coords, and know I need some sort of advanced math (trig, quats, etc) to find it but am struggling to understand which solution I need.

I know I basically have the following:

initial rotation of entity bone, initial position of entity bone, initial offset from entity bone, initial coordinates of offset

and

final rotation of entity bone, final position of entity bone, and the offset I need to find the coords

and need to calculate

final coords of the offset

• How do "initial offset from entity bone" and "initial coordinates of offset" differ from one another? What coordinate space are they measured in? May 20, 2022 at 0:49

The gist of it is that you need a matrix transformation for the bone. Well, two:

• The one at the moment you want at the time of impact, let us call it A.
• And the current one, let us call it B.

Now, let us say you have a point p_a in a reference coordinates system (whatever space you have the point defined it) at the time of impact. We convert it to the space of the bone (also at time of impact):

p = inverse(A) * p_a


Then we convert that back to the reference coordinate system, but using the other matrix, so we have the current position in the reference coordinate system:

p_b = B * p


It appears from what you say that each transformation is described as a combination of translations and rotations. So you could create the transformation matrix by making a translation matrix and a rotation matrix and multiplying them together.

I don't know how exactly it is setup in what you are working with (I do not understand what you say about offset), but I expect it to be something similar to a scene tree or scene graph.

If that is the case, each object has a transformation relative to its parent. For example, the bone has a transformation relative to its parent bone, and so on, until the parent is a game object or similar, which has a transformation relative to the root of the scene, which we call global or world coordinates.

Then, if you know the path from your reference coordinate system to the bone, you can make translation matrices and rotation matrices for the transformations, multiply them together in order, and that should give you the matrix you need.

Do not lose track of what is positioned relative to what. I also remind you that matrix multiplication is not commutative. That means that the order matters. Fortunately it is either one order or the reverse order, so if it is not working, try flipping the multiplication order.

• Sorry, I am still learning game dev, in FiveM you are able to get offsets from entities which are basically the local coords for the entity, and then can use that to calculate world coordinates. So say the bullet impacts at the very front of the vehicle, my offset would be say vector3(0.0, 1.0, 0.0) or 1 unit forward of the entity's position. Now that I explain it I realize I actually can't get that offset from the entity bone, and instead only have it for the ped. So basically i'm trying to rotate a coord around another coord knowing the previous and current rotation. May 20, 2022 at 4:00
• @user2828652 If you have all the information about the bone relative to the "ped", you should be able to create a matrix that describes the bone space relative to the "ped". Then, if you have the the position you want also relative to the "ped", you should be able to convert that position to a position in bone space. And to find out where it is when the bone moved, you do the operation backwards but swapping the old matrix with the new one. May 20, 2022 at 4:06
• I'm trying to find the end position of the 'flight path' of the bullet relative to the bone specifically, but it sounds like I need to create a transformation matrix then and use that to find the coordinates new position relative to the entity bone? i.imgur.com/teKFmfH.jpeg for context, basically I am using the coords of the impact and the coords of a normalized vector to recreate the flight path of the bullet, for rigid objects this is easy, but still doesn't take into account things like doors opening or closing on the vehicle. May 20, 2022 at 4:13
• @user2828652 I'm guessing the offset are vectors in space. How is the rotation described? I believe FiveM is this: fivem.net correct? I tried looking up the documentation, but I can't find something about bones. Edit: I found docs.fivem.net/natives/?_0x17C07FC640E86B4E May 20, 2022 at 4:13
• The function I am using is GetOffsetFromEntityGivenWorldCoords() found here, it basically just returns the local coords when given a set of world coords, it gives it in a vector format, but I am not sure if it is actually a vector (like I said I am new to all this and am still trying to understand all the math behind stuff). docs.fivem.net/natives/?_0xCE6294A232D03786 is one of the functions related to entity bones (which are the bones set up in the 3D model). So peds have hand, spine, foot bones etc, vehices often have door/seat bones. May 20, 2022 at 4:19