We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › 3D orientation/rotation 101 help
Pages: 1 2 
3D orientation/rotation 101 help (Read 3329 times)
3D orientation/rotation 101 help
Feb 8th, 2010, 7:39am
 
Hi, this is a desperate attempt to understand the logic behind the 3D rotations, I have been searching and asking question at gamedev.net and in this forum aswell, so far I manage to get something working, with None understanding of what is going on, and at the end that code is wrong and I dont know how to fix it (chek it out here http://processing.org/discourse/yabb2/num_1265029882__.html

The basic problem is that most of the tutorials I found they assume you know most of the concepts already. 90% of them say with the angle of rotation and the axis, do this and that, fine but can you explain how to obtain the angle of rotation!! that sort of stuff... missing pieces I cant work out. This may sound basic to some, and reading this may think what a #%&* I am and frankly at this point I take it.

So please if you know how this works be so kind to let me know as I have all sorts of questions to ask, if you now a book that explains this from scratch do so as well I need to learn this.

All I want to do is simple, to orientate an object to its velocity.


I am one step to go loco Cheesy

Cheers
rS

Re: 3D orientation/rotation 101 help
Reply #1 - Feb 8th, 2010, 8:42am
 
That question have been asked before, search the forums and read more on math. It will help you in the future, trust me:

http://processing.org/discourse/yabb2/num_1206925856.html
Re: 3D orientation/rotation 101 help
Reply #2 - Feb 8th, 2010, 8:45am
 
Every time I post, after some more browsing I found more pieces of the puzzle.

From this site http://www.essentialmath.com/tutorial.htm I found a ppt presentation about Quaternions, that help me understand where the axis of rotation and the angle of rotation are coming from, so correct me if I am wrong here:

So they say, if I want to rotate V1 to V2
Axis of rotation = crossProduct(v1, v2)
Angle of rotation = acos(dotProduct(v1, v2))


From there I can say V1 = velocity vector and V2 = up vector, am I right?

rS
Re: 3D orientation/rotation 101 help
Reply #3 - Feb 8th, 2010, 8:54am
 
I now V and that is what I want, understand how it works, but so far all I found is not clear for me, so I have to ask.

Maybe I need to change may search query, all I search is "3D rotation, quaternion, euler angles, orientation" but I will never think of "Calculating local coords for a 3d vector" Wink

Thanks for the link I will check it out later, looks very informative.

rS
Re: 3D orientation/rotation 101 help
Reply #4 - Feb 8th, 2010, 9:45am
 
This is an interesting question - one that I'm sure I'll want to answer for myself when as I start doing more 3D stuff.

Not wanting to frustrate you any further, but have you considered that for an object to be "pointing" in the direction of its velocity, it can also spin around on that velocity-direction-axis and still be pointing in that direction? Which of those orientations do you want?

Here's an idea:

think of the earth, and how we typically divide it up; latitude (the flat slices stacked on top of each other) and longitude (the beachball or orange segments that run through the poles).

Now, imagine a "unit sphere" (or earth with radius 1 unit). Your velocity vector can be normalised to be length 1 in the same direction. If we root this at the centre of the earth, our normalised velocity vector reaches to a point on the surface of the earth/sphere.

If we look at the Earth side-on, we can imagine a unit circle centred on X-Y axes. We know what the Y-value is (it is the Y value of our normalised vector) and we need to work out what the "latitude" (angle from the equator) is. If you draw a sketch, you can probably figure this out.

Then another step to work out what angle to rotate around the Y axis to get to the point on the latitude circle for the Z coordinate.

Well, that may or may not work, and may also involve doing some tests to check which quadrant of various circles you're looking at... and since it isn't an 'elegant' way using vector maths, probably isn't the best!

Your notion about V1 and V2 sounds reasonable (assuming your object is originally oriented "pointing" towards what you call "up"). The question is, though, how to use the quaternion (axis of rotation and angle of rotation) that you find?

rotateX(), rotate(Y), rotate(Z) are (I think) implemented using a rotation axis vector with some lower-level call - eg OpenGL's glRotatef(angle, x, y, z) - which isn't mentioned in the usual Processing methods.

-spxl
Re: 3D orientation/rotation 101 help
Reply #5 - Feb 8th, 2010, 9:55am
 
nardove wrote on Feb 8th, 2010, 8:45am:
So they say, if I want to rotate V1 to V2
Axis of rotation = crossProduct(v1, v2)
Angle of rotation = acos(dotProduct(v1, v2))

From another reference (The Dot and Cross Product)
v . w = ||v|| ||w|| cos q

so I think that means your v1 and v2 must be assumed to be unit vectors.
Re: 3D orientation/rotation 101 help
Reply #6 - Feb 8th, 2010, 10:30am
 
i was reading up on yaw, pitch and roll at the weekend, which sounds a lot like the same thing - it's how you describe how an aeroplane is oriented. getting the 'plane to be correctly oriented on screen was just a case of applying 3 matrix multiplications, one for each angle. BUT they had to be done in the right order, yaw, then pitch, then roll. (and your heading vector is just (SPEED, 0, 0) multiplied by the same matrices, assuming that x axis is forward.)

basically you spin the plane (facing along +ve X) around Y until it's pointing the right way, then rotate it up (which rotates it up against the *rotated* Z axis (Z= in / out)) then roll it in the X (which rotates it against the *twice rotated* X axis).

maths largely from here, although their axes were different (Z = up, is this an american thing?)
http://en.wikipedia.org/wiki/Tait-Bryan_rotations
Re: 3D orientation/rotation 101 help
Reply #7 - Feb 9th, 2010, 1:05am
 
Thanks a lot for the info, its becoming clear.

V said in a previous post:
Quote:
a point is only a position in space it has no direction.
if you're trying to rotate a vector or an object's orientation vector to follow another vector's orientation here goes 2 ways:

let V = V2 - V1;
U = (0, 1, 0);

1. normalize V vector
2. take the cross-product of U and V, this will give you the axis of rotation
3. take the dot-product of U and V. the arccosine of the result is the angle of rotation.
4. with this information you can already build a matrix or a quaternion and rotate your vector

just pass the axis and the angle and build a quaternion. rotate your vector/object


I got it until point 4, so how do I build a Quaternion? my guess is
Code:
Quaternion Q = new Quaternion(dotP, crossP.x, crossP.y, crossP.z); 


but how do I use this to perform the rotation?

I am using toxiclibs by the way

Cheers
rS
Re: 3D orientation/rotation 101 help
Reply #8 - Feb 9th, 2010, 2:05am
 
Trying to build my quaternion from this
Code:
Vec3D new_dir = new Vec3D(velocity);
new_dir.normalize();
Vec3D new_up = new Vec3D(0.0, 1.0, 0.0);
new_up.normalize();
Vec3D crossP = velocity.cross(new_up);
crossP.normalize();

float dotP = new_dir.dot(new_up);
float angle = new_dir.angleBetween(new_up, true);

Quaternion Q  = new Quaternion(cos(angle / 2.0), crossP.scale(sin(angle / 2.0)));


from Describing rotations with quaternions @ http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

No success

rS

Re: 3D orientation/rotation 101 help
Reply #9 - Feb 9th, 2010, 2:27am
 
not sure how that math classes work, but make sure you are using the correct metric. degrees or radians, according to what the methods ask.

anyway,

1) compute quaternion/rotation matrix
2) transform your boid with that rotation
3) render boid

