Revision a10ada2a
Added by Leszek Koltunski over 5 years ago
| src/main/java/org/distorted/object/Cubit.java | ||
|---|---|---|
| 159 | 159 |
float y = position.get1(); |
| 160 | 160 |
float z = position.get2(); |
| 161 | 161 |
|
| 162 |
float nc = mParent.mSize*0.5f;
|
|
| 162 |
float nc = parent.mSize*0.5f;
|
|
| 163 | 163 |
int TS = RubikObject.TEXTURE_SIZE; |
| 164 | 164 |
Static3D vector = new Static3D(TS*(x-nc), TS*(y-nc), TS*(z-nc)); |
| 165 | 165 |
|
| src/main/java/org/distorted/object/RubikCube.java | ||
|---|---|---|
| 19 | 19 |
|
| 20 | 20 |
package org.distorted.object; |
| 21 | 21 |
|
| 22 |
import android.content.SharedPreferences; |
|
| 23 | 22 |
import android.graphics.Bitmap; |
| 24 | 23 |
import android.graphics.Canvas; |
| 25 | 24 |
import android.graphics.Paint; |
| 26 | 25 |
|
| 27 |
import org.distorted.library.effect.Effect; |
|
| 28 | 26 |
import org.distorted.library.main.DistortedEffects; |
| 29 | 27 |
import org.distorted.library.main.DistortedTexture; |
| 30 | 28 |
import org.distorted.library.mesh.MeshCubes; |
| 31 | 29 |
import org.distorted.library.mesh.MeshRectangles; |
| 32 |
import org.distorted.library.message.EffectListener; |
|
| 33 | 30 |
import org.distorted.library.type.Static3D; |
| 34 | 31 |
import org.distorted.library.type.Static4D; |
| 35 | 32 |
|
| ... | ... | |
| 41 | 38 |
|
| 42 | 39 |
class RubikCube extends RubikObject |
| 43 | 40 |
{
|
| 44 |
private static final Static3D VectX = new Static3D(1,0,0); |
|
| 45 |
private static final Static3D VectY = new Static3D(0,1,0); |
|
| 46 |
private static final Static3D VectZ = new Static3D(0,0,1); |
|
| 47 |
private static final Static4D mapFront, mapBack, mapLeft, mapRight, mapTop, mapBottom, mapBlack; |
|
| 41 |
private static final Static4D mapFront, mapBack, mapLeft, mapRight, mapTop, mapBottom, mapBlack; |
|
| 48 | 42 |
|
| 49 |
private Cubit[][][] mCubits; |
|
| 43 |
static |
|
| 44 |
{
|
|
| 45 |
// 3x2 bitmap = 6 squares: |
|
| 46 |
// |
|
| 47 |
// RED GREEN BLUE |
|
| 48 |
// YELLOW WHITE BROWN |
|
| 50 | 49 |
|
| 51 |
static |
|
| 52 |
{
|
|
| 53 |
// 3x2 bitmap = 6 squares: |
|
| 54 |
// |
|
| 55 |
// RED GREEN BLUE |
|
| 56 |
// YELLOW WHITE BROWN |
|
| 57 |
|
|
| 58 |
final float ze = 0.0f; |
|
| 59 |
final float ot = 1.0f/3.0f; |
|
| 60 |
final float tt = 2.0f/3.0f; |
|
| 61 |
final float oh = 1.0f/2.0f; |
|
| 62 |
final float of = 1.0f/40.0f; |
|
| 63 |
|
|
| 64 |
mapFront = new Static4D(ze,oh, ze+ot,oh+oh); |
|
| 65 |
mapBack = new Static4D(tt,ze, tt+ot,ze+oh); |
|
| 66 |
mapLeft = new Static4D(ot,ze, ot+ot,ze+oh); |
|
| 67 |
mapRight = new Static4D(ze,ze, ze+ot,ze+oh); |
|
| 68 |
mapTop = new Static4D(tt,oh, tt+ot,oh+oh); |
|
| 69 |
mapBottom= new Static4D(ot,oh, ot+ot,oh+oh); |
|
| 70 |
|
|
| 71 |
mapBlack = new Static4D(ze,ze, ze+of,ze+of); |
|
| 72 |
} |
|
| 50 |
final float ze = 0.0f; |
|
| 51 |
final float ot = 1.0f/3.0f; |
|
| 52 |
final float tt = 2.0f/3.0f; |
|
| 53 |
final float oh = 1.0f/2.0f; |
|
| 54 |
final float of = 1.0f/40.0f; |
|
| 73 | 55 |
|
| 74 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 75 |
// All legal rotation quats of a RubikCube of any size must have all four of their components |
|
| 76 |
// equal to either 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2. |
|
| 56 |
mapFront = new Static4D(ze,oh, ze+ot,oh+oh); |
|
| 57 |
mapBack = new Static4D(tt,ze, tt+ot,ze+oh); |
|
| 58 |
mapLeft = new Static4D(ot,ze, ot+ot,ze+oh); |
|
| 59 |
mapRight = new Static4D(ze,ze, ze+ot,ze+oh); |
|
| 60 |
mapTop = new Static4D(tt,oh, tt+ot,oh+oh); |
|
| 61 |
mapBottom= new Static4D(ot,oh, ot+ot,oh+oh); |
|
| 77 | 62 |
|
| 78 |
float[] getLegalQuats() |
|
| 79 |
{
|
|
| 80 |
final float SQ2 = 0.5f*((float)Math.sqrt(2)); |
|
| 81 |
return new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
|
|
| 82 |
} |
|
| 63 |
mapBlack = new Static4D(ze,ze, ze+of,ze+of); |
|
| 64 |
} |
|
| 83 | 65 |
|
| 84 | 66 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 67 |
// i.e. size^3 - (size-2)^3 - number of cubits in the outside wall of the Cube (we dont create or |
|
| 68 |
// render the inside0 |
|
| 85 | 69 |
|
| 86 |
private boolean belongsToRotation( Static3D currentPosition, int vector, int row) |
|
| 87 |
{
|
|
| 88 |
switch(vector) |
|
| 89 |
{
|
|
| 90 |
case VECTX: return currentPosition.get0()==row; |
|
| 91 |
case VECTY: return currentPosition.get1()==row; |
|
| 92 |
case VECTZ: return currentPosition.get2()==row; |
|
| 93 |
} |
|
| 94 |
|
|
| 95 |
return false; |
|
| 96 |
} |
|
| 70 |
int getNumCubits(int size) |
|
| 71 |
{
|
|
| 72 |
return 6*size*size - 12*size + 8; |
|
| 73 |
} |
|
| 97 | 74 |
|
| 98 | 75 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 99 | 76 |
|
| 100 |
RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects) |
|
| 101 |
{
|
|
| 102 |
super(size,quatCur,quatAcc,texture,mesh,effects); |
|
| 103 |
|
|
| 104 |
mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE); |
|
| 105 |
mCubits = new Cubit[mSize][mSize][mSize]; |
|
| 77 |
int[][] getCubitPositions(int size) |
|
| 78 |
{
|
|
| 79 |
int[][] tmp = new int[getNumCubits(size)][3]; |
|
| 106 | 80 |
|
| 107 |
int vertices = (int)(24.0f/mSize + 2.0f);
|
|
| 81 |
int currentPosition = 0;
|
|
| 108 | 82 |
|
| 109 |
for(int x = 0; x< mSize; x++) |
|
| 110 |
for(int y = 0; y< mSize; y++) |
|
| 111 |
for(int z = 0; z< mSize; z++) |
|
| 83 |
for(int x = 0; x<size; x++) |
|
| 84 |
for(int y = 0; y<size; y++) |
|
| 85 |
for(int z = 0; z<size; z++) |
|
| 86 |
{
|
|
| 87 |
if( x==0 || x==size-1 || y==0 || y==size-1 || z==0 || z==size-1 ) |
|
| 112 | 88 |
{
|
| 113 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 114 |
{
|
|
| 115 |
mCubits[x][y][z] = new Cubit( this,createMesh(vertices,x,y,z),new Static3D(x,y,z) ); |
|
| 116 |
attach(mCubits[x][y][z].mNode); |
|
| 117 |
} |
|
| 118 |
} |
|
| 119 |
} |
|
| 120 |
|
|
| 121 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 122 |
// PUBLIC API |
|
| 123 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 124 |
// mSize already saved as RubikStatePlay.mButton |
|
| 125 |
|
|
| 126 |
public void savePreferences(SharedPreferences.Editor editor) |
|
| 127 |
{
|
|
| 128 |
for(int x=0; x<mSize; x++) |
|
| 129 |
for(int y=0; y<mSize; y++) |
|
| 130 |
for(int z=0; z<mSize; z++) |
|
| 131 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 132 |
{
|
|
| 133 |
mCubits[x][y][z].savePreferences(editor); |
|
| 134 |
} |
|
| 135 |
} |
|
| 136 |
|
|
| 137 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 138 |
|
|
| 139 |
public void restorePreferences(SharedPreferences preferences) |
|
| 140 |
{
|
|
| 141 |
for(int x=0; x<mSize; x++) |
|
| 142 |
for(int y=0; y<mSize; y++) |
|
| 143 |
for(int z=0; z<mSize; z++) |
|
| 144 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 145 |
{
|
|
| 146 |
mCubits[x][y][z].restorePreferences(preferences); |
|
| 147 |
} |
|
| 148 |
} |
|
| 149 |
|
|
| 150 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 151 |
|
|
| 152 |
public long finishRotationNow(EffectListener listener) |
|
| 153 |
{
|
|
| 154 |
boolean first = true; |
|
| 155 |
long effectID=0; |
|
| 156 |
|
|
| 157 |
for(int x=0; x<mSize; x++) |
|
| 158 |
for(int y=0; y<mSize; y++) |
|
| 159 |
for(int z=0; z<mSize; z++) |
|
| 160 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 161 |
{
|
|
| 162 |
if( belongsToRotation(mCubits[x][y][z].mCurrentPosition,mRotAxis,mRotRow) ) |
|
| 163 |
{
|
|
| 164 |
if( first ) |
|
| 165 |
{
|
|
| 166 |
first = false; |
|
| 167 |
effectID = mCubits[x][y][z].finishRotationNow(listener); |
|
| 168 |
} |
|
| 169 |
|
|
| 170 |
resetRotationAngle(mCubits[x][y][z].mRotationAngle); |
|
| 171 |
} |
|
| 172 |
} |
|
| 173 |
|
|
| 174 |
return effectID; |
|
| 175 |
} |
|
| 89 |
tmp[currentPosition][0] = x; |
|
| 90 |
tmp[currentPosition][1] = y; |
|
| 91 |
tmp[currentPosition][2] = z; |
|
| 176 | 92 |
|
| 177 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 178 |
|
|
| 179 |
public void releaseResources() |
|
| 180 |
{
|
|
| 181 |
mTexture.markForDeletion(); |
|
| 182 |
|
|
| 183 |
for(int x=0; x<mSize; x++) |
|
| 184 |
for(int y=0; y<mSize; y++) |
|
| 185 |
for(int z=0; z<mSize; z++) |
|
| 186 |
{
|
|
| 187 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 188 |
{
|
|
| 189 |
mCubits[x][y][z].releaseResources(); |
|
| 190 |
} |
|
| 191 |
} |
|
| 192 |
} |
|
| 193 |
|
|
| 194 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 195 |
|
|
| 196 |
public void apply(Effect effect, int position) |
|
| 197 |
{
|
|
| 198 |
for(int x=0; x<mSize; x++) |
|
| 199 |
for(int y=0; y<mSize; y++) |
|
| 200 |
for(int z=0; z<mSize; z++) |
|
| 201 |
{
|
|
| 202 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 203 |
{
|
|
| 204 |
mCubits[x][y][z].mEffect.apply(effect, position); |
|
| 205 |
} |
|
| 206 |
} |
|
| 207 |
} |
|
| 208 |
|
|
| 209 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 210 |
|
|
| 211 |
public void remove(long effectID) |
|
| 212 |
{
|
|
| 213 |
for(int x=0; x<mSize; x++) |
|
| 214 |
for(int y=0; y<mSize; y++) |
|
| 215 |
for(int z=0; z<mSize; z++) |
|
| 216 |
{
|
|
| 217 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 218 |
{
|
|
| 219 |
mCubits[x][y][z].mEffect.abortById(effectID); |
|
| 220 |
} |
|
| 221 |
} |
|
| 222 |
} |
|
| 223 |
|
|
| 224 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 225 |
|
|
| 226 |
public void solve() |
|
| 227 |
{
|
|
| 228 |
for(int x=0; x<mSize; x++) |
|
| 229 |
for(int y=0; y<mSize; y++) |
|
| 230 |
for(int z=0; z<mSize; z++) |
|
| 231 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 232 |
{
|
|
| 233 |
mCubits[x][y][z].solve(); |
|
| 234 |
} |
|
| 235 |
} |
|
| 236 |
|
|
| 237 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 93 |
currentPosition++; |
|
| 94 |
} |
|
| 95 |
} |
|
| 238 | 96 |
|
| 239 |
public boolean isSolved() |
|
| 240 |
{
|
|
| 241 |
Static4D q = mCubits[0][0][0].mQuatScramble; |
|
| 242 |
|
|
| 243 |
float x = q.get0(); |
|
| 244 |
float y = q.get1(); |
|
| 245 |
float z = q.get2(); |
|
| 246 |
float w = q.get3(); |
|
| 247 |
|
|
| 248 |
for(int i = 0; i< mSize; i++) |
|
| 249 |
for(int j = 0; j< mSize; j++) |
|
| 250 |
for(int k = 0; k< mSize; k++) |
|
| 251 |
{
|
|
| 252 |
if( i==0 || i==mSize-1 || j==0 || j==mSize-1 || k==0 || k==mSize-1 ) |
|
| 253 |
{
|
|
| 254 |
q = mCubits[i][j][k].mQuatScramble; |
|
| 255 |
|
|
| 256 |
if( q.get0()!=x || q.get1()!=y || q.get2()!=z || q.get3()!=w ) |
|
| 257 |
{
|
|
| 258 |
return false; |
|
| 259 |
} |
|
| 260 |
} |
|
| 261 |
} |
|
| 262 |
|
|
| 263 |
return true; |
|
| 264 |
} |
|
| 97 |
return tmp; |
|
| 98 |
} |
|
| 265 | 99 |
|
| 266 | 100 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 101 |
// All legal rotation quats of a RubikCube of any size must have all four of their components |
|
| 102 |
// equal to either 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2. |
|
| 267 | 103 |
|
| 268 |
public void beginNewRotation(int vector, int row ) |
|
| 269 |
{
|
|
| 270 |
Static3D axis = VectX; |
|
| 271 |
|
|
| 272 |
switch(vector) |
|
| 273 |
{
|
|
| 274 |
case VECTX: axis = VectX; break; |
|
| 275 |
case VECTY: axis = VectY; break; |
|
| 276 |
case VECTZ: axis = VectZ; break; |
|
| 277 |
} |
|
| 278 |
|
|
| 279 |
mRotAxis = vector; |
|
| 280 |
mRotRow = row; |
|
| 281 |
|
|
| 282 |
mRotationAngleStatic.set0(0.0f); |
|
| 283 |
|
|
| 284 |
for(int x=0; x<mSize; x++) |
|
| 285 |
for(int y=0; y<mSize; y++) |
|
| 286 |
for(int z=0; z<mSize; z++) |
|
| 287 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 288 |
{
|
|
| 289 |
if( belongsToRotation( mCubits[x][y][z].mCurrentPosition,vector,mRotRow) ) |
|
| 290 |
{
|
|
| 291 |
mCubits[x][y][z].beginNewRotation(axis); |
|
| 292 |
} |
|
| 293 |
} |
|
| 294 |
} |
|
| 104 |
float[] getLegalQuats() |
|
| 105 |
{
|
|
| 106 |
final float SQ2 = 0.5f*((float)Math.sqrt(2)); |
|
| 107 |
return new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
|
|
| 108 |
} |
|
| 295 | 109 |
|
| 296 | 110 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 297 | 111 |
|
| 298 |
public long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener ) |
|
| 112 |
boolean belongsToRotation( Static3D currentPosition, int vector, int row) |
|
| 113 |
{
|
|
| 114 |
switch(vector) |
|
| 299 | 115 |
{
|
| 300 |
Static3D axis = VectX; |
|
| 301 |
long effectID=0; |
|
| 302 |
boolean first = true; |
|
| 303 |
|
|
| 304 |
switch(vector) |
|
| 305 |
{
|
|
| 306 |
case VECTX: axis = VectX; break; |
|
| 307 |
case VECTY: axis = VectY; break; |
|
| 308 |
case VECTZ: axis = VectZ; break; |
|
| 309 |
} |
|
| 310 |
|
|
| 311 |
mRotAxis = vector; |
|
| 312 |
mRotRow = row; |
|
| 313 |
|
|
| 314 |
mRotationAngleStatic.set0(0.0f); |
|
| 315 |
|
|
| 316 |
for(int x=0; x<mSize; x++) |
|
| 317 |
for(int y=0; y<mSize; y++) |
|
| 318 |
for(int z=0; z<mSize; z++) |
|
| 319 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 320 |
{
|
|
| 321 |
if( belongsToRotation(mCubits[x][y][z].mCurrentPosition,vector,mRotRow) ) |
|
| 322 |
{
|
|
| 323 |
mCubits[x][y][z].addNewRotation(axis,durationMillis,angle); |
|
| 324 |
|
|
| 325 |
if( first ) |
|
| 326 |
{
|
|
| 327 |
first = false; |
|
| 328 |
effectID = mCubits[x][y][z].setUpCallback(listener); |
|
| 329 |
} |
|
| 330 |
} |
|
| 331 |
} |
|
| 332 |
|
|
| 333 |
return effectID; |
|
| 116 |
case VECTX: return currentPosition.get0()==row; |
|
| 117 |
case VECTY: return currentPosition.get1()==row; |
|
| 118 |
case VECTZ: return currentPosition.get2()==row; |
|
| 334 | 119 |
} |
| 335 | 120 |
|
| 336 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 337 |
|
|
| 338 |
public void removeRotationNow() |
|
| 339 |
{
|
|
| 340 |
float qx=0,qy=0,qz=0; |
|
| 341 |
boolean first = true; |
|
| 342 |
Static4D quat = null; |
|
| 343 |
|
|
| 344 |
switch(mRotAxis) |
|
| 345 |
{
|
|
| 346 |
case VECTX: qx=1; break; |
|
| 347 |
case VECTY: qy=1; break; |
|
| 348 |
case VECTZ: qz=1; break; |
|
| 349 |
} |
|
| 350 |
|
|
| 351 |
for(int x=0; x<mSize; x++) |
|
| 352 |
for(int y=0; y<mSize; y++) |
|
| 353 |
for(int z=0; z<mSize; z++) |
|
| 354 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
| 355 |
{
|
|
| 356 |
if( belongsToRotation(mCubits[x][y][z].mCurrentPosition,mRotAxis,mRotRow) ) |
|
| 357 |
{
|
|
| 358 |
if( first ) |
|
| 359 |
{
|
|
| 360 |
first = false; |
|
| 361 |
quat = mCubits[x][y][z].returnRotationQuat(qx,qy,qz); |
|
| 362 |
} |
|
| 363 |
|
|
| 364 |
mCubits[x][y][z].removeRotationNow(quat); |
|
| 365 |
} |
|
| 366 |
} |
|
| 367 |
|
|
| 368 |
mRotationAngleStatic.set0(0); |
|
| 369 |
} |
|
| 121 |
return false; |
|
| 122 |
} |
|
| 370 | 123 |
|
| 371 | 124 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 372 | 125 |
|
| 373 |
public void createTexture() |
|
| 374 |
{
|
|
| 375 |
Bitmap bitmap; |
|
| 376 |
|
|
| 377 |
final int S = 128; |
|
| 378 |
final int W = 3*S; |
|
| 379 |
final int H = 2*S; |
|
| 380 |
final int R = S/10; |
|
| 381 |
final int M = S/20; |
|
| 382 |
|
|
| 383 |
Paint paint = new Paint(); |
|
| 384 |
bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888); |
|
| 385 |
Canvas canvas = new Canvas(bitmap); |
|
| 386 |
|
|
| 387 |
paint.setAntiAlias(true); |
|
| 388 |
paint.setTextAlign(Paint.Align.CENTER); |
|
| 389 |
paint.setStyle(Paint.Style.FILL); |
|
| 390 |
|
|
| 391 |
// 3x2 bitmap = 6 squares: |
|
| 392 |
// |
|
| 393 |
// RED GREEN BLUE |
|
| 394 |
// YELLOW WHITE BROWN |
|
| 395 |
|
|
| 396 |
paint.setColor(0xff000000); // BLACK BACKGROUND |
|
| 397 |
canvas.drawRect(0, 0, W, H, paint); // |
|
| 398 |
|
|
| 399 |
paint.setColor(0xffff0000); // RED |
|
| 400 |
canvas.drawRoundRect( M, M, S-M, S-M, R, R, paint); // |
|
| 401 |
paint.setColor(0xff00ff00); // GREEN |
|
| 402 |
canvas.drawRoundRect( S+M, M, 2*S-M, S-M, R, R, paint); // |
|
| 403 |
paint.setColor(0xff0000ff); // BLUE |
|
| 404 |
canvas.drawRoundRect(2*S+M, M, 3*S-M, S-M, R, R, paint); // |
|
| 405 |
paint.setColor(0xffffff00); // YELLOW |
|
| 406 |
canvas.drawRoundRect( M, S+M, S-M, 2*S-M, R, R, paint); // |
|
| 407 |
paint.setColor(0xffffffff); // WHITE |
|
| 408 |
canvas.drawRoundRect( S+M, S+M, 2*S-M, 2*S-M, R, R, paint); // |
|
| 409 |
paint.setColor(0xffb5651d); // BROWN |
|
| 410 |
canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); // |
|
| 411 |
|
|
| 412 |
mTexture.setTexture(bitmap); |
|
| 413 |
} |
|
| 126 |
RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects) |
|
| 127 |
{
|
|
| 128 |
super(size,quatCur,quatAcc,texture,mesh,effects); |
|
| 129 |
} |
|
| 414 | 130 |
|
| 415 | 131 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 132 |
// PUBLIC API |
|
| 416 | 133 |
|
| 417 |
private MeshCubes createMesh(int vertices,int x, int y, int z) |
|
| 418 |
{
|
|
| 419 |
Static4D tmpLeft = (x== 0 ? mapLeft :mapBlack); |
|
| 420 |
Static4D tmpRight = (x== mSize-1 ? mapRight :mapBlack); |
|
| 421 |
Static4D tmpFront = (z== mSize-1 ? mapFront :mapBlack); |
|
| 422 |
Static4D tmpBack = (z== 0 ? mapBack :mapBlack); |
|
| 423 |
Static4D tmpTop = (y== mSize-1 ? mapTop :mapBlack); |
|
| 424 |
Static4D tmpBottom= (y== 0 ? mapBottom:mapBlack); |
|
| 425 |
|
|
| 426 |
return new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom); |
|
| 427 |
} |
|
| 134 |
public void createTexture() |
|
| 135 |
{
|
|
| 136 |
Bitmap bitmap; |
|
| 137 |
|
|
| 138 |
final int S = 128; |
|
| 139 |
final int W = 3*S; |
|
| 140 |
final int H = 2*S; |
|
| 141 |
final int R = S/10; |
|
| 142 |
final int M = S/20; |
|
| 143 |
|
|
| 144 |
Paint paint = new Paint(); |
|
| 145 |
bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888); |
|
| 146 |
Canvas canvas = new Canvas(bitmap); |
|
| 147 |
|
|
| 148 |
paint.setAntiAlias(true); |
|
| 149 |
paint.setTextAlign(Paint.Align.CENTER); |
|
| 150 |
paint.setStyle(Paint.Style.FILL); |
|
| 151 |
|
|
| 152 |
// 3x2 bitmap = 6 squares: |
|
| 153 |
// |
|
| 154 |
// RED GREEN BLUE |
|
| 155 |
// YELLOW WHITE BROWN |
|
| 156 |
|
|
| 157 |
paint.setColor(0xff000000); // BLACK BACKGROUND |
|
| 158 |
canvas.drawRect(0, 0, W, H, paint); // |
|
| 159 |
|
|
| 160 |
paint.setColor(0xffff0000); // RED |
|
| 161 |
canvas.drawRoundRect( M, M, S-M, S-M, R, R, paint); // |
|
| 162 |
paint.setColor(0xff00ff00); // GREEN |
|
| 163 |
canvas.drawRoundRect( S+M, M, 2*S-M, S-M, R, R, paint); // |
|
| 164 |
paint.setColor(0xff0000ff); // BLUE |
|
| 165 |
canvas.drawRoundRect(2*S+M, M, 3*S-M, S-M, R, R, paint); // |
|
| 166 |
paint.setColor(0xffffff00); // YELLOW |
|
| 167 |
canvas.drawRoundRect( M, S+M, S-M, 2*S-M, R, R, paint); // |
|
| 168 |
paint.setColor(0xffffffff); // WHITE |
|
| 169 |
canvas.drawRoundRect( S+M, S+M, 2*S-M, 2*S-M, R, R, paint); // |
|
| 170 |
paint.setColor(0xffb5651d); // BROWN |
|
| 171 |
canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); // |
|
| 172 |
|
|
| 173 |
mTexture.setTexture(bitmap); |
|
| 174 |
} |
|
| 175 |
|
|
| 176 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 177 |
|
|
| 178 |
MeshCubes createMesh(int vertices,int x, int y, int z) |
|
| 179 |
{
|
|
| 180 |
Static4D tmpLeft = (x== 0 ? mapLeft :mapBlack); |
|
| 181 |
Static4D tmpRight = (x== mSize-1 ? mapRight :mapBlack); |
|
| 182 |
Static4D tmpFront = (z== mSize-1 ? mapFront :mapBlack); |
|
| 183 |
Static4D tmpBack = (z== 0 ? mapBack :mapBlack); |
|
| 184 |
Static4D tmpTop = (y== mSize-1 ? mapTop :mapBlack); |
|
| 185 |
Static4D tmpBottom= (y== 0 ? mapBottom:mapBlack); |
|
| 186 |
|
|
| 187 |
return new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom); |
|
| 188 |
} |
|
| 428 | 189 |
} |
| src/main/java/org/distorted/object/RubikObject.java | ||
|---|---|---|
| 29 | 29 |
import org.distorted.library.main.DistortedEffects; |
| 30 | 30 |
import org.distorted.library.main.DistortedNode; |
| 31 | 31 |
import org.distorted.library.main.DistortedTexture; |
| 32 |
import org.distorted.library.mesh.MeshCubes; |
|
| 32 | 33 |
import org.distorted.library.mesh.MeshRectangles; |
| 33 | 34 |
import org.distorted.library.message.EffectListener; |
| 34 | 35 |
import org.distorted.library.type.Dynamic1D; |
| ... | ... | |
| 36 | 37 |
import org.distorted.library.type.Static3D; |
| 37 | 38 |
import org.distorted.library.type.Static4D; |
| 38 | 39 |
|
| 40 |
import static org.distorted.object.RubikObjectList.*; |
|
| 41 |
|
|
| 39 | 42 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 40 | 43 |
|
| 41 | 44 |
public abstract class RubikObject extends DistortedNode |
| 42 | 45 |
{
|
| 43 | 46 |
static final float OBJECT_SCREEN_RATIO = 0.5f; |
| 44 | 47 |
static final int TEXTURE_SIZE = 100; |
| 48 |
final float[] LEGAL_QUATS; |
|
| 45 | 49 |
|
| 46 | 50 |
private static final int POST_ROTATION_MILLISEC = 500; |
| 47 |
final float[] LEGAL_QUATS; |
|
| 51 |
private final int NUM_CUBITS; |
|
| 52 |
private final int[][] CUBIT_POSITIONS; |
|
| 53 |
private int mRotAxis, mRotRow; |
|
| 48 | 54 |
|
| 49 | 55 |
private Static3D mMove, mScale, mNodeMove, mNodeScale; |
| 50 | 56 |
private Static4D mQuatAccumulated; |
| 51 | 57 |
private DistortedTexture mNodeTexture; |
| 52 | 58 |
|
| 53 |
int mSize, mRotAxis, mRotRow;
|
|
| 59 |
int mSize; |
|
| 54 | 60 |
|
| 55 | 61 |
Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal; |
| 56 | 62 |
DistortedTexture mTexture; |
| ... | ... | |
| 61 | 67 |
MatrixEffectQuaternion mQuatCEffect; |
| 62 | 68 |
MatrixEffectQuaternion mQuatAEffect; |
| 63 | 69 |
|
| 70 |
private Cubit[] mCubits; |
|
| 71 |
|
|
| 64 | 72 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 65 | 73 |
|
| 66 | 74 |
RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects) |
| ... | ... | |
| 68 | 76 |
super(texture,effects,mesh); |
| 69 | 77 |
|
| 70 | 78 |
LEGAL_QUATS = getLegalQuats(); |
| 79 |
NUM_CUBITS = getNumCubits(size); |
|
| 80 |
CUBIT_POSITIONS = getCubitPositions(size); |
|
| 81 |
|
|
| 71 | 82 |
mNodeTexture = texture; |
| 72 | 83 |
mSize = size; |
| 73 | 84 |
|
| ... | ... | |
| 97 | 108 |
|
| 98 | 109 |
effects.apply(nodeScaleEffect); |
| 99 | 110 |
effects.apply(nodeMoveEffect); |
| 111 |
|
|
| 112 |
|
|
| 113 |
mCubits = new Cubit[NUM_CUBITS]; |
|
| 114 |
|
|
| 115 |
mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE); |
|
| 116 |
|
|
| 117 |
int vertices = (int)(24.0f/mSize + 2.0f); |
|
| 118 |
|
|
| 119 |
for(int i=0; i<NUM_CUBITS; i++) |
|
| 120 |
{
|
|
| 121 |
int x = CUBIT_POSITIONS[i][0]; |
|
| 122 |
int y = CUBIT_POSITIONS[i][1]; |
|
| 123 |
int z = CUBIT_POSITIONS[i][2]; |
|
| 124 |
|
|
| 125 |
mCubits[i] = new Cubit( this ,createMesh(vertices,x,y,z), new Static3D(x,y,z) ); |
|
| 126 |
attach(mCubits[i].mNode); |
|
| 127 |
} |
|
| 100 | 128 |
} |
| 101 | 129 |
|
| 102 | 130 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 103 | 131 |
|
| 104 |
void resetRotationAngle(Dynamic1D rotationAngle) |
|
| 132 |
private void resetRotationAngle(Dynamic1D rotationAngle)
|
|
| 105 | 133 |
{
|
| 106 | 134 |
rotationAngle.setDuration(POST_ROTATION_MILLISEC); |
| 107 | 135 |
rotationAngle.resetToBeginning(); |
| ... | ... | |
| 176 | 204 |
|
| 177 | 205 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 178 | 206 |
|
| 179 |
abstract float[] getLegalQuats(); |
|
| 207 |
public void savePreferences(SharedPreferences.Editor editor) |
|
| 208 |
{
|
|
| 209 |
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor); |
|
| 210 |
} |
|
| 180 | 211 |
|
| 181 |
public abstract void savePreferences(SharedPreferences.Editor editor); |
|
| 182 |
public abstract void restorePreferences(SharedPreferences preferences); |
|
| 212 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 183 | 213 |
|
| 184 |
public abstract void beginNewRotation(int vector, int row );
|
|
| 185 |
public abstract long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener );
|
|
| 186 |
public abstract long finishRotationNow(EffectListener listener);
|
|
| 187 |
public abstract void removeRotationNow();
|
|
| 214 |
public void restorePreferences(SharedPreferences preferences)
|
|
| 215 |
{
|
|
| 216 |
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].restorePreferences(preferences);
|
|
| 217 |
}
|
|
| 188 | 218 |
|
| 189 |
public abstract void apply(Effect effect, int position); |
|
| 190 |
public abstract void remove(long effectID); |
|
| 219 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 191 | 220 |
|
| 192 |
public abstract void releaseResources(); |
|
| 193 |
public abstract void createTexture(); |
|
| 221 |
public long finishRotationNow(EffectListener listener) |
|
| 222 |
{
|
|
| 223 |
boolean first = true; |
|
| 224 |
long effectID=0; |
|
| 225 |
|
|
| 226 |
for(int i=0; i<NUM_CUBITS; i++) |
|
| 227 |
{
|
|
| 228 |
if( belongsToRotation(mCubits[i].mCurrentPosition,mRotAxis,mRotRow) ) |
|
| 229 |
{
|
|
| 230 |
if( first ) |
|
| 231 |
{
|
|
| 232 |
first=false; |
|
| 233 |
effectID = mCubits[i].finishRotationNow(listener); |
|
| 234 |
} |
|
| 235 |
resetRotationAngle(mCubits[i].mRotationAngle); |
|
| 236 |
} |
|
| 237 |
} |
|
| 238 |
|
|
| 239 |
return effectID; |
|
| 240 |
} |
|
| 241 |
|
|
| 242 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 243 |
|
|
| 244 |
public void releaseResources() |
|
| 245 |
{
|
|
| 246 |
mTexture.markForDeletion(); |
|
| 247 |
|
|
| 248 |
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].releaseResources(); |
|
| 249 |
} |
|
| 250 |
|
|
| 251 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 252 |
|
|
| 253 |
public void apply(Effect effect, int position) |
|
| 254 |
{
|
|
| 255 |
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.apply(effect, position); |
|
| 256 |
} |
|
| 257 |
|
|
| 258 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 259 |
|
|
| 260 |
public void remove(long effectID) |
|
| 261 |
{
|
|
| 262 |
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.abortById(effectID); |
|
| 263 |
} |
|
| 194 | 264 |
|
| 195 |
public abstract void solve(); |
|
| 196 |
public abstract boolean isSolved(); |
|
| 265 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 266 |
|
|
| 267 |
public void solve() |
|
| 268 |
{
|
|
| 269 |
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].solve(); |
|
| 270 |
} |
|
| 271 |
|
|
| 272 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 273 |
|
|
| 274 |
public boolean isSolved() |
|
| 275 |
{
|
|
| 276 |
Static4D q = mCubits[0].mQuatScramble; |
|
| 277 |
|
|
| 278 |
float x = q.get0(); |
|
| 279 |
float y = q.get1(); |
|
| 280 |
float z = q.get2(); |
|
| 281 |
float w = q.get3(); |
|
| 282 |
|
|
| 283 |
for(int i=0; i<NUM_CUBITS; i++) |
|
| 284 |
{
|
|
| 285 |
q = mCubits[i].mQuatScramble; |
|
| 286 |
|
|
| 287 |
if( q.get0()!=x || q.get1()!=y || q.get2()!=z || q.get3()!=w ) return false; |
|
| 288 |
} |
|
| 289 |
|
|
| 290 |
return true; |
|
| 291 |
} |
|
| 292 |
|
|
| 293 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 294 |
|
|
| 295 |
public void beginNewRotation(int vector, int row ) |
|
| 296 |
{
|
|
| 297 |
Static3D axis = VectX; |
|
| 298 |
|
|
| 299 |
switch(vector) |
|
| 300 |
{
|
|
| 301 |
case VECTX: axis = VectX; break; |
|
| 302 |
case VECTY: axis = VectY; break; |
|
| 303 |
case VECTZ: axis = VectZ; break; |
|
| 304 |
} |
|
| 305 |
|
|
| 306 |
mRotAxis = vector; |
|
| 307 |
mRotRow = row; |
|
| 308 |
|
|
| 309 |
mRotationAngleStatic.set0(0.0f); |
|
| 310 |
|
|
| 311 |
for(int i=0; i<NUM_CUBITS; i++) |
|
| 312 |
if( belongsToRotation( mCubits[i].mCurrentPosition,vector,mRotRow) ) |
|
| 313 |
{
|
|
| 314 |
mCubits[i].beginNewRotation(axis); |
|
| 315 |
} |
|
| 316 |
} |
|
| 317 |
|
|
| 318 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 319 |
|
|
| 320 |
public long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener ) |
|
| 321 |
{
|
|
| 322 |
Static3D axis = VectX; |
|
| 323 |
long effectID=0; |
|
| 324 |
boolean first = true; |
|
| 325 |
|
|
| 326 |
switch(vector) |
|
| 327 |
{
|
|
| 328 |
case VECTX: axis = VectX; break; |
|
| 329 |
case VECTY: axis = VectY; break; |
|
| 330 |
case VECTZ: axis = VectZ; break; |
|
| 331 |
} |
|
| 332 |
|
|
| 333 |
mRotAxis = vector; |
|
| 334 |
mRotRow = row; |
|
| 335 |
|
|
| 336 |
mRotationAngleStatic.set0(0.0f); |
|
| 337 |
|
|
| 338 |
for(int i=0; i<NUM_CUBITS; i++) |
|
| 339 |
if( belongsToRotation(mCubits[i].mCurrentPosition,vector,mRotRow) ) |
|
| 340 |
{
|
|
| 341 |
mCubits[i].addNewRotation(axis,durationMillis,angle); |
|
| 342 |
|
|
| 343 |
if( first ) |
|
| 344 |
{
|
|
| 345 |
first = false; |
|
| 346 |
effectID = mCubits[i].setUpCallback(listener); |
|
| 347 |
} |
|
| 348 |
} |
|
| 349 |
|
|
| 350 |
return effectID; |
|
| 351 |
} |
|
| 352 |
|
|
| 353 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 354 |
|
|
| 355 |
public void removeRotationNow() |
|
| 356 |
{
|
|
| 357 |
float qx=0,qy=0,qz=0; |
|
| 358 |
boolean first = true; |
|
| 359 |
Static4D quat = null; |
|
| 360 |
|
|
| 361 |
switch(mRotAxis) |
|
| 362 |
{
|
|
| 363 |
case VECTX: qx=1; break; |
|
| 364 |
case VECTY: qy=1; break; |
|
| 365 |
case VECTZ: qz=1; break; |
|
| 366 |
} |
|
| 367 |
|
|
| 368 |
for(int i=0; i<NUM_CUBITS; i++) |
|
| 369 |
if( belongsToRotation(mCubits[i].mCurrentPosition,mRotAxis,mRotRow) ) |
|
| 370 |
{
|
|
| 371 |
if( first ) |
|
| 372 |
{
|
|
| 373 |
first = false; |
|
| 374 |
quat = mCubits[i].returnRotationQuat(qx,qy,qz); |
|
| 375 |
} |
|
| 376 |
|
|
| 377 |
mCubits[i].removeRotationNow(quat); |
|
| 378 |
} |
|
| 379 |
|
|
| 380 |
mRotationAngleStatic.set0(0); |
|
| 381 |
} |
|
| 382 |
|
|
| 383 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 384 |
|
|
| 385 |
abstract int getNumCubits(int size); |
|
| 386 |
abstract int[][] getCubitPositions(int size); |
|
| 387 |
abstract float[] getLegalQuats(); |
|
| 388 |
abstract boolean belongsToRotation(Static3D position, int axis, int row); |
|
| 389 |
abstract MeshCubes createMesh(int vertices,int x, int y, int z); |
|
| 390 |
|
|
| 391 |
public abstract void createTexture(); |
|
| 197 | 392 |
} |
| src/main/java/org/distorted/object/RubikObjectList.java | ||
|---|---|---|
| 22 | 22 |
import org.distorted.library.main.DistortedEffects; |
| 23 | 23 |
import org.distorted.library.main.DistortedTexture; |
| 24 | 24 |
import org.distorted.library.mesh.MeshRectangles; |
| 25 |
import org.distorted.library.type.Static3D; |
|
| 25 | 26 |
import org.distorted.library.type.Static4D; |
| 26 | 27 |
import org.distorted.magic.R; |
| 27 | 28 |
|
| ... | ... | |
| 40 | 41 |
public static final int VECTX = 0; // |
| 41 | 42 |
public static final int VECTY = 1; // don't change this |
| 42 | 43 |
public static final int VECTZ = 2; // |
| 44 |
static final Static3D VectX = new Static3D(1,0,0); |
|
| 45 |
static final Static3D VectY = new Static3D(0,1,0); |
|
| 46 |
static final Static3D VectZ = new Static3D(0,0,1); |
|
| 43 | 47 |
|
| 44 | 48 |
public static final int LENGTH = values().length; |
| 45 | 49 |
private final int mObjectSize, mIconID; |
Also available in: Unified diff
More work on Cubit.