Skip to content

Commit 148d5a3

Browse files
Add rotation animation to cube
1 parent 736144b commit 148d5a3

File tree

4 files changed

+130
-9
lines changed

4 files changed

+130
-9
lines changed

floppysolver

320 Bytes
Binary file not shown.

src/floppy.c

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,43 @@ FloppyCube InitFloppyCube(FloppyCube floppyCube){
1111
return floppyCube;
1212
}
1313

14-
void DrawFloppyCube(FloppyCube floppyCube){
14+
float progress = 0;
15+
int fRotating = 0;
16+
int rRotating = 0;
17+
int lRotating = 0;
18+
int bRotating = 0;
19+
20+
int animSpeed = 4;
21+
22+
FloppyCube DrawFloppyCube(FloppyCube floppyCube){
23+
if(fRotating || rRotating || lRotating || bRotating){
24+
progress += animSpeed;
25+
}
26+
if(progress >= 180){
27+
progress = 0;
28+
if(fRotating){
29+
floppyCube = ExecuteTurn(floppyCube, F);
30+
fRotating = 0;
31+
}
32+
if(rRotating){
33+
floppyCube = ExecuteTurn(floppyCube, R);
34+
rRotating = 0;
35+
}
36+
if(lRotating){
37+
floppyCube = ExecuteTurn(floppyCube, L);
38+
lRotating = 0;
39+
}
40+
if(bRotating){
41+
floppyCube = ExecuteTurn(floppyCube, B);
42+
bRotating = 0;
43+
}
44+
}
1545
Vector2 planeSize = {1.9, 1.9};
1646

1747
float planeOffset = 0.005;
1848

1949
// Black centre area
20-
DrawCube((Vector3){0.0,0.0,0.0}, 6.0, 2.0, 6.0, BLACK);
50+
// DrawCube((Vector3){0.0,0.0,0.0}, 6.0, 2.0, 6.0, BLACK);
2151

2252
// White and yellow stickers
2353
DrawPlane((Vector3){0.0,planeOffset+1.0,0.0}, planeSize, WHITE);
@@ -45,46 +75,122 @@ void DrawFloppyCube(FloppyCube floppyCube){
4575
index--;
4676
}
4777

78+
rlPushMatrix();
79+
if(i > 3 && i != 5 && fRotating){
80+
rlRotatef(progress, 0, 0, 1);
81+
}
82+
if(rRotating && (index == 2 || index == 4 || index == 7)){
83+
rlRotatef(progress, 1, 0, 0);
84+
}
85+
if(lRotating && (index == 0 || index == 3 || index == 5)){
86+
rlRotatef(progress, 1, 0, 0);
87+
}
88+
if(bRotating && index < 3){
89+
rlRotatef(progress, 0, 0, 1);
90+
}
4891
DrawPlane((Vector3){pos.x,planeOffset+1.0,pos.y}, planeSize, GetColorFromIndex(floppyCube.topFace[index]));
92+
rlPopMatrix();
4993

5094
rlPushMatrix();
5195
rlRotatef(180, 0, 0, -1);
5296
rlRotatef(180, 0, 1, 0);
97+
if(i < 3 && fRotating){
98+
rlRotatef(progress, 0, 0, -1);
99+
}
100+
if(bRotating && index > 4){
101+
rlRotatef(progress, 0, 0, -1);
102+
}
103+
if(rRotating && (index == 2 || index == 4 || index == 7)){
104+
rlRotatef(progress, 1, 0, 0);
105+
}
106+
if(lRotating && (index == 0 || index == 3 || index == 5)){
107+
rlRotatef(progress, 1, 0, 0);
108+
}
53109
DrawPlane((Vector3){pos.x,planeOffset+1.0,pos.y}, planeSize, GetColorFromIndex(floppyCube.bottomFace[index]));
54110
rlPopMatrix();
55111
}
56112

57113
// Green stickers
58114
rlPushMatrix();
59115
rlRotatef(90, 1, 0, 0);
116+
if(fRotating){
117+
rlRotatef(progress, 0, 1, 0);
118+
}
119+
if(lRotating){
120+
rlRotatef(progress, 1, 0, 0);
121+
}
60122
DrawPlane((Vector3){-2,3+planeOffset,0}, planeSize, GetColorFromIndex(floppyCube.frontFace[0]));
123+
if(lRotating){
124+
rlRotatef(progress, -1, 0, 0);
125+
}
61126
DrawPlane((Vector3){0,3+planeOffset,0}, planeSize, GetColorFromIndex(floppyCube.frontFace[1]));
127+
if(rRotating){
128+
rlRotatef(progress, 1, 0, 0);
129+
}
62130
DrawPlane((Vector3){2,3+planeOffset,0}, planeSize, GetColorFromIndex(floppyCube.frontFace[2]));
63131
rlPopMatrix();
64132

65133
// Red stickers
66134
rlPushMatrix();
67135
rlRotatef(-90, 0, 0, 1);
136+
if(rRotating){
137+
rlRotatef(progress, 0, 1, 0);
138+
}
139+
if(fRotating){
140+
rlRotatef(progress, 0, 0, 1);
141+
}
68142
DrawPlane((Vector3){0,3+planeOffset,2}, planeSize, GetColorFromIndex(floppyCube.rightFace[0]));
143+
if(fRotating){
144+
rlRotatef(progress, 0, 0, -1);
145+
}
69146
DrawPlane((Vector3){0,3+planeOffset,0}, planeSize, GetColorFromIndex(floppyCube.rightFace[1]));
147+
if(bRotating){
148+
rlRotatef(progress, 0, 0, 1);
149+
}
70150
DrawPlane((Vector3){0,3+planeOffset,-2}, planeSize, GetColorFromIndex(floppyCube.rightFace[2]));
71151
rlPopMatrix();
72152

73153
// Orange stickers
74154
rlPushMatrix();
75155
rlRotatef(90, 0, 0, 1);
156+
if(lRotating){
157+
rlRotatef(progress, 0, -1, 0);
158+
}
159+
if(bRotating){
160+
rlRotatef(progress, 0, 0, 1);
161+
}
76162
DrawPlane((Vector3){0,3+planeOffset,-2}, planeSize, GetColorFromIndex(floppyCube.leftFace[0]));
163+
if(bRotating){
164+
rlRotatef(progress, 0, 0, -1);
165+
}
77166
DrawPlane((Vector3){0,3+planeOffset,0}, planeSize, GetColorFromIndex(floppyCube.leftFace[1]));
167+
if(fRotating){
168+
rlRotatef(progress, 0, 0, 1);
169+
}
78170
DrawPlane((Vector3){0,3+planeOffset,2}, planeSize, GetColorFromIndex(floppyCube.leftFace[2]));
79171
rlPopMatrix();
80172

81173
// Blue stickers
82174
rlPushMatrix();
83175
rlRotatef(-90, 1, 0, 0);
176+
if(bRotating){
177+
rlRotatef(progress, 0, -1, 0);
178+
}
179+
if(rRotating){
180+
rlRotatef(progress, 1, 0, 0);
181+
}
84182
DrawPlane((Vector3){2,3+planeOffset,0}, planeSize, GetColorFromIndex(floppyCube.backFace[0]));
183+
if(rRotating){
184+
rlRotatef(progress, -1, 0, 0);
185+
}
85186
DrawPlane((Vector3){0,3+planeOffset,0}, planeSize, GetColorFromIndex(floppyCube.backFace[1]));
187+
if(lRotating){
188+
rlRotatef(progress, 1, 0, 0);
189+
}
86190
DrawPlane((Vector3){-2,3+planeOffset,0}, planeSize, GetColorFromIndex(floppyCube.backFace[2]));
87191
rlPopMatrix();
192+
193+
return floppyCube;
88194
}
89195

