Revision bef47287
Added by Leszek Koltunski over 5 years ago
| src/main/java/org/distorted/magic/RubikSurfaceView.java | ||
|---|---|---|
| 220 | 220 |
} |
| 221 | 221 |
} |
| 222 | 222 |
|
| 223 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 224 |
|
|
| 225 |
private void setUpDragOrRotate(float x, float y) |
|
| 226 |
{
|
|
| 227 |
Static4D touchPoint1 = new Static4D(x, y, 0, 0); |
|
| 228 |
Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated); |
|
| 229 |
Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated); |
|
| 230 |
|
|
| 231 |
if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) ) |
|
| 232 |
{
|
|
| 233 |
mDragging = false; |
|
| 234 |
mBeginningRotation = mRenderer.canRotate(); |
|
| 235 |
mContinuingRotation = false; |
|
| 236 |
} |
|
| 237 |
else |
|
| 238 |
{
|
|
| 239 |
mDragging = mRenderer.canDrag(); |
|
| 240 |
mBeginningRotation = false; |
|
| 241 |
mContinuingRotation = false; |
|
| 242 |
} |
|
| 243 |
} |
|
| 244 |
|
|
| 223 | 245 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 224 | 246 |
|
| 225 | 247 |
@Override |
| ... | ... | |
| 233 | 255 |
{
|
| 234 | 256 |
case MotionEvent.ACTION_DOWN: mX = x; |
| 235 | 257 |
mY = y; |
| 236 |
|
|
| 237 |
Static4D touchPoint1 = new Static4D(x, y, 0, 0); |
|
| 238 |
Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated); |
|
| 239 |
Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated); |
|
| 240 |
|
|
| 241 |
if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) ) |
|
| 242 |
{
|
|
| 243 |
mDragging = false; |
|
| 244 |
mBeginningRotation = mRenderer.canRotate(); |
|
| 245 |
mContinuingRotation = false; |
|
| 246 |
} |
|
| 247 |
else |
|
| 248 |
{
|
|
| 249 |
mDragging = mRenderer.canDrag(); |
|
| 250 |
mBeginningRotation = false; |
|
| 251 |
mContinuingRotation = false; |
|
| 252 |
} |
|
| 258 |
setUpDragOrRotate(x,y); |
|
| 253 | 259 |
break; |
| 254 | 260 |
case MotionEvent.ACTION_MOVE: if( mBeginningRotation ) |
| 255 | 261 |
{
|
| ... | ... | |
| 281 | 287 |
float angle = mMovement.continueRotation(rotatedTouchPoint3); |
| 282 | 288 |
mRenderer.getObject().continueRotation(SWIPING_SENSITIVITY*angle); |
| 283 | 289 |
} |
| 284 |
else if( mDragging || mRenderer.canDrag() )
|
|
| 290 |
else if( mDragging ) |
|
| 285 | 291 |
{
|
| 286 |
mDragging = true; |
|
| 287 | 292 |
mTempCurrent.set(quatFromDrag(mX-x,y-mY)); |
| 288 | 293 |
mRenderer.setQuatCurrentOnNextRender(); |
| 289 | 294 |
|
| ... | ... | |
| 297 | 302 |
mRenderer.setQuatAccumulatedOnNextRender(); |
| 298 | 303 |
} |
| 299 | 304 |
} |
| 305 |
else |
|
| 306 |
{
|
|
| 307 |
setUpDragOrRotate(x,y); |
|
| 308 |
} |
|
| 300 | 309 |
break; |
| 301 | 310 |
case MotionEvent.ACTION_UP : if( mDragging ) |
| 302 | 311 |
{
|
| src/main/java/org/distorted/object/RubikCubeMovement.java | ||
|---|---|---|
| 51 | 51 |
|
| 52 | 52 |
boolean isInsideFace(float[] p) |
| 53 | 53 |
{
|
| 54 |
return ( p[0]<=0.5f && p[0]>=-0.5f && p[1]<=0.5f && p[1]>=-0.5f && p[2]<=0.5f && p[2]>=-0.5f );
|
|
| 54 |
return ( p[0]<=0.5f && p[0]>=-0.5f && p[1]<=0.5f && p[1]>=-0.5f ); |
|
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 | 57 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| src/main/java/org/distorted/object/RubikObjectMovement.java | ||
|---|---|---|
| 30 | 30 |
float[] mTouch; |
| 31 | 31 |
int mRotationVect, mLastTouchedAxis; |
| 32 | 32 |
|
| 33 |
private float[] mPoint, mCamera, mDiff; |
|
| 33 |
private float[] mPoint, mCamera, mDiff, m2Dpoint;
|
|
| 34 | 34 |
private int mLastTouchedLR; |
| 35 | 35 |
private int mNumAxis, mNumFacesPerAxis; |
| 36 | 36 |
private int[] mPossible; |
| ... | ... | |
| 53 | 53 |
mDiff = new float[3]; |
| 54 | 54 |
mTouch = new float[3]; |
| 55 | 55 |
|
| 56 |
m2Dpoint = new float[2]; |
|
| 57 |
|
|
| 56 | 58 |
mAxis = axis; |
| 57 | 59 |
mNumAxis = mAxis.length; |
| 58 | 60 |
mNumFacesPerAxis = numFacesPerAxis; |
| ... | ... | |
| 101 | 103 |
} |
| 102 | 104 |
} |
| 103 | 105 |
|
| 106 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 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 |
|
|
| 104 | 160 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 105 | 161 |
// PUBLIC API |
| 106 | 162 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 122 | 178 |
if( faceIsVisible(mAxis[mLastTouchedAxis], mLastTouchedLR) ) |
| 123 | 179 |
{
|
| 124 | 180 |
castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mTouch); |
| 181 |
convertTo2Dcoords(mTouch, mAxis[mLastTouchedAxis], mLastTouchedLR, m2Dpoint); |
|
| 125 | 182 |
|
| 126 |
if( isInsideFace(mTouch) )
|
|
| 183 |
if( isInsideFace(m2Dpoint) )
|
|
| 127 | 184 |
{
|
| 185 |
// android.util.Log.e("move", "touched "+mLastTouchedAxis+" touch point: ("+m2Dpoint[0]+","+m2Dpoint[1]+")");
|
|
| 186 |
|
|
| 128 | 187 |
fillPossibleRotations(mLastTouchedAxis, mPossible); |
| 129 | 188 |
return true; |
| 130 | 189 |
} |
| ... | ... | |
| 150 | 209 |
mDiff[2] -= mTouch[2]; |
| 151 | 210 |
|
| 152 | 211 |
float offset = fillUpRotationVectAndOffset(mDiff, mPossible); |
| 153 |
|
|
| 154 |
mTouch[0] = mPoint[0]; |
|
| 155 |
mTouch[1] = mPoint[1]; |
|
| 156 |
mTouch[2] = mPoint[2]; |
|
| 157 |
|
|
| 158 | 212 |
return new Static2D(mRotationVect,offset); |
| 159 | 213 |
} |
| 160 | 214 |
|
| ... | ... | |
| 162 | 216 |
|
| 163 | 217 |
public float continueRotation(Static4D rotatedTouchPoint) |
| 164 | 218 |
{
|
| 165 |
mDiff[0] = rotatedTouchPoint.get0()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[0];
|
|
| 166 |
mDiff[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[1];
|
|
| 167 |
mDiff[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO - mTouch[2];
|
|
| 219 |
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];
|
|
| 168 | 222 |
|
| 169 | 223 |
return (mLastTouchedLR-0.5f)*returnAngle(mDiff, mPossible); |
| 170 | 224 |
} |
| src/main/java/org/distorted/object/RubikPyraminxMovement.java | ||
|---|---|---|
| 55 | 55 |
|
| 56 | 56 |
boolean isInsideFace(float[] p) |
| 57 | 57 |
{
|
| 58 |
return false;//( p[0]<=0.5f && p[0]>=-0.5f && p[1]<=0.5f && p[1]>=-0.5f && p[2]<=0.5f && p[2]>=-0.5f ); |
|
| 58 |
boolean a1 = p[1] >= -SQ3/6; |
|
| 59 |
boolean a2 = p[1]*SQ3 <= 1 + 2*p[0]; |
|
| 60 |
boolean a3 = p[1]*SQ3 <= 1 - 2*p[0]; |
|
| 61 |
|
|
| 62 |
return a1 && a2 && a3; |
|
| 59 | 63 |
} |
| 60 | 64 |
|
| 61 | 65 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Progress with object Movement.