Project

General

Profile

« Previous | Next » 

Revision 37a25788

Added by Leszek Koltunski about 4 years ago

Progress with object Movement.

View differences:

src/main/java/org/distorted/object/RubikObjectMovement.java
20 20
package org.distorted.object;
21 21

  
22 22
import org.distorted.library.type.Static2D;
23
import org.distorted.library.type.Static3D;
23 24
import org.distorted.library.type.Static4D;
24 25

  
25 26
///////////////////////////////////////////////////////////////////////////////////////////////////
26 27

  
27 28
public abstract class RubikObjectMovement
28 29
  {
29
  float[] mPoint, mCamera, mTouch;
30
  float[] mTouch;
30 31
  int mRotationVect, mLastTouchedAxis;
31 32

  
32
  private float[] mDiff;
33
  private float[] mPoint, mCamera, mDiff;
33 34
  private int mLastTouchedLR;
34 35
  private int mNumAxis, mNumFacesPerAxis;
35 36
  private int[] mPossible;
37
  private float mDistanceCenterFace;
38
  private Static3D[] mAxis;
36 39

  
37 40
///////////////////////////////////////////////////////////////////////////////////////////////////
38 41

  
39
  abstract boolean faceIsVisible(int axis, int lr);
40
  abstract void castTouchPointOntoFace(int axis, int lr, float[] output);
41 42
  abstract boolean isInsideFace(float[] point);
42
  abstract void fillPossibleRotations(int axis, int[] output);
43 43
  abstract float fillUpRotationVectAndOffset(float[] vect, int[] possible);
44 44
  abstract float returnAngle(float[] vect, int[] possible);
45
  abstract void fillPossibleRotations(int axis, int[] output);
45 46

  
46 47
///////////////////////////////////////////////////////////////////////////////////////////////////
47 48

  
48
  RubikObjectMovement(int numAxis, int numFacesPerAxis)
49
  RubikObjectMovement(Static3D[] axis, int numFacesPerAxis, float distance)
49 50
    {
50 51
    mPoint = new float[3];
51 52
    mCamera= new float[3];
52 53
    mDiff  = new float[3];
53 54
    mTouch = new float[3];
54 55

  
55
    mNumAxis = numAxis;
56
    mAxis = axis;
57
    mNumAxis = mAxis.length;
56 58
    mNumFacesPerAxis = numFacesPerAxis;
59
    mDistanceCenterFace = distance;
57 60
    mPossible = new int[mNumAxis-1];
58 61
    }
59 62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

  
65
  private boolean faceIsVisible(Static3D axis, int lr)
66
    {
67
    float castCameraOnAxis = mCamera[0]*axis.get0() + mCamera[1]*axis.get1() + mCamera[2]*axis.get2();
68
    return (2*lr-1)*castCameraOnAxis > mDistanceCenterFace;
69
    }
70

  
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
73
// compute point 'output[]' which:
74
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0)) [and this
75
//    distance is +-mDistanceCenterFace, depending if it is the face on the left or the right end of
76
//    the axis] (lr=0 or 1, so (2lr-1)*mDistanceCenterFace)
77
// 2) is co-linear with mCamera and mPoint
78
//
79
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
80

  
81
  private void castTouchPointOntoFace(Static3D axis, int lr, float[] output)
82
    {
83
    float d0 = mPoint[0]-mCamera[0];
84
    float d1 = mPoint[1]-mCamera[1];
85
    float d2 = mPoint[2]-mCamera[2];
86
    float a0 = axis.get0();
87
    float a1 = axis.get1();
88
    float a2 = axis.get2();
89

  
90
    float denom = a0*d0 + a1*d1 + a2*d2;
91

  
92
    if( denom != 0.0f )
93
      {
94
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
95
      float distance = (2*lr-1)*mDistanceCenterFace;
96
      float alpha = (distance-axisCam)/denom;
97

  
98
      output[0] = mCamera[0] + d0*alpha;
99
      output[1] = mCamera[1] + d1*alpha;
100
      output[2] = mCamera[2] + d2*alpha;
101
      }
102
    }
103

  
60 104
///////////////////////////////////////////////////////////////////////////////////////////////////
61 105
// PUBLIC API
62 106
///////////////////////////////////////////////////////////////////////////////////////////////////
......
75 119
      {
76 120
      for( mLastTouchedLR=0; mLastTouchedLR<mNumFacesPerAxis; mLastTouchedLR++)
77 121
        {
78
        if( faceIsVisible(mLastTouchedAxis, mLastTouchedLR) )
122
        if( faceIsVisible(mAxis[mLastTouchedAxis], mLastTouchedLR) )
79 123
          {
80
          castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mTouch);
124
          castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mTouch);
81 125

  
82 126
          if( isInsideFace(mTouch) )
83 127
            {
......
99 143
    mPoint[1] = rotatedTouchPoint.get1()/RubikObject.OBJECT_SCREEN_RATIO;
100 144
    mPoint[2] = rotatedTouchPoint.get2()/RubikObject.OBJECT_SCREEN_RATIO;
101 145

  
102
    castTouchPointOntoFace(mLastTouchedAxis, mLastTouchedLR, mDiff);
146
    castTouchPointOntoFace(mAxis[mLastTouchedAxis], mLastTouchedLR, mDiff);
103 147

  
104 148
    mDiff[0] -= mTouch[0];
105 149
    mDiff[1] -= mTouch[1];

Also available in: Unified diff