Revision f16ff19d
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/object/RubikObject.java | ||
---|---|---|
31 | 31 |
import org.distorted.library.main.DistortedTexture; |
32 | 32 |
import org.distorted.library.mesh.MeshRectangles; |
33 | 33 |
import org.distorted.library.message.EffectListener; |
34 |
import org.distorted.library.type.Dynamic1D; |
|
34 | 35 |
import org.distorted.library.type.Static1D; |
35 | 36 |
import org.distorted.library.type.Static3D; |
36 | 37 |
import org.distorted.library.type.Static4D; |
38 |
import org.distorted.magic.RubikSurfaceView; |
|
37 | 39 |
|
38 | 40 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
39 | 41 |
|
40 | 42 |
public abstract class RubikObject extends DistortedNode |
41 | 43 |
{ |
42 | 44 |
static final float OBJECT_SCREEN_RATIO = 0.5f; |
43 |
static final int POST_ROTATION_MILLISEC = 500; |
|
44 | 45 |
static final int TEXTURE_SIZE = 100; |
45 | 46 |
|
47 |
private static final int POST_ROTATION_MILLISEC = 500; |
|
48 |
private final float[] LEGAL_QUATS; |
|
49 |
|
|
46 | 50 |
private Static3D mMove, mScale, mNodeMove, mNodeScale; |
47 | 51 |
private Static4D mQuatAccumulated; |
48 | 52 |
private DistortedTexture mNodeTexture; |
... | ... | |
64 | 68 |
{ |
65 | 69 |
super(texture,effects,mesh); |
66 | 70 |
|
71 |
LEGAL_QUATS = getLegalQuats(); |
|
67 | 72 |
mNodeTexture = texture; |
68 |
|
|
69 | 73 |
mSize = size; |
70 | 74 |
|
71 | 75 |
mRotationAngleStatic = new Static1D(0); |
... | ... | |
96 | 100 |
effects.apply(nodeMoveEffect); |
97 | 101 |
} |
98 | 102 |
|
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 |
|
|
99 | 166 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
100 | 167 |
|
101 | 168 |
int computeNearestAngle(float angle) |
... | ... | |
108 | 175 |
return NEAREST*tmp; |
109 | 176 |
} |
110 | 177 |
|
178 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
179 |
|
|
180 |
void resetRotationAngle(Dynamic1D rotationAngle) |
|
181 |
{ |
|
182 |
rotationAngle.setDuration(POST_ROTATION_MILLISEC); |
|
183 |
rotationAngle.resetToBeginning(); |
|
184 |
rotationAngle.removeAll(); |
|
185 |
rotationAngle.add(mRotationAngleStatic); |
|
186 |
rotationAngle.add(mRotationAngleMiddle); |
|
187 |
rotationAngle.add(mRotationAngleFinal); |
|
188 |
} |
|
189 |
|
|
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 |
|
|
111 | 213 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
112 | 214 |
|
113 | 215 |
private float getSinkStrength() |
... | ... | |
173 | 275 |
|
174 | 276 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
175 | 277 |
|
278 |
abstract float[] getLegalQuats(); |
|
279 |
|
|
176 | 280 |
public abstract void savePreferences(SharedPreferences.Editor editor); |
177 | 281 |
public abstract void restorePreferences(SharedPreferences preferences); |
178 | 282 |
|
Also available in: Unified diff
Separate RubikCubit inner class.