Project

General

Profile

Download (4.6 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / helpers / QuatHelper.java @ b9d4aa3b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.helpers;
21

    
22
import org.distorted.library.type.Static4D;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
public class QuatHelper
27
  {
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
// return quat1*quat2
30

    
31
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
32
      {
33
      float qx = quat1.get0();
34
      float qy = quat1.get1();
35
      float qz = quat1.get2();
36
      float qw = quat1.get3();
37

    
38
      float rx = quat2.get0();
39
      float ry = quat2.get1();
40
      float rz = quat2.get2();
41
      float rw = quat2.get3();
42

    
43
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
44
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
45
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
46
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
47

    
48
      return new Static4D(tx,ty,tz,tw);
49
      }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
53

    
54
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
55
      {
56
      float qx = quat.get0();
57
      float qy = quat.get1();
58
      float qz = quat.get2();
59
      float qw = quat.get3();
60

    
61
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
62
      Static4D tmp = quatMultiply(quat,vector);
63

    
64
      return quatMultiply(tmp,quatInverted);
65
      }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
69

    
70
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
71
      {
72
      float qx = quat.get0();
73
      float qy = quat.get1();
74
      float qz = quat.get2();
75
      float qw = quat.get3();
76

    
77
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
78
      Static4D tmp = quatMultiply(quatInverted,vector);
79

    
80
      return quatMultiply(tmp,quat);
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
    public static Static4D quatFromDrag(float dragX, float dragY)
86
      {
87
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
88
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
89
      float axisZ = 0;
90
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
91

    
92
      if( axisL>0 )
93
        {
94
        axisX /= axisL;
95
        axisY /= axisL;
96
        axisZ /= axisL;
97

    
98
        float ratio = axisL;
99
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
100

    
101
        float cosA = (float)Math.cos(Math.PI*ratio);
102
        float sinA = (float)Math.sqrt(1-cosA*cosA);
103

    
104
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
105
        }
106

    
107
      return new Static4D(0f, 0f, 0f, 1f);
108
      }
109
  }
(8-8/12)