Project

General

Profile

« Previous | Next » 

Revision 903041cd

Added by Leszek Koltunski almost 3 years ago

Progress with BandagedCreator: marking a cubit by touching it.

View differences:

src/main/java/org/distorted/bandaged/BandagedCreatorRenderer.java
35 35

  
36 36
public class BandagedCreatorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
37 37
{
38
   private final GLSurfaceView mView;
38
   private final BandagedCreatorView mView;
39 39
   private final DistortedScreen mScreen;
40 40
   private final Static3D mScale;
41 41
   private final Static3D mMoveWhole;
......
43 43
   private final Static3D[] mMoveUnscaled;
44 44
   private final BandagedCubit[] mCubits;
45 45

  
46
   private final int COLOR_DEFAULT = 0xffffff55;
46
   static final int COLOR_DEFAULT = 0xffffff55;
47
   static final int COLOR_MARKED  = 0xffff0000;
48

  
49
   static final float SCREEN_RATIO = 0.5f;
50
   static final float OBJECT_SIZE  = 3.0f;
47 51

  
48 52
   private final float[][] POSITIONS = new float[][]
49 53
        {
......
144 148
      move.set(totalX,totalY,totalZ);
145 149
      }
146 150

  
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

  
153
   public BandagedCubit[] getCubits()
154
     {
155
     return mCubits;
156
     }
157

  
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

  
160
   public DistortedScreen getScreen()
161
     {
162
     return mScreen;
163
     }
164

  
147 165
///////////////////////////////////////////////////////////////////////////////////////////////////
148 166

  
149 167
   @Override
......
158 176
   @Override
159 177
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
160 178
      {
161
      final float Q = 0.15f;
179
      final float Q = SCREEN_RATIO/OBJECT_SIZE;
162 180
      float scale = width<height ? Q*width : Q*height;
163 181

  
164 182
      mScreen.detachAll();
......
184 202

  
185 203
      mScale.set( scale,scale,scale );
186 204
      mScreenMin = Math.min(width, height);
205
      mView.setScreenSize(width,height, (int)yOffset);
187 206
      mScreen.resize(width,height);
188 207
      }
189 208

  
src/main/java/org/distorted/bandaged/BandagedCreatorView.java
29 29

  
30 30
import com.google.firebase.crashlytics.FirebaseCrashlytics;
31 31

  
32
import org.distorted.library.main.DistortedScreen;
33

  
32 34
///////////////////////////////////////////////////////////////////////////////////////////////////
33 35

  
34 36
public class BandagedCreatorView extends GLSurfaceView
35 37
{
36 38
    private final static int DIRECTION_SENSITIVITY=  12;
37 39
    private int mX, mY;
40
    private int mTouchedIndex;
38 41
    private BandagedCreatorRenderer mRenderer;
42
    private BandagedTouchControl mTouchControl;
43
    private int mScreenWidth, mScreenHeight;
44
    private int mYoffset;
39 45

  
40 46
///////////////////////////////////////////////////////////////////////////////////////////////////
41 47
// PUBLIC API
......
52 58
        {
53 59
        BandagedCreatorActivity act = (BandagedCreatorActivity)context;
54 60
        mRenderer = new BandagedCreatorRenderer(this);
61
        BandagedCubit[] cubits = mRenderer.getCubits();
62
        DistortedScreen screen = mRenderer.getScreen();
63
        mTouchControl = new BandagedTouchControl(cubits, BandagedCreatorRenderer.SCREEN_RATIO, screen.getFOV() );
55 64

  
56 65
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
57 66

  
......
106 115

  
107 116
///////////////////////////////////////////////////////////////////////////////////////////////////
108 117

  
109
    @Override public boolean onTouchEvent(MotionEvent event)
118
    public void setScreenSize(int width, int height, int yOffset)
119
      {
120
      mScreenWidth = width;
121
      mScreenHeight= height;
122
      mYoffset     = yOffset;
123
      }
124

  
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

  
127
    @Override
128
    public boolean onTouchEvent(MotionEvent event)
110 129
      {
111 130
      int action = event.getAction();
112 131
      int x = (int)event.getX();
113
      int y = (int)event.getY();
132
      int y = (int)event.getY() + mYoffset;
114 133

  
115 134
      switch(action)
116 135
         {
117
         case MotionEvent.ACTION_DOWN: mX = x;
118
                                       mY = y;
136
         case MotionEvent.ACTION_DOWN: float x1 = (x -  mScreenWidth*0.5f)/mRenderer.mScreenMin;
137
                                       float y1 = (mScreenHeight*0.5f - y)/mRenderer.mScreenMin;
138

  
139
                                       mTouchedIndex = mTouchControl.cubitTouched(x1,y1,mRenderer.mQuat2);
140

  
141
                                       if( mTouchedIndex<0 )
142
                                         {
143
                                         mX = x;
144
                                         mY = y;
145
                                         }
146
                                       else
147
                                         {
148
                                         mX = -1;
149
                                         mY = -1;
150

  
151
                                         mTouchControl.markCubit(mTouchedIndex, BandagedCreatorRenderer.COLOR_MARKED);
152
                                         }
153

  
119 154
                                       break;
120 155

  
121 156
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
......
147 182

  
148 183
         case MotionEvent.ACTION_UP  : mX = -1;
149 184
                                       mY = -1;
185

  
186
                                       if( mTouchedIndex>=0 )
187
                                         {
188
                                         mTouchControl.markCubit(mTouchedIndex, BandagedCreatorRenderer.COLOR_DEFAULT);
189
                                         mTouchedIndex = -1;
190
                                         }
191

  
150 192
        	                             resetQuats();
151 193
                                       break;
152 194
         }
src/main/java/org/distorted/bandaged/BandagedCubit.java
41 41
    private final DistortedEffects mEffects;
42 42
    private final MeshBase mMesh;
43 43
    private final float[] mPosition;
44
    private boolean mIsAttached;
44 45

  
45 46
///////////////////////////////////////////////////////////////////////////////////////////////////
46 47
// PUBLIC API
......
49 50
    public BandagedCubit(float[] position, Static4D quat1, Static4D quat2, Static3D move1, Static3D move2, Static3D scale, int color)
50 51
      {
51 52
      mPosition = position;
53
      mIsAttached = true;
52 54

  
53 55
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
54 56
      mMesh = factory.createMesh(position);
......
79 81
      mTexture.setColorARGB(color);
80 82
      }
81 83

  
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

  
86
    public void detach()
87
      {
88
      mIsAttached = false;
89
      }
90

  
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

  
93
    public boolean isAttached()
94
      {
95
      return mIsAttached;
96
      }
97

  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

  
100
    public float[] getPosition()
101
      {
102
      return mPosition;
103
      }
104

  
82 105
///////////////////////////////////////////////////////////////////////////////////////////////////
83 106

  
84 107
    public DistortedNode getNode()
src/main/java/org/distorted/bandaged/BandagedTouchControl.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.bandaged;
21

  
22
import org.distorted.library.main.QuatHelper;
23
import org.distorted.library.type.Static3D;
24
import org.distorted.library.type.Static4D;
25
import org.distorted.objectlib.touchcontrol.TouchControlHexahedron;
26

  
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

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

  
34
  private final Static4D CAMERA_POINT;
35
  private final BandagedCubit[] mCubits;
36
  private final int mNumCubits;
37
  private final float[] mPoint, mCamera, mTouch, mPos;
38
  private final float[] mPoint2D;
39
  private float mObjectRatio;
40
  private int mLastTouchedFace;
41
  private final Static3D[] mFaceAxis;
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

  
45
  private boolean isInsideFace(float[] p)
46
    {
47
    return ( p[0]<=DIST2D && p[0]>=-DIST2D && p[1]<=DIST2D && p[1]>=-DIST2D );
48
    }
49

  
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
52
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
53
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
54
// original 3D Y axis and our 2D in-plane origin.
55
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
56
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
57
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
58
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
59
// (ax,ay,az) - vector normal to the face surface.
60

  
61
  void convertTo2Dcoords(float[] point3D, float ax, float ay, float az , float[] output)
62
    {
63
    float y0,y1,y2; // base Y vector of the 2D coord system
64

  
65
    if( ax==0.0f && az==0.0f )
66
      {
67
      y0=0; y1=0; y2=-ay;
68
      }
69
    else if( ay==0.0f )
70
      {
71
      y0=0; y1=1; y2=0;
72
      }
73
    else
74
      {
75
      float norm = (float)(-ay/Math.sqrt(1-ay*ay));
76
      y0 = norm*ax; y1= norm*(ay-1/ay); y2=norm*az;
77
      }
78

  
79
    float x0 = y1*az - y2*ay;  //
80
    float x1 = y2*ax - y0*az;  // (2D coord baseY) x (axis) = 2D coord baseX
81
    float x2 = y0*ay - y1*ax;  //
82

  
83
    float originAlpha = point3D[0]*ax + point3D[1]*ay + point3D[2]*az;
84

  
85
    float origin0 = originAlpha*ax; // coords of the point where axis
86
    float origin1 = originAlpha*ay; // intersects surface plane i.e.
87
    float origin2 = originAlpha*az; // the origin of our 2D coord system
88

  
89
    float v0 = point3D[0] - origin0;
90
    float v1 = point3D[1] - origin1;
91
    float v2 = point3D[2] - origin2;
92

  
93
    output[0] = v0*x0 + v1*x1 + v2*x2;
94
    output[1] = v0*y0 + v1*y1 + v2*y2;
95
    }
96

  
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
99
// compute point 'output[]' which:
100
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
101
// 2) is co-linear with mCamera and mPoint
102
//
103
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
104

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

  
109
    float d0 = mPoint[0]-mCamera[0];
110
    float d1 = mPoint[1]-mCamera[1];
111
    float d2 = mPoint[2]-mCamera[2];
112
    float a0 = faceAxis.get0();
113
    float a1 = faceAxis.get1();
114
    float a2 = faceAxis.get2();
115

  
116
    float denom = a0*d0 + a1*d1 + a2*d2;
117

  
118
    if( denom != 0.0f )
119
      {
120
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
121
      float alpha = (DIST3D-axisCam)/denom;
122

  
123
      output[0] = mCamera[0] + d0*alpha;
124
      output[1] = mCamera[1] + d1*alpha;
125
      output[2] = mCamera[2] + d2*alpha;
126
      }
127
    }
128

  
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

  
131
  private boolean faceIsVisible(int index)
132
    {
133
    Static3D faceAxis = mFaceAxis[index];
134
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
135
    return castCameraOnAxis > DIST3D;
136
    }
137

  
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

  
140
  private boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
141
    {
142
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
143
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
144
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
145

  
146
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
147
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
148
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
149

  
150
    for( mLastTouchedFace=0; mLastTouchedFace<6; mLastTouchedFace++)
151
      {
152
      if( faceIsVisible(mLastTouchedFace) )
153
        {
154
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
155

  
156
        float ax = mFaceAxis[mLastTouchedFace].get0();
157
        float ay = mFaceAxis[mLastTouchedFace].get1();
158
        float az = mFaceAxis[mLastTouchedFace].get2();
159

  
160
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
161
        if( isInsideFace(mPoint2D) ) return true;
162
        }
163
      }
164

  
165
    return false;
166
    }
167

  
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

  
175
    switch(face)
176
      {
177
      case 0: mPos[0] = 1.0f; mPos[1] =    y; mPos[2] =   -x; break;
178
      case 1: mPos[0] =-1.0f; mPos[1] =    y; mPos[2] =    x; break;
179
      case 2: mPos[0] =    x; mPos[1] = 1.0f; mPos[2] =   -y; break;
180
      case 3: mPos[0] =    x; mPos[1] =-1.0f; mPos[2] =    y; break;
181
      case 4: mPos[0] =    x; mPos[1] =    y; mPos[2] = 1.0f; break;
182
      case 5: mPos[0] =   -x; mPos[1] =    y; mPos[2] =-1.0f; break;
183
      }
184
    }
185

  
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

  
188
  private int whichCubitTouched(int face, float pointX, float pointY)
189
    {
190
    computePosition(face,pointX,pointY);
191

  
192
    for(int c=0; c<mNumCubits; c++)
193
      if( mCubits[c].isAttached() )
194
        {
195
        float[] pos = mCubits[c].getPosition();
196
        int len = pos.length/3;
197

  
198
        for(int p=0; p<len; p++)
199
          if( pos[3*p]==mPos[0] && pos[3*p+1]==mPos[1] && pos[3*p+2]==mPos[2] ) return c;
200
        }
201

  
202
    android.util.Log.e("D", "whichCubitTouched: IMPOSSIBLE!!");
203
    return -1;
204
    }
205

  
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
// PUBLIC API
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

  
210
  public BandagedTouchControl(BandagedCubit[] cubits, float ratio, float fov)
211
    {
212
    mCubits = cubits;
213
    mNumCubits = cubits.length;
214
    mPoint = new float[3];
215
    mCamera= new float[3];
216
    mTouch = new float[3];
217
    mPos   = new float[3];
218
    mPoint2D = new float[2];
219
    mFaceAxis = TouchControlHexahedron.FACE_AXIS;
220
    mObjectRatio = ratio;
221

  
222
    double halfFOV = fov * (Math.PI/360);
223
    float tanHalf = (float)Math.tan(halfFOV);
224
    float dist = 0.5f/tanHalf;
225

  
226
    CAMERA_POINT = new Static4D(0,0,dist,0);
227
    }
228

  
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

  
231
  public void setObjectRatio(float ratio)
232
    {
233
    mObjectRatio = ratio;
234
    }
235

  
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
// return the index of the cubit touched; if none, return -1.
238

  
239
  public int cubitTouched(float x, float y, Static4D quat)
240
    {
241
    Static4D touchPoint = new Static4D(x, y, 0, 0);
242
    Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, quat);
243
    Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, quat);
244

  
245
    boolean touched = objectTouched(rotatedTouchPoint,rotatedCamera);
246

  
247
    if( !touched ) return -1;
248

  
249
    return whichCubitTouched(mLastTouchedFace,mPoint2D[0],mPoint2D[1]);
250
    }
251

  
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

  
254
  public void markCubit(int index, int color)
255
    {
256
    mCubits[index].setTexture(color);
257
    }
258
}
259

  

Also available in: Unified diff