Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (593) Games in Android Showcase (168) games submitted by our members Games in WIP (646) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Math Question, I suck at math  (Read 1768 times) 0 Members and 1 Guest are viewing this topic.
NexusOne

Junior Devvie

Java games rock!

 « Posted 2003-11-29 21:46:20 »

Ok, I have a vector of length 1, and what I want to do is calculate the rotation matrix that would move the point (0, 1, 0) to the end of the vector. How do I do this?

Here's what I've been trying, it may be wrong:

double xAngle = 0, yAngle = 0, zAngle = 0;
if(vector.y == 0) xAngle = 0;
else xAngle = Math.atan(vector.z / vector.y);
if(vector.z == 0) yAngle = 0;
else yAngle = Math.atan(vector.x / vector.z);
if(vector.y == 0) zAngle = 0;
else zAngle = Math.atan(-1 * vector.x / vector.y);

pointX = 0;
pointY = 1;
pointZ = 0;

//hardcoded calculation that would be done in a rotational matrix
pointY = Math.cos(xAngle)*pointY + Math.sin(xAngle)*pointZ;
pointZ = -1*Math.sin(xAngle)*pointY + Math.cos(xAngle)*pointZ;
pointX = Math.cos(yAngle)*pointX + -1*Math.sin(yAngle)*pointZ;
pointZ = Math.sin(yAngle)*pointX + Math.cos(yAngle)*pointZ;
pointX = Math.cos(zAngle)*pointX + Math.sin(zAngle)*pointY;
pointY = -1*Math.sin(zAngle)*pointX + Math.cos(zAngle)*pointY;

If there exists some method which can do this for me, or some short vector or matrix manipulation (I have a vecmath.jar with simple vector and matrix classes and methods), or you have some short code that does just what i want, please tell me. Thanks a lot!
crystalsquid

Junior Devvie

... Boing ...

 « Reply #1 - Posted 2003-11-30 18:40:09 »

This is easier than that. Matrices are just arrays of axis vectors. What you are after is a matrix that has the y-axis pointing along the vector you describe. This pseudo-code should show how to do it, it will need tweaking to whichever vactor & matrix classes you use:

1) Start with our desired y-axis:

myVector y = vector;

Then set the z-axis to point up Z:

myVector z(0, 0, 1);

You then build the true x-axis by:

myVector x = y.crossProduct( z );
x.normalize();

Finally, build the true z-axis:

z = x.crossProduct( y );

Then build the matrix:

myMatrixClass m;
m.setXAxis( x );
m.setYAxis( y );
m.setZAxis( z );

And that is your rotation matrix.
Note that this one should leave +z in roughly the same direction as the original z-axis, so the point (0,0,1) should come out with a positive z always. If you set x instead of z and change the cross-products to:
z = x.cross( y ); x.normalize(); x = y.cross( z );
That will keep the x-axis in the same direction instead.
Hope this helps,

Dom
swpalmer

JGO Coder

Exp: 12 years

Where's the Kaboom?

 « Reply #2 - Posted 2003-11-30 21:12:44 »

Do you need that step of building the 'true' Z-axis? Since you used the 0,0,1 Z to create X from Y&Z in the first place, shouldn't it come out the same (0,0,1) when you derive Z from X&Y?

 Games published by our own members! Check 'em out!
crystalsquid

Junior Devvie

... Boing ...

 « Reply #3 - Posted 2003-12-02 12:21:41 »

Yep, because:

(y-axis fixed)
z-axis setto up <- this is probably not 90 degrees to Y

make x-axis - due to cross product, this IS perpendicular to the y and z axis, but z is still not perpendicular to Y

build true Z - this one will now be truly perpendicular.

The standard use for this is aligning a game camera in the world (except z is fixed, and y is up). You make a normal from the camera position to the point of interest (character usually), and set the camera z-axis to this (or away from in OpenGL I think). You then set Y axis to straight up. The first cross product gets you the right axis for the camera. The last cross product gets you the 'tilted' up-axis of the camera (for example if you were looking 45 degrees down along the z axis, the y-axis should come out as (0, 0.707, 0.707) not the (0,1,0) we fed in at the start.
Hope that makes sense (I have a tendancy to ramble...)

- Dom
swpalmer

JGO Coder

Exp: 12 years

Where's the Kaboom?

 « Reply #4 - Posted 2003-12-03 00:43:35 »

makes perfect sense.  I don't know where my brain was.  It's been far to long since I've done some real math...  I keep meaning to find the time to do some 3D stuff to refresh my memory but it just hasn't happned yet... Got any spare time you can lend me?

One note:
I think this will fail if the desired y is such that it also just happens to be pointing straight 'up', since ZY won't define a plane.  Be careful of those edge conditions that cause errors when you least expect them.

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Riven (15 views) 2015-07-03 11:22:16 deepthought (42 views) 2015-06-30 15:39:44 deepthought (46 views) 2015-06-30 15:39:09 deepthought (56 views) 2015-06-30 15:36:52 Za\'Anzabar (26 views) 2015-06-29 05:44:54 TritonDreyja (42 views) 2015-06-24 17:10:40 CopyableCougar4 (41 views) 2015-06-23 00:34:45 BurntPizza (45 views) 2015-06-21 20:36:46 cookiecompiler (87 views) 2015-06-11 15:42:53 cookiecompiler (50 views) 2015-06-11 15:41:14
 princec 34x wessles 24x BurntPizza 19x opiop65 18x CopyableCougar4 18x Riven 15x nsigma 15x theagentd 15x EgonOlsen 14x KaiHH 13x KevinWorkman 11x sunburn 11x SauronWatchesYou 11x Archive 10x NegativeZero 10x ags1 10x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org