Project

General

Profile

« Previous | Next » 

Revision 213c15de

Added by Leszek Koltunski about 2 years ago

Bandaged: progress with touch control.

View differences:

src/main/java/org/distorted/bandaged/BandagedCreatorTouchControl.java
28 28

  
29 29
public class BandagedCreatorTouchControl
30 30
{
31
  private static final float DIST3D = 0.5f;
32 31
  private static final float DIST2D = 0.5f;
33 32

  
34 33
  private final Static4D CAMERA_POINT;
......
36 35
  private final float[] mPoint2D;
37 36
  private float mObjectRatio;
38 37
  private final Static3D[] mFaceAxis;
38
  private final float[] mDist3D;
39 39

  
40 40
  private BandagedCubit[] mCubits;
41 41
  private int mNumCubits;
42 42
  private int mLastTouchedFace;
43
  private float mX, mY, mZ, mMax;
43 44

  
44 45
///////////////////////////////////////////////////////////////////////////////////////////////////
45 46

  
46
  private boolean isInsideFace(float[] p)
47
  private boolean isInsideFace(int face, float[] p)
47 48
    {
48 49
    return ( p[0]<=DIST2D && p[0]>=-DIST2D && p[1]<=DIST2D && p[1]>=-DIST2D );
49 50
    }
......
103 104
//
104 105
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
105 106

  
106
  private void castTouchPointOntoFace(int index, float[] output)
107
  private void castTouchPointOntoFace(int face, float[] output)
107 108
    {
108
    Static3D faceAxis = mFaceAxis[index];
109
    Static3D faceAxis = mFaceAxis[face];
109 110

  
110 111
    float d0 = mPoint[0]-mCamera[0];
111 112
    float d1 = mPoint[1]-mCamera[1];
......
119 120
    if( denom != 0.0f )
120 121
      {
121 122
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
122
      float alpha = (DIST3D-axisCam)/denom;
123
      float alpha = (mDist3D[face]-axisCam)/denom;
123 124

  
124 125
      output[0] = mCamera[0] + d0*alpha;
125 126
      output[1] = mCamera[1] + d1*alpha;
......
129 130

  
130 131
///////////////////////////////////////////////////////////////////////////////////////////////////
131 132

  
132
  private boolean faceIsVisible(int index)
133
  private void stretchPoint(int face, float[] output)
133 134
    {
134
    Static3D faceAxis = mFaceAxis[index];
135
    switch(face/2)
136
      {
137
      case 0: output[0] *= (mMax/mZ); output[1] *= (mMax/mY); break;
138
      case 1: output[0] *= (mMax/mX); output[1] *= (mMax/mZ); break;
139
      case 2: output[0] *= (mMax/mX); output[1] *= (mMax/mY); break;
140
      }
141
    }
142

  
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

  
145
  private boolean faceIsVisible(int face)
146
    {
147
    Static3D faceAxis = mFaceAxis[face];
135 148
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
136
    return castCameraOnAxis > DIST3D;
149
    return castCameraOnAxis > mDist3D[face];
137 150
    }
138 151

  
139 152
///////////////////////////////////////////////////////////////////////////////////////////////////
......
159 172
        float az = mFaceAxis[mLastTouchedFace].get2();
160 173

  
161 174
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
162
        if( isInsideFace(mPoint2D) ) return true;
175
        stretchPoint(mLastTouchedFace,mPoint2D);
176
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
163 177
        }
164 178
      }
165 179

  
......
170 184

  
171 185
  private void computePosition(int face, float pointX, float pointY)
172 186
    {
173
    int x = (int)(3*pointX+1.5f) -1;
174
    int y = (int)(3*pointY+1.5f) -1;
175

  
176 187
    switch(face)
177 188
      {
178
      case 0: mPos[0] = 1.0f; mPos[1] =    y; mPos[2] =   -x; break;
179
      case 1: mPos[0] =-1.0f; mPos[1] =    y; mPos[2] =    x; break;
180
      case 2: mPos[0] =    x; mPos[1] = 1.0f; mPos[2] =   -y; break;
181
      case 3: mPos[0] =    x; mPos[1] =-1.0f; mPos[2] =    y; break;
182
      case 4: mPos[0] =    x; mPos[1] =    y; mPos[2] = 1.0f; break;
183
      case 5: mPos[0] =   -x; mPos[1] =    y; mPos[2] =-1.0f; break;
189
      case 0: mPos[0] = (mX-1)/2;
190
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
191
              mPos[2] = (int)(-mZ*pointX-mZ/2)+(mZ-1)/2;
192
              break;
193
      case 1: mPos[0] =-(mX-1)/2;
194
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
195
              mPos[2] = (int)(+mZ*pointX+mZ/2)-(mZ-1)/2;
196
              break;
197
      case 2: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2;
198
              mPos[1] = (mY-1)/2;
199
              mPos[2] = (int)(-mZ*pointY-mZ/2)+(mZ-1)/2;
200
              break;
201
      case 3: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2;
202
              mPos[1] =-(mY-1)/2;
203
              mPos[2] = (int)(+mZ*pointY+mZ/2)-(mZ-1)/2;
204
              break;
205
      case 4: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2;
206
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
207
              mPos[2] = (mZ-1)/2;
208
              break;
209
      case 5: mPos[0] = (int)(-mX*pointX-mX/2)+(mX-1)/2;
210
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
211
              mPos[2] =-(mZ-1)/2;
212
              break;
184 213
      }
185 214
    }
186 215

  
......
215 244
    mTouch = new float[3];
216 245
    mPos   = new float[3];
217 246
    mPoint2D = new float[2];
247
    mDist3D  = new float[6];
218 248
    mFaceAxis = TouchControlHexahedron.FACE_AXIS;
219 249
    mObjectRatio = ratio;
220 250

  
......
227 257

  
228 258
///////////////////////////////////////////////////////////////////////////////////////////////////
229 259

  
230
  public void setCubits(BandagedCubit[] cubits)
260
  public void setCubits(BandagedCubit[] cubits, int x, int y, int z)
231 261
    {
232 262
    mCubits = cubits;
233 263
    mNumCubits = cubits.length;
264

  
265
    mX = x;
266
    mY = y;
267
    mZ = z;
268
    mMax = mX>mY ? Math.max(mX,mZ) : Math.max(mY,mZ);
269

  
270
    mDist3D[0] = mDist3D[1] = 0.5f*(mX/mMax);
271
    mDist3D[2] = mDist3D[3] = 0.5f*(mY/mMax);
272
    mDist3D[4] = mDist3D[5] = 0.5f*(mZ/mMax);
234 273
    }
235 274

  
236 275
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff