Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikObjectMovement.java @ bef47287

1 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.object;
21
22 efef689c Leszek Koltunski
import org.distorted.library.type.Static2D;
23 37a25788 Leszek Koltunski
import org.distorted.library.type.Static3D;
24 27a70eae Leszek Koltunski
import org.distorted.library.type.Static4D;
25
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
28
public abstract class RubikObjectMovement
29
  {
30 37a25788 Leszek Koltunski
  float[] mTouch;
31 dd65ead3 Leszek Koltunski
  int mRotationVect, mLastTouchedAxis;
32
33 bef47287 Leszek Koltunski
  private float[] mPoint, mCamera, mDiff, m2Dpoint;
34 dd65ead3 Leszek Koltunski
  private int mLastTouchedLR;
35
  private int mNumAxis, mNumFacesPerAxis;
36
  private int[] mPossible;
37 37a25788 Leszek Koltunski
  private float mDistanceCenterFace;
38
  private Static3D[] mAxis;
39 dd65ead3 Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42
  abstract boolean isInsideFace(float[] point);
43
  abstract float fillUpRotationVectAndOffset(float[] vect, int[] possible);
44
  abstract float returnAngle(float[] vect, int[] possible);
45 37a25788 Leszek Koltunski
  abstract void fillPossibleRotations(int axis, int[] output);
46 dd65ead3 Leszek Koltunski
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 37a25788 Leszek Koltunski
  RubikObjectMovement(Static3D[] axis, int numFacesPerAxis, float distance)
50 dd65ead3 Leszek Koltunski
    {
51
    mPoint = new float[3];
52
    mCamera= new float[3];
53
    mDiff  = new float[3];
54
    mTouch = new float[3];
55
56 bef47287 Leszek Koltunski
    m2Dpoint = new float[2];
57
58 37a25788 Leszek Koltunski
    mAxis = axis;
59
    mNumAxis = mAxis.length;
60 dd65ead3 Leszek Koltunski
    mNumFacesPerAxis = numFacesPerAxis;
61 37a25788 Leszek Koltunski
    mDistanceCenterFace = distance;
62 dd65ead3 Leszek Koltunski
    mPossible = new int[mNumAxis-1];
63
    }
64
65 37a25788 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67
  private boolean faceIsVisible(Static3D axis, int lr)
68
    {
69
    float castCameraOnAxis = mCamera[0]*axis.get0() + mCamera[1]*axis.get1() + mCamera[2]*axis.get2();
70
    return (2*lr-1)*castCameraOnAxis > mDistanceCenterFace;
71
    }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
75
// compute point 'output[]' which:
76
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0)) [and this
77
//    distance is +-mDistanceCenterFace, depending if it is the face on the left or the right end of
78
//    the axis] (lr=0 or 1, so (2lr-1)*mDistanceCenterFace)
79
// 2) is co-linear with mCamera and mPoint
80
//
81
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
82
83
  private void castTouchPointOntoFace(Static3D axis, int lr, float[] output)
84
    {
85
    float d0 = mPoint[0]-mCamera[0];
86
    float d1 = mPoint[1]-mCamera[1];
87
    float d2 = mPoint[2]-mCamera[2];
88
    float a0 = axis.get0();
89
    float a1 = axis.get1();
90
    float a2 = axis.get2();
91
92
    float denom = a0*d0 + a1*d1 + a2*d2;
93
94
    if( denom != 0.0f )
95
      {
96
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
97
      float distance = (2*lr-1)*mDistanceCenterFace;
98
      float alpha = (distance-axisCam)/denom;
99
100
      output[0] = mCamera[0] + d0*alpha;
101
      output[1] = mCamera[1] + d1*alpha;
102
      output[2] = mCamera[2] + d2*alpha;
103
      }
104
    }
105
106 bef47287 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
108
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
109
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
110
// original 3D Y axis and our 2D in-plane origin.
111
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
112
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
113
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
114
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
115
116
  private void convertTo2Dcoords(float[] point3D, Static3D axis, int lr, float[] output)
117
    {
118
    float y0,y1,y2, x0,x1,x2;  // base X and Y vectors of the 2D coord system
119
    float a0 = axis.get0();
120
    float a1 = axis.get1();
121
    float a2 = axis.get2();
122
123
    if( lr==0 )
124
      {
125
      a0=-a0; a1=-a1; a2=-a2;
126
      }
127
128
    if( a0==0.0f && a2==0.0f )
129
      {
130
      y0=0; y1=0; y2=-a1;
131
      }
132
    else if( a1==0.0f )
133
      {
134
      y0=0; y1=1; y2=0;
135
      }
136
    else
137
      {
138
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
139
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=a2;
140
      }
141
142
    x0 = y1*a2 - y2*a1;  //
143
    x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
144
    x2 = y0*a1 - y1*a0;  //
145
146
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
147
148
    float origin0 = originAlpha*a0; // coords of the point where axis
149
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
150
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
151
152
    float v0 = point3D[0] - origin0;
153
    float v1 = point3D[1] - origin1;
154
    float v2 = point3D[2] - origin2;
155
156
    output[0] = v0*x0 + v1*x1 + v2*x2;
157
    output[1] = v0*y0 + v1*y1 + v2*y2;
158
    }
159
160 dd65ead3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
161
// PUBLIC API
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
165
    {
166
    mPoint[0]  = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
167
    mPoint[1]  = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
168
    mPoint[2]  = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
169
170
    mCamera[0] = rotatedCamera.get0()/RubikObject.OBJECT_SCREEN_RATIO;
171
    mCamera[1] = rotatedCamera.get1()/RubikObject.OBJECT_SCREEN_RATIO;
172
    mCamera[2] = rotatedCamera.get2()/RubikObject.OBJECT_SCREEN_RATIO;
173
174
    for( mLastTouchedAxis=0; mLastTouchedAxis<mNumAxis; mLastTouchedAxis++)
175
      {
176
      for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
177
        {
178 37a25788 Leszek Koltunski
        if( faceIsVisible(mAxis[mLastTouchedAxis], mLastTouchedLR) )
179 dd65ead3 Leszek Koltunski
          {
180 37a25788 Leszek Koltunski
          castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mTouch);
181 bef47287 Leszek Koltunski
          convertTo2Dcoords(mTouch, mAxis[mLastTouchedAxis], mLastTouchedLR, m2Dpoint);
182 dd65ead3 Leszek Koltunski
183 bef47287 Leszek Koltunski
          if( isInsideFace(m2Dpoint) )
184 dd65ead3 Leszek Koltunski
            {
185 bef47287 Leszek Koltunski
 // android.util.Log.e("move", "touched "+mLastTouchedAxis+" touch point: ("+m2Dpoint[0]+","+m2Dpoint[1]+")");
186
187 dd65ead3 Leszek Koltunski
            fillPossibleRotations(mLastTouchedAxis, mPossible);
188
            return true;
189
            }
190
          }
191
        }
192
      }
193
194
    return false;
195
    }
196
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
199
  public Static2D newRotation(Static4D rotatedTouchPoint)
200
    {
201
    mPoint[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO;
202
    mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
203
    mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
204
205 37a25788 Leszek Koltunski
    castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mDiff);
206 dd65ead3 Leszek Koltunski
207
    mDiff[0] -= mTouch[0];
208
    mDiff[1] -= mTouch[1];
209
    mDiff[2] -= mTouch[2];
210
211
    float offset = fillUpRotationVectAndOffset(mDiff, mPossible);
212
    return new Static2D(mRotationVect,offset);
213
    }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  public float continueRotation(Static4D rotatedTouchPoint)
218
    {
219 bef47287 Leszek Koltunski
    mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO - mPoint[0];
220
    mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO - mPoint[1];
221
    mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO - mPoint[2];
222 dd65ead3 Leszek Koltunski
223
    return (mLastTouchedLR-0.5f)*returnAngle(mDiff, mPossible);
224
    }
225 27a70eae Leszek Koltunski
  }