10-20-2022, 12:30 AM
I finally got it. Had to make use of _ATAN2 and that made a huge difference. I didn't know what it meant but it's a great command for this. Without using degrees it captures the radian value of any x,y position and you can add and subtract these radian values to get a reflection. That makes this simple. I threw out most of my code once I got this concept going.
Code: (Select All)
'vector reflection demo
'james2464
Screen _NewImage(800, 600, 32)
Const PI = 3.141592654#
Dim c(10) As Long
c(0) = _RGB(30, 30, 30)
c(1) = _RGB(255, 255, 255)
c(2) = _RGB(255, 255, 0)
c(3) = _RGB(255, 0, 0)
c(4) = _RGB(0, 255, 0)
c(5) = _RGB(0, 255, 255)
'0,0 origin
xx = 400
yy = 300
na = 45 'wall starting angle (mouse wheel controlled)
flag = 0
Do
_Limit 50
Cls
'=====================================================
mouseclick1 = 0
Do While _MouseInput
na = na + _MouseWheel * 5
Loop
mx% = _MouseX
my% = _MouseY
If mx% < xx - 400 Then mx% = xx - 400
If mx% > xx + 400 Then mx% = xx + 400
If my% < yy - 300 Then my% = yy - 300
If my% > yy + 300 Then my% = yy + 300
lc% = _MouseButton(1)
If lc% = -1 Then mouseclick1 = 1
x = 0 - xx + mx%
y = yy - my%
'=====================================================
h = _Hypot(-x, y)
'=====================================================
'origin lines
Line (0, yy)-(800, yy), c(0)
Line (xx, 0)-(xx, 600), c(0)
'=====================================================
'angled wall
a = (Sin(na * .017453292)) * 200
b = (Cos(na * .017453292)) * 200
Line (xx, yy)-(xx + b, yy + a), c(1)
Line (xx, yy)-(xx - b, yy - a), c(1)
'=====================================================
Circle (xx + x, yy - y), 10, c(2) 'point I
Line (xx, yy)-(xx + x, yy - y), c(2) 'line from origin to point I
'=====================================================
nx = Cos(na * (PI / 180)) 'normalize wall angle
ny = Sin(na * (PI / 180)) 'normalize wall angle
dx = -x * ny * -1: dy = y * nx: ndp = dx + dy
'dot product V.N - used to find distance of N
'The distance of N is from the point of origin to the middle of line A
'line A is a line from point I to point R (parallel to the angled wall)
ndpx = Sin(na * (PI / 180)) * ndp
ndpy = Cos(na * (PI / 180)) * ndp
'=====================================================
'calculate point R
th1 = _Atan2(-y, x) 'radian value of ball (point I)
th2 = _Atan2(-ndpy, ndpx) 'radian value of line N
thd = th1 - th2 'find difference
th3 = th2 - thd 'subtract difference from line N
rx = Cos(th3) * h: ry = Sin(th3) * h 'point R position - th3 * length of point I to origin
Circle (xx + rx, yy + ry), 10, c(3) 'point R
Line (xx, yy)-(xx + rx, yy + ry), c(3) 'origin to point R
_Display
Loop Until flag = 1