Revision 70b76549
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/object/RubikObject.java | ||
---|---|---|
35 | 35 |
import org.distorted.library.type.Static1D; |
36 | 36 |
import org.distorted.library.type.Static3D; |
37 | 37 |
import org.distorted.library.type.Static4D; |
38 |
import org.distorted.magic.RubikSurfaceView; |
|
39 | 38 |
|
40 | 39 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
41 | 40 |
|
... | ... | |
45 | 44 |
static final int TEXTURE_SIZE = 100; |
46 | 45 |
|
47 | 46 |
private static final int POST_ROTATION_MILLISEC = 500; |
48 |
private final float[] LEGAL_QUATS;
|
|
47 |
final float[] LEGAL_QUATS; |
|
49 | 48 |
|
50 | 49 |
private Static3D mMove, mScale, mNodeMove, mNodeScale; |
51 | 50 |
private Static4D mQuatAccumulated; |
... | ... | |
100 | 99 |
effects.apply(nodeMoveEffect); |
101 | 100 |
} |
102 | 101 |
|
103 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
104 |
// Because of quatMultiplication, errors can accumulate - so to avoid this, we |
|
105 |
// correct the value of the 'scramble' quat to what it should be - one of the legal quats from the |
|
106 |
// list LEGAL_QUATS. |
|
107 |
// |
|
108 |
// We also have to remember that the group of unit quaternions is a double-cover of rotations |
|
109 |
// in 3D ( q represents the same rotation as -q ) - so invert if needed. |
|
110 |
|
|
111 |
void normalizeScrambleQuat(Static4D quat) |
|
112 |
{ |
|
113 |
float x = quat.get0(); |
|
114 |
float y = quat.get1(); |
|
115 |
float z = quat.get2(); |
|
116 |
float w = quat.get3(); |
|
117 |
float diff; |
|
118 |
|
|
119 |
for(float legal: LEGAL_QUATS) |
|
120 |
{ |
|
121 |
diff = x-legal; |
|
122 |
if( diff*diff<0.01f ) x = legal; |
|
123 |
diff = y-legal; |
|
124 |
if( diff*diff<0.01f ) y = legal; |
|
125 |
diff = z-legal; |
|
126 |
if( diff*diff<0.01f ) z = legal; |
|
127 |
diff = w-legal; |
|
128 |
if( diff*diff<0.01f ) w = legal; |
|
129 |
} |
|
130 |
|
|
131 |
if( w<0 ) |
|
132 |
{ |
|
133 |
w = -w; |
|
134 |
z = -z; |
|
135 |
y = -y; |
|
136 |
x = -x; |
|
137 |
} |
|
138 |
else if( w==0 ) |
|
139 |
{ |
|
140 |
if( z<0 ) |
|
141 |
{ |
|
142 |
z = -z; |
|
143 |
y = -y; |
|
144 |
x = -x; |
|
145 |
} |
|
146 |
else if( z==0 ) |
|
147 |
{ |
|
148 |
if( y<0 ) |
|
149 |
{ |
|
150 |
y = -y; |
|
151 |
x = -x; |
|
152 |
} |
|
153 |
else if( y==0 ) |
|
154 |
{ |
|
155 |
if( x<0 ) |
|
156 |
{ |
|
157 |
x = -x; |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
|
|
163 |
quat.set(x,y,z,w); |
|
164 |
} |
|
165 |
|
|
166 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
167 |
|
|
168 |
int computeNearestAngle(float angle) |
|
169 |
{ |
|
170 |
final int NEAREST = 90; |
|
171 |
|
|
172 |
int tmp = (int)((angle+NEAREST/2)/NEAREST); |
|
173 |
if( angle< -(NEAREST*0.5) ) tmp-=1; |
|
174 |
|
|
175 |
return NEAREST*tmp; |
|
176 |
} |
|
177 |
|
|
178 | 102 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
179 | 103 |
|
180 | 104 |
void resetRotationAngle(Dynamic1D rotationAngle) |
... | ... | |
187 | 111 |
rotationAngle.add(mRotationAngleFinal); |
188 | 112 |
} |
189 | 113 |
|
190 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
191 |
|
|
192 |
void modifyCurrentPosition(Static3D currentPosition, Static4D quat) |
|
193 |
{ |
|
194 |
float diff = 0.5f*(mSize-1); |
|
195 |
float cubitCenterX = currentPosition.get0() - diff; |
|
196 |
float cubitCenterY = currentPosition.get1() - diff; |
|
197 |
float cubitCenterZ = currentPosition.get2() - diff; |
|
198 |
|
|
199 |
Static4D cubitCenter = new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0); |
|
200 |
Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat); |
|
201 |
|
|
202 |
float rotatedX = rotatedCenter.get0() + diff; |
|
203 |
float rotatedY = rotatedCenter.get1() + diff; |
|
204 |
float rotatedZ = rotatedCenter.get2() + diff; |
|
205 |
|
|
206 |
int roundedX = (int)(rotatedX+0.1f); |
|
207 |
int roundedY = (int)(rotatedY+0.1f); |
|
208 |
int roundedZ = (int)(rotatedZ+0.1f); |
|
209 |
|
|
210 |
currentPosition.set(roundedX, roundedY, roundedZ); |
|
211 |
} |
|
212 |
|
|
213 | 114 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
214 | 115 |
|
215 | 116 |
private float getSinkStrength() |
Also available in: Unified diff
More work on Cubit.