90196
FloppyCube ExecuteTurn(FloppyCube cube, int turn){
@@ -120,6 +226,21 @@ FloppyCube ExecuteTurn(FloppyCube cube, int turn){
120226
return cube;
121227
}
122228

229+
void DoTurn(FloppyCube cube, int turn){
230+
if(turn == F){
231+
fRotating = 1;
232+
}
233+
if(turn == B){
234+
bRotating = 1;
235+
}
236+
if(turn == R){
237+
rRotating = 1;
238+
}
239+
if(turn == L){
240+
lRotating = 1;
241+
}
242+
}
243+
123244
FloppyCube RandomScramble(FloppyCube cube){
124245
int previous;
125246

src/floppy.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ enum _Turns{
4040

4141
typedef enum _Turns Turns;
4242

43-
void DrawFloppyCube(FloppyCube floppyCube);
43+
void DoTurn(FloppyCube cube, int turn);
4444

4545
FloppyCube RandomScramble(FloppyCube cube);
46+
FloppyCube DrawFloppyCube(FloppyCube floppyCube);
4647
FloppyCube InitFloppyCube(FloppyCube floppyCube);
4748
FloppyCube ExecuteTurn(FloppyCube cube, int turn);
4849

src/main.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ void setup(){
1313
srand(time(NULL));
1414

1515
floppy = InitFloppyCube(floppy);
16-
floppy = ExecuteTurn(floppy, R);
1716
}
1817

1918
int main(){
@@ -43,7 +42,7 @@ int main(){
4342
ClearBackground(LIGHTGRAY);
4443

4544
BeginMode3D(camera);
46-
DrawFloppyCube(floppy);
45+
floppy = DrawFloppyCube(floppy);
4746
EndMode3D();
4847

4948
DrawFPS(10,10);
@@ -76,16 +75,16 @@ int main(){
7675
}
7776

7877
if(IsKeyReleased(KEY_F)){
79-
floppy = ExecuteTurn(floppy, F);
78+
/*floppy = */DoTurn(floppy, F);
8079
}
8180
if(IsKeyReleased(KEY_R)){
82-
floppy = ExecuteTurn(floppy, R);
81+
/*floppy = */DoTurn(floppy, R);
8382
}
8483
if(IsKeyReleased(KEY_L)){
85-
floppy = ExecuteTurn(floppy, L);
84+
/*floppy = */DoTurn(floppy, L);
8685
}
8786
if(IsKeyReleased(KEY_B)){
88-
floppy = ExecuteTurn(floppy, B);
87+
/*floppy =*/ DoTurn(floppy, B);
8988
}
9089
if(IsKeyReleased(KEY_S)){
9190
floppy = RandomScramble(floppy);

0 commit comments

Comments
 (0)