are you transforming the boid at all?
Re: 3D orientation/rotation 101 help
Reply #10 - Feb 9th, 2010, 3:07am
 
Hi V,

Yes I am transforming the boids, and using the right metric notation, if I rotate by
Code:
rotate(-angle, crossP.x, crossP.y, crossP.z); 



It works fine, but there is a funny jump sort-of quick back and forward, I am not sure the cause.

I think is something to do calculating the angleBetween, when that method is set to true, it avoids getting NaN as result, and when that happens is when I can see the jump.

So I want to try a different approach.

The rotation stuff is in the Boid class at the top of the display() method

http://nardove.com/p5/misc/steering_3ex/

And here is the javadoc for the Vec3D and Quaternion
http://toxiclibs.org/docs/core/

Thanks
rS
Re: 3D orientation/rotation 101 help
Reply #11 - Feb 9th, 2010, 7:44am
 
it looks good to me. By jump you mean the rotation it makes when changing direction? using quaternions you can do a Slerp between current and next quaternion, and you can also look into finding the smallest arc between the two quaternions. that might get you started.

good luck and good job!
Re: 3D orientation/rotation 101 help
Reply #12 - Feb 9th, 2010, 8:02am
 
V Quote:
By jump you mean the rotation it makes when changing direction?

Not exactly by the jump I mean, there are moments when the Agent(cone) flip back and forward very quickly, will the SLERP fix that? I will look into it

Cheers
rS
Re: 3D orientation/rotation 101 help
Reply #13 - Feb 9th, 2010, 9:08am
 
it flips because your direction vector does so too. SLERP stands for spherical interpolation. it can help in smoothing out your motion yes
Re: 3D orientation/rotation 101 help
Reply #14 - Feb 9th, 2010, 11:16am
 
this question comes up frequently (in fact there appears to be yet another just started here: http://processing.org/discourse/yabb2/num_1265738576_.html
sometimes it's about aligning objects to their velocity, sometimes it's about building oriented cylinders, but they're all aspects of the same question:  given two points, how do you rotate around the first to orient towards the second

the problem with trying to provide THE answer is that there isn't a single answer, and it really depends on what data you're keeping track of and what aspect of the problem you're trying to solve.  often you can just cheat and apply a spherical coordinate approach - see my posts in these threads:
http://processing.org/discourse/yabb2/num_1264161510.html
http://processing.org/discourse/yabb2/num_1206925856.html

but what that doesn't account for is "roll" (to use the airplane analogy).  with only two points (well, a position and a velocity vector, if I've correctly followed your several threads) you can't nail down all three implied axes, all you can get is two. (which is enough to "point at", but not enough to determine where "up/roll" should be)

typically if you need all three axes you'll have to carry around your own pitch/roll/yaw or a frenet frame or orthonormal TNB vectors or etcetera, potentially converting back and forth to quats if you need to slerp them, and WON'T be attempting to "derive" them from velocity-position alone.  

you can try faking that third local axes ("up") by hard-coding it (perhaps as the world y-axis) sometimes.  then you can cross your "at" vector (vel-pos) with "up" to get "right" and you now have local TNB.  keep in mind that crossing only guarantees perpendicular, not WHICH perpendicular, so you typically cross back, maybe iterate, etc, yuck -- it's still only a partial solution.  so you can expect to experience problems as your velocity approaches being colinear with whatever hard-coded value you choose.

hth
Pages: 1 2