Repository navigation
Expand file tree
/
Copy pathball.asm
More file actions
313 lines (268 loc) · 8.56 KB
/
Copy pathball.asm
File metadata and controls
313 lines (268 loc) · 8.56 KB
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
ball:
.angle dd ?
.slope dd ?
.x dw ?
.y dw ?
.xOrigin dw ?
.yOrigin dw ?
.direction dw ?
; word <bitfield r> checkBallCollision(void)
;
; Test for the ball's hitbox touching any other hitbox, and return a corresponding
; bitfield.
checkBallCollision:
push bx
push cx
xor ax, ax
.verticalSurfaces:
; Check for field border collisions
mov bx, word [ball.x] ; Test left field border
cmp bx, field.xMin
jne @f
or ax, r.VMIN
jmp .horizontalSurfaces
@@:
add bx, ball.width ; Test right field border
cmp bx, field.xMax
jne @f
or ax, r.VMAX
jmp .horizontalSurfaces
; Check for paddle collisions
@@:
mov bx, word [ball.x] ; Test left side of the paddle
cmp bx, word [paddle.x]
jne @f
mov cx, word [paddle.y]
mov bx, word [ball.y]
cmp bx, cx
ja @f
sub cx, paddle.height
cmp bx, cx
jb @f
or ax, r.VPADDLELEFT
jmp .horizontalSurfaces
@@:
add bx, paddle.width ; Test right side of the paddle
mov cx, word [paddle.x]
add cx, paddle.width
cmp bx, cx
jne @f
mov cx, word [paddle.y]
mov bx, word [ball.y]
cmp bx, cx
ja @f
sub cx, paddle.height
cmp bx, cx
jb @f
or ax, r.VPADDLERIGHT
jmp .horizontalSurfaces
@@:
.horizontalSurfaces:
; Check for field border collisions
mov bx, word [ball.y] ; Test bottom field border
cmp bx, field.yMin
jne @f
or ax, r.HMIN
jmp .end
@@:
add bx, ball.height ; Test top field border
cmp bx, field.yMax
jne @f
or ax, r.HMAX
jmp .end
; Check for paddle collisions
@@:
mov bx, word [ball.y] ; Test top of paddle
cmp bx, word [paddle.y]
jne @f
mov cx, word [paddle.x]
mov bx, word [ball.x]
cmp bx, cx
jb @f
add cx, paddle.width
cmp bx, cx
ja @f
or ax, r.HPADDLE
jmp .end
@@:
.end:
pop cx
pop bx
ret
; void ricochet(word <bitfield r> reflection)
;
; Adjust the ball's slope and direction value in accordance to the surface it's
; bouncing off of.
ricochet:
push bx
.verticalSurfaces:
test ax, r.VMIN or r.VMAX ; are at least one of the vertical bits set?
jz .horizontalSurfaces
fld dword [ball.slope] ; slope *= -1
fchs
fstp dword [ball.slope]
btc word [ball.direction], d.bit.X
; invert direction flag for X
.horizontalSurfaces:
test ax, r.HMIN or r.HMAX
jz .horizontalPaddle ; are at least one of the horizontal bits set?
fld dword [ball.slope] ; slope *= -1
fchs ; this will be overwritten if it's a paddle collision
fstp dword [ball.slope]
btc word [ball.direction], d.bit.Y
; invert direction flag for Y
.horizontalPaddle:
bt ax, r.bit.HPADDLE ; is the horizontal paddle collision bit set?
jnc .verticalPaddle
; The full range of the possible angles is 2pi/3 radians, or pi/6 to 5pi/6.
; This range is divided into however many pixels make up the paddle's width.
; angle = (2pi/3 / paddle.width) * (paddle.x + paddle.width - ball.x) + pi/6
mov bx, word [paddle.x]
add bx, paddle.width
sub bx, word [ball.x] ; bx = distance of ball from right side of paddle
mov word [ball.angle], bx ; using [ball.angle] as a temporary variable
fild word [ball.angle] ; load bx
mov dword [ball.angle], ball.angleRange
fmul dword [ball.angle] ; st0 = bx * 2pi/3
mov word [ball.angle], paddle.width
fidiv word [ball.angle] ; st0 /= paddle.width
mov dword [ball.angle], ball.angleMin
fadd dword [ball.angle] ; st0 += pi/6
fst dword [ball.angle] ; [ball.angle] = st0
call newTrajectory ; Calculate new ball slope from angle
.verticalPaddle:
bt ax, r.bit.VPADDLELEFT ; left vertical side of paddle?
jnc @f
mov dword [ball.angle], ball.angleMax
call newTrajectory
@@:
bt ax, r.bit.VPADDLERIGHT ; right vertical side of paddle?
jnc @f
mov dword [ball.angle], ball.angleMin
call newTrajectory
@@:
.newOrigin:
cmp ax, 0 ; were any of the bits set?
jz .end
bt ax, r.bit.VMIN ; adjust coordinates in the event of a collision
jnc @f
inc word [ball.x]
jmp .set
@@:
bt ax, r.bit.VMAX
jnc @f
dec word [ball.x]
jmp .set
@@:
bt ax, r.bit.HMIN
jnc @f
inc word [ball.y]
jmp .set
@@:
bt ax, r.bit.HMAX
jnc @f
dec word [ball.y]
jmp .set
@@:
bt ax, r.bit.VPADDLELEFT
jnc @f
mov bx, word [paddle.x]
mov word [ball.x], bx
mov bx, word [paddle.y]
inc bx
mov word [ball.y], bx
jmp .set
@@:
bt ax, r.bit.VPADDLERIGHT
jnc @f
mov bx, word [paddle.x]
add bx, paddle.width
mov word [ball.x], bx
mov bx, word [paddle.y]
inc bx
mov word [ball.y], bx
jmp .set
@@:
bt ax, r.bit.HPADDLE
jnc @f
inc word [ball.y]
jmp .set
@@:
.set:
mov ax, word [ball.x] ; set origin point to current location
mov word [ball.xOrigin], ax
mov ax, word [ball.y]
mov word [ball.yOrigin], ax
.end:
pop bx
ret
; void newTrajectory(void)
;
; Recalculate the slope of the ball's trajectory for a new angle value.
;
; if cos(slope) != 0.0
; ball.slope = sin(slope) / cos(slope)
; else
; ball.slope = log(10)2
newTrajectory:
fld dword [ball.angle] ; st0 = angle
fsincos ; st1 = sin(angle), st0 = cos(angle)
ftst ; compare st0 to 0.0
push ax ; ...can't divide by zero!
fstsw ax
sahf
pop ax
jz .alternativeSlope ; ZF set if st0 == 0.0
fdivp ; st0 = tan(angle)
jmp .saveSlope
.alternativeSlope:
fldlg2 ; st0 = log(10)2
.saveSlope:
fstp dword [ball.slope] ; save slope
.setDirection:
bts word [ball.direction], d.bit.Y
.end:
ret
; void nextPosition(void)
;
; Update the ball's X and Y values with respect to its slope and direction.
nextPosition:
push ax
fld dword [ball.slope] ; st0 = slope
fld st0 ; push another copy of slope
fabs ; st0 = abs(slope)
fld1 ; push 1.0
fcompp ; compare 1.0 and abs(slope) ; pop twice
fstsw ax
sahf
jc .use_Y_axis ; carry set if 1.0 < abs(slope)
.use_X_axis: ; X +- 1 ; Y = m(X - xOrigin) + yOrigin
bt word [ball.direction], d.bit.X
jnc .x_dec
inc word [ball.x]
jmp @f
.x_dec:
dec word [ball.x]
@@:
fild word [ball.x] ; push X
fisub word [ball.xOrigin] ; st0 = X - xOrigin
fmulp ; st0 *= m
fiadd word [ball.yOrigin] ; st0 += yOrigin
fistp word [ball.y] ; [ball.y] = st0
jmp .end
.use_Y_axis: ; Y +- 1 ; X = (Y - yOrigin)/m + xOrigin
bt word [ball.direction], d.bit.Y
jnc .y_dec
inc word [ball.y]
jmp @f
.y_dec:
dec word [ball.y]
@@:
fild word [ball.y] ; push Y
fisub word [ball.yOrigin] ; st0 = Y - yOrigin
fdivrp ; st0 /= m
fiadd word [ball.xOrigin] ; st0 += xOrigin
fistp word [ball.x] ; [ball.x] = st0
.end:
pop ax
ret