05-24-2022, 12:58 AM
B+, I've been using your math code from this game to make an example of wall reflection. It's not perfect, but I thought I would show you what I made using this code. If you have any suggestions or comments, I'm all ears. Thanks for making this game!
Code: (Select All)
'Walls Reflection Example by SierraKen
'Reflection math from B+'s Air Hockey.
Screen _NewImage(800, 600, 32)
Const pr = 20 '
Const pr2 = 2 * pr '
start:
Cls
cx = 350: cy = 250: r = 20
c = _RGB32(0, 255, 0)
rr = 20
cc = _RGB32(255, 0, 0)
speed = 5
Dim pao
Randomize Timer
pao = _Pi(1 / 10) * Rnd
If Rnd < .5 Then pa = _Pi(.5) Else pa = _Pi(1.5)
If Rnd < .5 Then pa = pa + pao Else pa = pa - pao
_Title "Reflection Walls Example - Press Space Bar to reset."
Do
_Limit 100
a$ = InKey$
If a$ = " " Then GoTo start:
If a$ = Chr$(27) Then End
Line (100, 100)-(700, 500), _RGB32(255, 255, 255), B
Do While _MouseInput 'mouse status changes only
x = _MouseX
y = _MouseY
Loop
fillCircle x, y, rr, cc
If Sqr((x - cx) ^ 2 + (y - cy) ^ 2) < (pr + pr2) Then
pa = _Atan2(y - cy, x - cx)
pa = _Pi(1) - pa
GoTo go:
End If
go:
cx = cx + speed * Cos(pa)
cy = cy + speed * Sin(pa)
If cx > 675 Then pa = -pa: speed = -speed
If cx < 125 Then pa = -pa: speed = -speed
If cy > 475 Then pa = -pa
If cy < 125 Then pa = -pa
fillCircle cx, cy, r, c
_Display
Cls
Loop
'from Steve Gold standard
Sub fillCircle (CX As Integer, CY As Integer, R As Integer, C As _Unsigned Long)
Dim Radius As Integer, RadiusError As Integer
Dim X As Integer, Y As Integer
Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
If Radius = 0 Then PSet (CX, CY), C: Exit Sub
Line (CX - X, CY)-(CX + X, CY), C, BF
While X > Y
RadiusError = RadiusError + Y * 2 + 1
If RadiusError >= 0 Then
If X <> Y + 1 Then
Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
End If
X = X - 1
RadiusError = RadiusError - X * 2
End If
Y = Y + 1
Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
Wend
End Sub