Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
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
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Quat4f and rotation around local axis  (Read 1778 times)
0 Members and 1 Guest are viewing this topic.
Offline ernest210

Senior Newbie





« Posted 2012-03-06 18:59:50 »

I assume there is a better way to do what I want to do.
I haven't problem with rotation cube around axis unfortunately global axis.
When I want to make the rotation around local axis I just remember central point of my cube then move cube to position x=0, y=0, z=0 and I start rotation.
After rotation I move cube back to saved position.
Is there any way to do it without moving cobe?
This is my code responsible for rotation.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
private static float angle = 4;
   private Quat4f roll = new Quat4f();
   private Quat4f quantBase = new Quat4f();
   private AxisAngle4f angle4f = new AxisAngle4f();
   private void update(boolean rotX, boolean rotY, boolean rotZ){
      if(rotX){
         angle4f.set(new Vector3f(1, 0, 0), angle * (float) (Math.PI / 180));
      } if(rotY) {
         angle4f.set(new Vector3f(0, 1, 0), angle * (float) (Math.PI / 180));
      } if(rotZ) {
         angle4f.set(new Vector3f(0, 0, 1), angle * (float) (Math.PI / 180));
      }
      roll.set(angle4f);
     
      for(int i = 0;i<array.length;i+=3) {
         quantBase.x = array[i];
         quantBase.y = array[i+1];
         quantBase.z = array[i+2];
         quantBase.w = 0f;
         quantBase.mul(roll, quantBase);
         if(rotX) {
            array[i+1] = quantBase.y;
            array[i+2] = quantBase.z;
         }
         if(rotY) {
            array[i] = quantBase.x;
            array[i+2] = quantBase.z;
         }
         if(rotZ) {
            array[i] = quantBase.x;
            array[i+1] = quantBase.y;
         }
      }
       vertexData.put(array);
        vertexData.flip();
   }

For clarity, this is how I understand the local and global axis.

Offline Roquen
« Reply #1 - Posted 2012-03-06 19:59:12 »

If you're going to transform more than a couple of points, you want to convert to a matrix.  see here: http://www.java-gaming.org/topics/skeletal-animation/25483/view.html
Offline ernest210

Senior Newbie





« Reply #2 - Posted 2012-03-15 19:41:26 »

I did it in this way
When i press key to rotate my cube I call this method which only remember central point cube.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
float localX = 0;
float localY = 0;
float localZ = 0;
private void setPosition(){
   float lX = 0;
   float lY = 0;
   float lZ = 0;
   for(int i = 0;i<array.length;i+=3) {
      lX+=array[i];
      lY+=array[i+1];
      lZ+=array[i+2];
      localX=lX/24;
      localY=lY/24;
      localZ=lZ/24;
   }
   System.out.println("x: "+localX+" y: "+localY+" z: "+localZ );
}


and then start the rotation
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
private Vector4f vBase = new Vector4f();
private Vector4f angle = new Vector4f();
private void update(boolean rotX, boolean rotY, boolean rotZ){
   if (rotX) {
      angle = rotationX(angle, DEGTORAD);
   }
   if (rotY) {
      angle = rotationY(angle, DEGTORAD);
   }
   if (rotZ) {
      angle = rotationZ(angle, DEGTORAD);
   }
   
   for(int i = 0;i<array.length;i+=3) {
      vBase.x = array[i];
      vBase.y = array[i+1];
      vBase.z = array[i+2];
      vBase.w = (DEGTORAD);
     
      Vector4f vFin = new Vector4f();
     
      vBase.x -= localX;
      vBase.y -= localY;
      vBase.z -= localZ;
      mul(vFin, angle, vBase);
      vFin.x +=localX;
      vFin.y +=localY;
      vFin.z +=localZ;
     
      if(rotX) {
         array[i+1] = vFin.y;
         array[i+2] = vFin.z;
      }
      if(rotY) {
         array[i] = vFin.x;
         array[i+2] = vFin.z;
      }
      if(rotZ) {
         array[i] = vFin.x;
         array[i+1] = vFin.y;
      }
   }
    vertexData.put(array);
    vertexData.flip();
}

private Vector4f rotationX(Vector4f q, float a){
   System.out.println("-- rotationX --");
   a *= 0.5f;
   float sinA = (float) Math.sin(a);
   float cosA = (float) Math.cos(a);
   q.set(sinA, 0, 0, cosA);
   return q;
}

private Vector4f rotationY(Vector4f q, float a){
   System.out.println("-- rotationY --");
   a *= 0.5f;
   float sinA = (float) Math.sin(a);
   float cosA = (float) Math.cos(a);
   q.set(0.0f, sinA, 0.0f, cosA);
   return q;
}

private Vector4f rotationZ(Vector4f q, float a){
   System.out.println("-- rotationZ --");
   a *= 0.5f;
   float sinA = (float) Math.sin(a);
   float cosA = (float) Math.cos(a);
   q.set(0.0f, 0.0f, sinA, cosA);
   return q;
}

void mul(Vector4f out, Vector4f q1, Vector4f q2) {
   System.out.println("-- mul --");
   out.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
   out.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;
   out.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;
   out.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
}

All this works quite well.
But now I want to something more I want to change size but not all cube only width or height or thickness and I want to do this
on rotated cube.
I know I must to do something like this
Px' = S1 * Px
but it is not enough beacause from this

I'm geting this

Scaling works only on not rotated cube. I assume that I need to add a multiplication by the angle but I am not store information about angle.
Can you give me some advice?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Stranger

Senior Member


Medals: 6



« Reply #3 - Posted 2012-03-16 18:24:39 »

Try to apply this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
    public static void mul(Vector4f q, Vector3f vCoord, Vector3f scale, Vector3f result) {
        Matrix3f m1 = new Matrix3f();

        setFromQuat(q.getX(), q.getY(), q.getZ(), q.getW(), m1);

        Matrix3f tmp = new Matrix3f();

        tmp.setIdentity();
        tmp.m00(scale.getX());
        tmp.m11(scale.getY());
        tmp.m22(scale.getZ());

        m1.mul(tmp);

        transform(m1, vCoord, result);
    }

    private static void setFromQuat(float a, float b, float c, float d, Matrix3f m) {
        final float n = a * a + b * b + c * c + d * d;
        final float s = (n > 0.0f) ? (2.0f / n) : 0.0f;

        final float xs = a * s, ys = b * s, zs = c * s;
        final float wx = d * xs, wy = d * ys, wz = d * zs;
        final float xx = a * xs, xy = a * ys, xz = a * zs;
        final float yy = b * ys, yz = b * zs, zz = c * zs;

        m.m00(1.0f - (yy + zz));
        m.m01(xy - wz);
        m.m02(xz + wy);
        m.m10(xy + wz);
        m.m11(1.0f - (xx + zz));
        m.m12(yz - wx);
        m.m20(xz - wy);
        m.m21(yz + wx);
        m.m22(1.0f - (xx + yy));
    }

    private static void transform(Matrix3f m, Vector3f t3f, Vector3f result) {
        result.set(m.m00() * t3f.getX() + m.m01() * t3f.getY() + m.m02() * t3f.getZ(),
                m.m10() * t3f.getX() + m.m11() * t3f.getY() + m.m12() * t3f.getZ(),
                m.m20() * t3f.getX() + m.m21() * t3f.getY() + m.m22() * t3f.getZ()
        );
    }

It's a draft solution. Unfortunately, I can't test it now, so hopefully it will work with a little changes.

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

radar3301 (13 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (104 views)
2014-09-10 06:39:09

Tekkerue (51 views)
2014-09-09 02:24:56
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-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
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!