Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Rotating a Vector3f by a Vector3f in LWJGL  (Read 2158 times)
0 Members and 1 Guest are viewing this topic.
Offline 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);
       }
Offline ra4king

JGO Kernel


Medals: 356
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?

Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 114
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... ? Cheesy

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 Cheesy
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 Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline 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.
Offline 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);
Offline Tim Spekler

« JGO Spiffy Duke »


Medals: 58
Projects: 2
Exp: 4 years



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

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (136 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!