Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (497) Games in Android Showcase (114) games submitted by our members Games in WIP (563) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Math Question, I suck at math  (Read 1539 times) 0 Members and 1 Guest are viewing this topic.
NexusOne

Junior Member

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 Member

... 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:

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

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?

crystalsquid

Junior Member

... 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

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.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 BurntPizza (22 views) 2014-09-19 03:14:18 Dwinin (35 views) 2014-09-12 09:08:26 Norakomi (63 views) 2014-09-10 13:57:51 TehJavaDev (90 views) 2014-09-10 06:39:09 Tekkerue (44 views) 2014-09-09 02:24:56 mitcheeb (65 views) 2014-09-08 06:06:29 BurntPizza (48 views) 2014-09-07 01:13:42 Longarmx (35 views) 2014-09-07 01:12:14 Longarmx (40 views) 2014-09-07 01:11:22 Longarmx (37 views) 2014-09-07 01:10:19
 BurntPizza 37x Riven 18x Rayvolution 18x ags1 16x princec 16x basil_ 16x KevinWorkman 15x LiquidNitrogen 12x kevglass 12x theagentd 11x nsigma 11x deathpat 10x HeroesGraveDev 9x The Lion King 7x TehJavaDev 6x Gibbo3771 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 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