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
 Rotating a Vector3f by a Vector3f in LWJGL  (Read 1949 times) 0 Members and 1 Guest are viewing this topic.
CyanPrime
 « Posted 2012-06-14 02:39:25 »

Alright, So I got this code where I punch in these values:

 1  2 `    rot      :1.0416666   0.0   -0.0    pos      :-71.25   67.5   91.875`

And get these values back:

 1 `    rotPos   :-71.25   67.500015   91.874985`

Now I'm clearly trying to rotate a Vector3f by another Vector3f, but the following code isn't rotating anything. Can someone tell me what I'm doing wrong?

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `    public Vector3f rotV3fByV3f(Vector3f npos, Vector3f nrot){              Vector3f pos = new Vector3f(0,0,0);               Vector3f rot = new Vector3f(0,0,0);               Matrix4f  matrix = new Matrix4f();               matrix.setIdentity();                              pos = new Vector3f(npos);                              matrix.m03 = pos.x;               matrix.m13 = pos.y;               matrix.m23 = pos.z;                              rot = new Vector3f(nrot);               Matrix4f.rotate((float) Math.toRadians(360), rot, matrix, matrix);                   return new Vector3f(matrix.m03, matrix.m13, matrix.m23);       }`
ra4king

JGO Kernel

Medals: 345
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #1 - Posted 2012-06-14 07:58:04 »

I'm trying to wrap my head around how you can even rotate a vector by another vector....how does that make any sense?

Now do you want to rotate your vector around another vector by a certain angle?

Roquen
 « Reply #2 - Posted 2012-06-14 08:14:03 »

I'm assuming one represents the axis of rotation...you additionally need an angle.  The code example doesn't make any sense.  You're shoving what I'm assuming to be the axis of rotation into a translation slot..assuming that's the convention you've chosen.
matheus23

JGO Kernel

Medals: 107
Projects: 3

You think about my Avatar right now!

 « Reply #3 - Posted 2012-06-14 13:36:59 »

Oh okey... seems like, you've been really tired at the time you wrote this... ?

First, you create the Vector3f's "pos" and "rot" 2 times. In lines 2 and 3, and in lines 7 and 13 again.

Also, that code seems to work just as it should.
But wtf???
You rotate the matrix by 360 ° ?!? Of course you get the same vector again
 1 `Matrix4f.rotate((float) Math.toRadians(360), rot, matrix, matrix);`

These guys above me are right, you can only rotate a vector by another vector and an angle (or just by another Quaternion).
So I'd suggest:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `public Vector3f rotV3fByV3f(Vector3f npos, Vector3f nrot, float rotation) {   Matrix4f matrix = new Matrix4f();   Vector3f pos = new Vector3f(npos);   matrix.m03 = pos.x;   matrix.m13 = pos.y;   matrix.m23 = pos.z;   Vector3f rot = new Vector3f(nrot);   Matrix4f.rotate((float) Math.toRadians(rotation), rot, matrix, matrix);   return new Vector3f(matrix.m03, matrix.m13, matrix.m23);}`

EDIT: Removed "matrix.setIdentity()". The matrix is already an identity matrix, if you create it

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Roquen
 « Reply #4 - Posted 2012-06-14 14:45:46 »

Still not a rotation matrix.  A quick glance at the API yields:

public Matrix4f rotate(float angle, Vector3f axis)
public Matrix4f rotate(float angle, Vector3f axis, Matrix4f dest)

Which I'd assume create rotation matrices from an axis and an angle.
CyanPrime
 « Reply #5 - Posted 2012-06-16 19:27:09 »

I was wondering what the correct way to rotate Matrix4fs by 3 axises is.

This is what I got so far:

 1  2  3  4  5 `Matrix4f x =new Matrix4f(mat_transform.rotate(rotationVector.x , new Vector3f(1,0,0)));              Matrix4f y =new Matrix4f(mat_transform.rotate(rotationVector.y , new Vector3f(0,1,0)));              Matrix4f z =new Matrix4f(mat_transform.rotate(rotationVector.z , new Vector3f(0,0,1)));                            mat_transform = Matrix4f.mul(Matrix4f.mul(x,y,null), z, null);`
Tim Spekler
 « Reply #6 - Posted 2012-06-19 21:29:18 »

If you 're wondering how to correctly use rotation to move in a 3D world,then there is not just one way to rotate matrix in a 3D space. You always have to choose in which order you're going to apply your rotation (first x, then y and the z, or another way) and each order implies different results.
In conclusion, you have to choose the correct order that matches to your expectation.

If your talking about the implementation, here is what I do for a FPS:
 1  2  3  4 `Matrix4f camera = new Matrix4f();camera.translate(new Vector3f(X, Y, Z));camera.rotate(Yangle,new Vector3f(0,1,0));camera.rotate(Xangle,new Vector3f(1,0,0));`

Hope it helps
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 (8 views) 2014-09-21 00:34:41 moogie (7 views) 2014-09-21 00:26:15 UprightPath (22 views) 2014-09-20 20:14:06 BurntPizza (27 views) 2014-09-19 03:14:18 Dwinin (40 views) 2014-09-12 09:08:26 Norakomi (70 views) 2014-09-10 13:57:51 TehJavaDev (96 views) 2014-09-10 06:39:09 Tekkerue (49 views) 2014-09-09 02:24:56 mitcheeb (70 views) 2014-09-08 06:06:29 BurntPizza (52 views) 2014-09-07 01:13:42
 BurntPizza 37x Riven 18x Rayvolution 18x ags1 16x princec 16x basil_ 16x KevinWorkman 15x LiquidNitrogen 12x theagentd 11x kevglass 11x deathpat 11x nsigma 11x HeroesGraveDev 8x TehJavaDev 6x PandaMoniumHUN 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