Project

General

Profile

« Previous | Next » 

Revision 0c5d8bf7

Added by Leszek Koltunski over 2 years ago

FIx some issues with ShapeChanging; implement per-object TouchControlSquare and TouchControlMirror.

View differences:

src/main/java/org/distorted/objectlib/touchcontrol/TouchControlShapeConstant.java
93 93
    mCastedRotAxis   = new float[mNumFaceAxis][rotAxis.length][2];
94 94
    mCastedRotAxis4D = new Static4D[mNumFaceAxis][rotAxis.length];
95 95

  
96
    float fx,fy,fz,f;
97

  
98 96
    for( int casted=0; casted<rotAxis.length; casted++)
99 97
      {
100 98
      Static3D a = rotAxis[casted];
......
104 102

  
105 103
      for( int face=0; face<mNumFaceAxis; face++)
106 104
        {
107
        convertTo2Dcoords( mPoint, face, mCastedRotAxis[face][casted]);
105
        float ax = mFaceAxis[face].get0();
106
        float ay = mFaceAxis[face].get1();
107
        float az = mFaceAxis[face].get2();
108

  
109
        convertTo2Dcoords( mPoint, ax,ay,az, mCastedRotAxis[face][casted]);
108 110
        normalize2D(mCastedRotAxis[face][casted]);
109 111

  
110
        fx = mFaceAxis[face].get0();
111
        fy = mFaceAxis[face].get1();
112
        fz = mFaceAxis[face].get2();
113
        f  = mPoint[0]*fx + mPoint[1]*fy + mPoint[2]*fz;
114
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*fx, mPoint[1]-f*fy, mPoint[2]-f*fz, 0);
112
        float f = mPoint[0]*ax + mPoint[1]*ay + mPoint[2]*az;
113
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*ax, mPoint[1]-f*ay, mPoint[2]-f*az, 0);
115 114
        }
116 115
      }
117 116
    }
......
125 124
    vect[1] /= len;
126 125
    }
127 126

  
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
130

  
131
  private int computeRotationIndex(float[][] rotAxis, float[] move2D, int[] enabled)
132
    {
133
    float cosAngle, minCosAngle = Float.MAX_VALUE;
134
    int minIndex=0, index;
135
    float m0 = move2D[0];
136
    float m1 = move2D[1];
137
    int numAxis = enabled[0];
138

  
139
    for(int axis=1; axis<=numAxis; axis++)
140
      {
141
      index = enabled[axis];
142
      cosAngle = m0*rotAxis[index][0] + m1*rotAxis[index][1];
143
      if( cosAngle<0 ) cosAngle = -cosAngle;
144

  
145
      if( cosAngle<minCosAngle )
146
        {
147
        minCosAngle=cosAngle;
148
        minIndex = index;
149
        }
150
      }
151

  
152
    return minIndex;
153
    }
154

  
155 127
///////////////////////////////////////////////////////////////////////////////////////////////////
156 128
// in the center of the face offset is always 0 regardless of the axis
157 129

  
......
201 173
      }
202 174
    }
203 175

  
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
206
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
207
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
208
// original 3D Y axis and our 2D in-plane origin.
209
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
210
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
211
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
212
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
213

  
214
  private void convertTo2Dcoords(float[] point3D, int index , float[] output)
215
    {
216
    Static3D faceAxis = mFaceAxis[index];
217

  
218
    float y0,y1,y2; // base Y vector of the 2D coord system
219
    float a0 = faceAxis.get0();
220
    float a1 = faceAxis.get1();
221
    float a2 = faceAxis.get2();
222

  
223
    if( a0==0.0f && a2==0.0f )
224
      {
225
      y0=0; y1=0; y2=-a1;
226
      }
227
    else if( a1==0.0f )
228
      {
229
      y0=0; y1=1; y2=0;
230
      }
231
    else
232
      {
233
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
234
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
235
      }
236

  
237
    float x0 = y1*a2 - y2*a1;  //
238
    float x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
239
    float x2 = y0*a1 - y1*a0;  //
240

  
241
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
242

  
243
    float origin0 = originAlpha*a0; // coords of the point where axis
244
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
245
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
246

  
247
    float v0 = point3D[0] - origin0;
248
    float v1 = point3D[1] - origin1;
249
    float v2 = point3D[2] - origin2;
250

  
251
    output[0] = v0*x0 + v1*x1 + v2*x2;
252
    output[1] = v0*y0 + v1*y1 + v2*y2;
253
    }
254

  
255 176
///////////////////////////////////////////////////////////////////////////////////////////////////
256 177

  
257 178
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
......
420 341
      if( faceIsVisible(mLastTouchedFace) )
421 342
        {
422 343
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
423
        convertTo2Dcoords(mTouch, mLastTouchedFace, mPoint2D);
344

  
345
        float ax = mFaceAxis[mLastTouchedFace].get0();
346
        float ay = mFaceAxis[mLastTouchedFace].get1();
347
        float az = mFaceAxis[mLastTouchedFace].get2();
348

  
349
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
424 350
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
425 351
        }
426 352
      }
......
437 363
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
438 364

  
439 365
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
440
    convertTo2Dcoords(mTouch, mLastTouchedFace, mMove2D);
366

  
367
    float ax = mFaceAxis[mLastTouchedFace].get0();
368
    float ay = mFaceAxis[mLastTouchedFace].get1();
369
    float az = mFaceAxis[mLastTouchedFace].get2();
370

  
371
    convertTo2Dcoords(mTouch, ax,ay,az, mMove2D);
441 372

  
442 373
    mMove2D[0] -= mPoint2D[0];
443 374
    mMove2D[1] -= mPoint2D[1];

Also available in: Unified diff