• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

3D Calculating 3D Offset given Length and XYZ Rotation

mimusic

Member
Hey all! I've been struggling with this problem all day, and none of my searches for an answer have yielded anything useful. I'm trying to create a system in which one 3D object attaches itself to another 3D object at a specific point, accounting for the "anchor" object's rotation around all 3 axes. To provide an example of what I'm trying to accomplish:
  • You have 2 objects; a pole and a star
  • The star should always appear at the top of the pole, regardless of the pole's rotation along any axis; its XYZ should therefor be found using the pole's XYZ coords and XYZ rotation
  • The origin of the pole is at its base; Because of this, the base of the pole remains in the same position upon rotating.
  • You know the length of the pole and the xrotation, yrotation, and zrotation of the pole.
I know how to tackle the calculation of offset coordinates when it comes to 2D math:
  • Given base coordinates (xx, yy) and length L, find the offset coordinates with given angle A:
  • dx = xx + L*dcos( A ); dy = yy + L*dsin( A );
But I can't seem to wrap my head around how to apply this approach to 3D. My closest attempts have rendered the correct offset along a single axis at a time (i.e. the attached object will be at the correct x, but incorrect y and z).

Any help, whether it be advice or links to helpful math explanations/videos, would be greatly appreciated!
 

mimusic

Member
Read up on hierarchical transformations in the first half of these lecture notes.

This is what people who know what they're doing in 3D graphics use to handle piecewise positioning in jointed shapes.
Thanks for sharing! This looks good, and simplifies the approach I was taking on the drawing end of things, but (unless I'm missing something here) it still leaves me with the issue of actually extracting the coordinates of a particular appendage for use in my code. There are cases in which I need to check the proximity of an appendage to an object, move an independent object to an appendages coordinates, etc...

edit: it seems that I might be able to make greater use of this code-wise if I learn more about matrix multiplication and manipulation. So far I've been relying on GMS 1.4's built-in 3D features, but I guess those can only take me so far...
 
if you've got a model-to-world matrix for some object or part of some object, let's call that matrix "m", then the origin of that object, (or part), should be located in the world at: x = m[12], y = m[13], z = m[14].

if you think of a matrix layed out as a 1d array in gamemaker, then you've got the elements arranged like this:
Code:
0  4  8  12
1  5  9  13
2  6  10 14
3  7  11 15
the right-most column (ignore 15), is the translation part of the matrix.
 

mimusic

Member
if you've got a model-to-world matrix for some object or part of some object, let's call that matrix "m", then the origin of that object, (or part), should be located in the world at: x = m[12], y = m[13], z = m[14].

if you think of a matrix layed out as a 1d array in gamemaker, then you've got the elements arranged like this:
Code:
0  4  8  12
1  5  9  13
2  6  10 14
3  7  11 15
the right-most column (ignore 15), is the translation part of the matrix.
Awesome! I got rid of (most of) the provided D3D functions and replaced then with GMS2's matrix versions (grabbed them by importing all the necessary D3D functions from 1.4 into 2 and throwing those compatibility scripts back into 1.4). Doing this has allowed me to see how each individual transform modifies the matrix, and I think I'm getting a much better handle on how 3D space is manipulated with matrices because of it. Combining what you've told me here with FrostyCat's link on hierarchical transforms, grabbing the necessary coordinates has turned out to be as simple as grabbing [12],[13], and [14] from matrix_world after performing the transforms on the limb I'm trying to locate! Thanks!
 

TheSnidr

Heavy metal viking dentist
GMC Elder
The nice thing about matrices and dual quaternions is that multiplying them together "adds" their transformations. This is nice for skeletal animation, where each bone only stores its own local orientation within the parent bone's orientation, and when creatying a sample, you just multiply the matrices or dual quaternions together, starting at the bottom of the hierarchy. Say you want to attach something to the star as well - then this will become very useful!
I'm personally a fan of dual quaternions, at least for skeletal animation, since they are half the size of matrices (8 values for dual quats vs 16 for matrices) and since they produce a rotation when interpolated instead of a linear transformation.
 
Top