-
Notifications
You must be signed in to change notification settings - Fork 6
/
sample.ts
225 lines (197 loc) · 5.51 KB
/
sample.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { Dimensions } from "react-native";
import { SharedValue } from "react-native-reanimated";
import { MAX_SPEED, PADDLE_HEIGHT, PADDLE_WIDTH, RADIUS } from "./constants";
import {
BrickInterface,
CircleInterface,
Collision,
PaddleInterface,
ShapeInterface,
} from "./types";
const { width, height } = Dimensions.get("window");
const move = (object: ShapeInterface, dt: number) => {
"worklet";
if (object.type === "Circle") {
object.vx += object.ax * dt;
object.vy += object.ay * dt;
if (object.vx > MAX_SPEED) {
object.vx = MAX_SPEED;
}
if (object.vx < -MAX_SPEED) {
object.vx = -MAX_SPEED;
}
if (object.vy > MAX_SPEED) {
object.vy = MAX_SPEED;
}
if (object.vy < -MAX_SPEED) {
object.vy = -MAX_SPEED;
}
object.x.value += object.vx * dt;
object.y.value += object.vy * dt;
}
};
export const resolveCollisionWithBounce = (info: Collision) => {
"worklet";
const circleInfo = info.o1 as CircleInterface;
circleInfo.y.value = circleInfo.y.value - circleInfo.r;
if (info.o2.type === "Brick" && circleInfo.ay > 0) {
return;
}
circleInfo.vx = circleInfo.vx;
circleInfo.ax = circleInfo.ax;
circleInfo.vy = -circleInfo.vy;
circleInfo.ay = -circleInfo.ay;
};
// Source: https://martinheinz.dev/blog/15
export const resolveWallCollision = (object: ShapeInterface) => {
"worklet";
// Collision with the right wall
if (object.type === "Circle") {
const circleObject = object as CircleInterface;
if (circleObject.x.value + circleObject.r > width) {
// Calculate the overshot
circleObject.x.value = width - circleObject.r * 2;
circleObject.vx = -circleObject.vx;
circleObject.ax = -circleObject.ax;
}
// Collision with the bottom wall
else if (circleObject.y.value + circleObject.r > height) {
circleObject.x.value = 100;
circleObject.y.value = 450;
circleObject.ax = 0.5;
circleObject.ay = 1;
circleObject.vx = 0;
circleObject.vy = 0;
return true;
}
// Collision with the left wall
else if (circleObject.x.value - circleObject.r < 0) {
circleObject.x.value = circleObject.r * 2;
circleObject.vx = -circleObject.vx;
circleObject.ax = -circleObject.ax;
}
// Detect collision with the top wall
else if (circleObject.y.value - circleObject.r < 0) {
circleObject.y.value = circleObject.r;
circleObject.vy = -circleObject.vy;
circleObject.ay = -circleObject.ay;
}
return false;
}
};
export const createBouncingExample = (circleObject: CircleInterface) => {
"worklet";
circleObject.x.value = 100;
circleObject.y.value = 450;
circleObject.r = RADIUS;
circleObject.ax = 0.5;
circleObject.ay = 1;
circleObject.vx = 0;
circleObject.vy = 0;
circleObject.m = RADIUS * 10;
};
// Source: https://www.jeffreythompson.org/collision-detection/table_of_contents.php
function circleRect(
cx: number,
cy: number,
rx: number,
ry: number,
rw: number,
rh: number
) {
"worklet";
// temporary variables to set edges for testing
let testX = cx;
let testY = cy;
// which edge is closest?
if (cx < rx) testX = rx; // test left edge
else if (cx > rx + rw) testX = rx + rw; // right edge
if (cy < ry) testY = ry; // top edge
else if (cy > ry + rh) testY = ry + rh; // bottom edge
// get distance from closest edges
let distX = cx - testX;
let distY = cy - testY;
let distance = Math.sqrt(distX * distX + distY * distY);
// if the distance is less than the radius, collision!
if (distance <= RADIUS) {
return true;
}
return false;
}
export const checkCollision = (o1: ShapeInterface, o2: ShapeInterface) => {
"worklet";
if (
(o1.type === "Circle" && o2.type === "Paddle") ||
(o1.type === "Circle" && o2.type === "Brick")
) {
if (o2.type === "Brick") {
const brick = o2 as BrickInterface;
if (!brick.canCollide.value) {
return {
collisionInfo: null,
collided: false,
};
}
}
const dx = o2.x.value - o1.x.value;
const dy = o2.y.value - o1.y.value;
const d = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
const circleObj = o1 as CircleInterface;
const paddleObj = o2 as PaddleInterface;
const isCollision = circleRect(
circleObj.x.value,
circleObj.y.value,
paddleObj.x.value,
paddleObj.y.value,
PADDLE_WIDTH,
PADDLE_HEIGHT
);
if (isCollision) {
if (o2.type === "Brick") {
const brick = o2 as BrickInterface;
brick.canCollide.value = false;
}
return {
collisionInfo: { o1, o2, dx, dy, d },
collided: true,
};
}
}
return {
collisionInfo: null,
collided: false,
};
};
export const animate = (
objects: ShapeInterface[],
timeSincePreviousFrame: number,
brickCount: SharedValue<number>
) => {
"worklet";
for (const o of objects) {
move(o, (0.15 / 16) * timeSincePreviousFrame);
}
for (const o of objects) {
const isGameLost = resolveWallCollision(o);
if (isGameLost) {
brickCount.value = -1;
}
}
const collisions: Collision[] = [];
for (const [i, o1] of objects.entries()) {
for (const [j, o2] of objects.entries()) {
if (i < j) {
const { collided, collisionInfo } = checkCollision(o1, o2);
if (collided && collisionInfo) {
collisions.push(collisionInfo);
}
}
}
}
for (const col of collisions) {
if (col.o2.type === "Brick") {
brickCount.value++;
}
resolveCollisionWithBounce(col);
}
};