-
Notifications
You must be signed in to change notification settings - Fork 1
/
CollisionModule.x68
320 lines (203 loc) · 5.32 KB
/
CollisionModule.x68
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
;Collision related logic
CheckWallCollision:
;check if in bounds
cmp.l #(TOP_LEFT_BOARD_X_POS), (BallXPosition)
ble .Collided
cmp.l #(BOTTOM_RIGHT_BOARD_X_POS-BALL_WIDTH), (BallXPosition)
bge .Collided
move.l #(FALSE),d0
jmp .EndCheck
.Collided:
move.l #(TRUE), d0
.EndCheck:
rts
CheckPegCollision:
;movem.l ALL_REG, -(sp)
;set params
move.l (BallXPosition), d0
add.l #(BALL_RADIUS), d0 ;shift to center
move.l (BallYPosition), d1
add.l #(BALL_RADIUS), d1 ;shift to center
move.l CURRENT_PEG_X_STACK_OFFSET(sp),d2
move.l CURRENT_PEG_Y_STACK_OFFSET(sp),d3
;check if we even should do collision check
;dy is first
cmp.l d1,d3
bge .SubBallYPegY ; if peg y bigger
sub.l d3,d1
move.l d1,d5 ;dy
jmp .CheckDy
.SubBallYPegY:
sub.l d1,d3
move.l d3, d5 ;dy
.CheckDy:
cmp.l #(MIN_DY_TO_CHECK_COLLISION), d5
bgt .DidntCollide
;dx is second
cmp.l d0,d2
bge .SubBallXPegX ; if peg x bigger
sub.l d2,d0
move.l d0,d4 ;dx
jmp .CheckDx
.SubBallXPegX:
sub.l d0,d2
move.l d2, d4 ;dx
.CheckDx:
cmp.l #(MIN_DX_TO_CHECK_COLLISION), d4
bgt .DidntCollide
;store the closest peg coor
move.l CURRENT_PEG_X_STACK_OFFSET(sp),(ClosestPegX)
move.l CURRENT_PEG_Y_STACK_OFFSET(sp),(ClosestPegY)
;if we're here. we check for collision
move.l d4,d0 ;dx
mulu.w d4,d0 ;d0 = dx^2
move.l d5,d1 ;dy
mulu.w d5,d1 ;d1 = dy^2
add.l d0, d1 ;d1 = dx^2 + dy^2
lsl.l #2, d1
lea (Sqrts), a6
add.l d1, a6
move.l (a6), d0
;move.l (a6, d1), d0 ;d0 = sqrt(dx^2 + dy^2) with no byte swap
jsr SwapBytes ;d0 bytes are swapped, so it has the correct sqrt
lsr.l #(FRACTION_BITS),d0
;combine radiuses
move.l #(BALL_RADIUS), d1
add.l #(PEG_RADIUS), d1
cmp.l d1, d0
bgt .DidntCollide
move.l #(TRUE), d0
jmp .CheckEnd
.DidntCollide:
move.l #(FALSE), d0
.CheckEnd:
;movem.l (sp)+, ALL_REG
rts
BounceBallOffWall:
move.l (BallXVelocity), d0
;muls.w #(HALF), d0
;asr.l #(FRACTION_BITS),d0 ;soften collision
neg.l d0
move.l d0, (BallXVelocity)
rts
BounceBallOffPeg:
;set tangent vect
move.l (ClosestPegY),d0
sub.l (BallYPosition), d0
add.l #(BALL_RADIUS), d0 ;d0 = tgX = Ypeg - Yball
move.l (BallXPosition), d1
add.l #(BALL_RADIUS), d1
sub.l (ClosestPegX), d1 ;d1 = tgY = Xball - Xpeg
;get vect length
move.l d0, d6
move.l d1, d7
muls.w d0, d6 ;d6 = tgX ^ 2
muls.w d1, d7 ;d7 = tgY ^ 2
add.l d6, d7 ;d7 = tgX ^ 2 + tgY ^ 2
lsl.l #2, d7
lea (Sqrts), a6
add.l d7, a6
move.l (a6), d2
;move.l (a6, d7), d2 ;d2 = TgMag = sqrt(tgX ^ 2 + tgY ^ 2) with no byte swap
move.l d0, d7 ;store tgX
move.l d2, d0
jsr SwapBytes
lsr.l #(FRACTION_BITS),d0
move.l d0, d2 ;d2 bytes are swapped, so it has the correct sqrt
move.l d7, d0 ;restore d0
cmp.l #0, d2
bne .NotZeroSqrt
; to avoid div by 0
move.l #1, d2
.NotZeroSqrt:
;get normalized tangent
divs.w d2, d0 ;Xtg
swap d0
clr.w d0
swap d0
divs.w d2, d1 ;Ytg
swap d1
clr.w d1
swap d1
;get length by calculating the dot product
;copy x and y
move.l d0, d2 ;Xtg
move.l d1, d3 ;Ytg
;get velocity
move.l (BallXVelocity), d6
move.l (BallYVelocity), d7
; dot product
muls.w d6, d2
muls.w d7, d3
asr.l #(FRACTION_BITS),d2
asr.l #(FRACTION_BITS),d3
add.l d2, d3
move.l d3, d4 ;d4 = length
;get the velocity comp parallel to the tg
;copy x and y of tg
move.l d0, d2 ;Xtg
move.l d1, d3 ;Ytg
muls.w d4, d2 ;d2 = Xparal = length * Xtg
muls.w d4, d3 ;d3 = Yparal = length * Ytg
;get velocity
move.l (BallXVelocity), d0 ;Xvel
move.l (BallYVelocity), d1 ;Yvel
;get the velocity comp perpendicular to the tg
sub.l d2, d0 ;d0 = Xper = Xvel - Xparal
sub.l d3, d1 ;d1 = Yper = Yvel - Yparal
;get the new velocity
asl.l #2, d0
asl.l #2, d1
move.l (BallXVelocity), d2 ;Xvel
move.l (BallYVelocity), d3 ;Yvel
sub.l d0, d2 ;d2 = NewXvel = Xvel - 2 * Xper
sub.l d1, d3 ;d3 = NewYvel = Yvel - 2 * Yper
muls.w #(Y_VELOCITY_SOFTNER), d3
asr.l #(FRACTION_BITS),d3 ;soften y velocity
;get the abs to test on it
move.l d2, d1
jsr GetAbsoluteValue ;abs will be in d0
cmp.l #(MIN_RAND_X_VEL_ALLOWED),d0
blt .NoXSoftening
muls.w #(X_VELOCITY_SOFTNER), d2 ;soften x velocity
asr.l #(FRACTION_BITS),d2
jmp .NoRandVelX
.NoXSoftening:
jsr GetRandomXVel
move.l d0, d2
.NoRandVelX:
;update the velocity
move.l d2, (BallXVelocity)
move.l d3, (BallYVelocity)
rts
GetRandomXVel:
movem.l d1,-(sp)
jsr GetRandomNumber
and.l #(MAX_RAND_X_VELOCITY), d0
add.l #(MIN_RAND_X_VELOCITY), d0 ;between min and max
;on first drop
cmp.l #(FALSE), (BallDropped)
beq .RandFirstXVel
;decide if negative or positive based on the ball x position compared to peg
move.l (BallXPosition), d1
sub.l (ClosestPegX), d1
cmp.l #0, d1
bge .EndRand
;go right else go left
neg.l d0
jmp .EndRand
.RandFirstXVel:
move.l d0, d1 ;store rand
jsr GetRandomNumber ; get new rand
and.l #1, d0
cmp.l #0, d0
bne .MakeVelNegative
;go right
move.l d1, d0 ;restore pos rand
jmp .EndRand
.MakeVelNegative: ;go left
neg.l d1
move.l d1, d0 ;restore neg rand
.EndRand:
movem.l (sp)+,d1
rts