# 3d rotations about an arbitrary axis

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all, I have a problem... and my knowledge of the 3d arts isn't sufficient for me to figure out where I have gone wrong, and lo... gamedev.net cometh to my aid (hopefully). Ecuse my lengthy rant, but I'll try to explain myself first... Here's the situation: I have 2 objects, say, 2 cubes. each cube has "connection points", on it (described by xyz offsets and a vector denoting connection direction). Each cube will have connection points on each face, with the direction of the connection pointing outward at right angles to the face. ther are to be connections at the verticies of the cube as well, pointing in various directions (say, at a vector of (1,1,1)) what I'm trying to do is, move and rotate cube 2 so that the 2 cubes are connected by any two arbitrary connection points,and having cube 2 rotated so that it's connection point vector is the inverse of the connection point vector on cube 1. (imagine connecting two complicated lego blocks together... translation and rotation to get the connection points to line up) http://markus.orcon.net.nz/cubes.jpg for a visualisation (soon) What I'm attemping to do is, 1) translate both cubes to the origin so that each connection point sits at 0,0,0 2) calculate the axis of rotation and the required angle of rotation required to rotate cube 2 into position based on the normalized vectors of each connection point 3) apply the positional and rotational result to cube 2 4) apply positional results to the other conenction points on cube 2 5) apply apply the results to the vectors representing the connection point directions to get them to face the right way 6) translate everything back to cube1's original worldspace offsets tada. now... its all good assuming i'm connecting cubes face to face at 90 degrees, but when i start connecting anything else, my cube2 has the incorrect rotation (but seemingly correct position), but my connection points position and face correctly. I'm stuck, and I'm not clued enough to know what I did wrong... Is there any one willing to delve any further into my issue and help with the creation of the next biggest thing since Roboforge? (ok, well, Roboforge wasnt that bug, but it was a very sexy 3d robot cunstruction/combat game in its time)

##### Share on other sites
In general, if you want to rotate an object so that a vector A lines up with a target vector B, you should take the cross product of A and B (call it C) and rotate around C by an angle of arcsin(|C|) assuming A and B are normalised.

Probably it'll be easiest to perform the rotation first, and then moving the cubes into position should be trivial.

Edit: useful reference: http://mathworld.wolfram.com/CrossProduct.html

##### Share on other sites
Quote:
 Is there any one willing to delve any further into my issue and help...
I'd be glad to help, but I don't fully understand the problem. Here are a couple of questions:

1. Aside from the connecting issue, can the boxes be oriented arbitrarily? Or are they always axis-aligned?

2. Are the connection points and vectors only what you mentioned, i.e. perpendicular and extending from the corners and the centers of the sides? Or can they change position and direction?

3. Does 'connection distance' factor in somewhere? Is the distance variable, or are the boxes always a fixed distance apart from each other?

##### Share on other sites
Quote:
 Original post by positIn general, if you want to rotate an object so that a vector A lines up with a target vector B, you should take the cross product of A and B (call it C) and rotate around C by an angle of arcsin(|C|) assuming A and B are normalised.Probably it'll be easiest to perform the rotation first, and then moving the cubes into position should be trivial.Edit: useful reference: http://mathworld.wolfram.com/CrossProduct.html

Cheers, I'll check that with what I am currently doing and see if it helps

##### Share on other sites
Quote:
 1. Aside from the connecting issue, can the boxes be oriented arbitrarily? Or are they always axis-aligned?

Well, the boxes are just the beginning (get the basics working first). To begin with they are axis aligned, but the idea is eventually to have arbitrary meshes of many different shapes, with connection points pointing in any directions. And, as one piece attaches to the base cube, other pieces will be able to attache to the connection points of the secondary pieces, etc etc

Quote:
 2. Are the connection points and vectors only what you mentioned, i.e. perpendicular and extending from the corners and the centers of the sides? Or can they change position and direction?

to begin with, thats all they are, but, it will change. Next on the list after this problem is to allow the attached components to be rotated about the connection point (treating its direction as an axis), and thus rotating the other connection points (and any other sub connected items)

Quote:
 3. Does 'connection distance' factor in somewhere? Is the distance variable, or are the boxes always a fixed distance apart from each other?

distance is a non-issue, the items being attached are created (pulled from the lego box if you were) on the fly when they are attached. i.e. there are templates of items (cubes, spheres, planks, cones, tri-mesh) and new instances of them are created and attached when selected.

How i initially begin is to translate the item to be attached so that the connection point to be used is at the origin, so i dont think its relevant.

Cheers peoples

##### Share on other sites
Quote:
 Original post by positIn general, if you want to rotate an object so that a vector A lines up with a target vector B, you should take the cross product of A and B (call it C) and rotate around C by an angle of arcsin(|C|) assuming A and B are normalised.

I just want to be sure i understand your notation correctly (it's been years since I've had to read higher maths)

arcsin(|C|) = the arcsine of the scalar length of the vector C ? or the absolute value of the length of C?

... and another question, what happens if the vectors are exactly opposite? then the axis (C) becomes a bit odd does it not?

tia

Markus

##### Share on other sites
Quote:
 arcsin(|C|) = the arcsine of the scalar length of the vector C?
Yes.
Quote:
 ...or the absolute value of the length of C?
Vector length is always positive, so no need for absolute value. With a scaler, |a| means absolute value. With a vector, |v| means length.
Quote:
 ... and another question, what happens if the vectors are exactly opposite? then the axis (C) becomes a bit odd does it not?
When the vectors are opposite, there is no unique axis of rotation. This is reflected mathematically in that C is the zero vector. In this case you can choose an angle of pi, and any axis perpendicular to the input vectors.

##### Share on other sites
Quote:
 to begin with, thats all they are, but, it will change. Next on the list after this problem is to allow the attached components to be rotated about the connection point (treating its direction as an axis), and thus rotating the other connection points (and any other sub connected items)

I'm a math neophite but if I understand correctly, this might help. This comes from 3d Math Primer by Fletcher Dunn

You want to rotate a vector about an arbitrary axis correct?
let
n = a normalized arbitrary vector
v = the vector that will be rotated about n
vll = the portion of v projected unto n and parallel to n
vT = is the portion of v perpendicular to n
w = a vector perpendicular to vll and vT or the cross product of n X vT
v' = the rotated vector or v' = v'T + vll

v'T = cos@vT + sin@w

Substitute for vT and w

vll = (v dot n ) n //projects v unto n

vT = v - vll or v - ( v dot n ) n

w = n X vT or n X ( v - vll ) which is the same as:
n X v - n X vll or n X v - 0 which equals n X v

back to:
v'T = cos@vT + sin@w
= cos@( v - ( v dot n ) n ) + sin@( n X v )

substituting for v'
v' = v'T + vll
= cos@( v - ( v dot n ) n) + sine@( n X v ) + ( v dot n ) n

Hope that helps.

dgd

##### Share on other sites
That you all for your help... after digging deeper, i have a suspicion that the vector rotation is all good, but my problem is when I apply the resulting rotation to my cube it is being rotated about its center, rather than about the origin... I'll see if I can nut it out later 2nite.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633705
• Total Posts
3013462
×