10-18-2022, 10:47 PM
(10-18-2022, 08:20 PM)OldMoses Wrote:Works great! Thanks for giving this a go. I'm still trying to figure it out but I think I'll get there. Right now I'm trying to get the full 360 degree rotation but I did get the reflection to work at least. Cheers!(10-16-2022, 12:17 AM)james2464 Wrote: This video explains the reflection of a vector, but I don't know what 'n' is. At 10:27 he says "don't forget if n is unit length you know that n.n is 1 and you can cross that out". But there are still more n's in the formula and I can't figure out what they are supposed to represent. He just said n=1 !! Or n.n = 1 anyway. I just wish there were numbers involved instead of just letters. That would be a huge help.
https://youtu.be/naaeH1qbjdQ
I took a shot at doing a test bed program that uses the video procedure.
This is what I came up with, that is mostly vector based.
Code: (Select All)TYPE V2
x AS SINGLE
y AS SINGLE
END TYPE
DIM AS V2 wall, ball, reflec, orth, tempv
wall.x = -1: wall.y = 2
orth.x = -wall.y: orth.y = wall.x
SCREEN _NEWIMAGE(600, 600, 32) ' setup screen & display grid
WINDOW (-300, 300)-(300, -300)
DO UNTIL x% > 300
IF x% = 0 THEN c~& = &HFF00FF00 ELSE c~& = &H7F7F7F7F
LINE (-300, x%)-(300, x%), c~&
LINE (-300, -x%)-(300, -x%), c~&
LINE (x%, 300)-(x%, -300), c~&
LINE (-x%, 300)-(-x%, -300), c~&
x% = x% + 50
LOOP
back& = _COPYIMAGE(0)
DO
WHILE _MOUSEINPUT: WEND ' get inputs
ball.x = PMAP(_MOUSEX, 2): ball.y = PMAP(_MOUSEY, 3)
IF _KEYDOWN(18432) THEN ' up arrow
d! = _ATAN2(wall.y, wall.x)
d! = d! + (_PI / 180)
wall.x = COS(d!): wall.y = SIN(d!)
orth.x = -wall.y: orth.y = wall.x
END IF
IF _KEYDOWN(20480) THEN ' down arrow
d! = _ATAN2(wall.y, wall.x)
d! = d! - (_PI / 180)
wall.x = COS(d!): wall.y = SIN(d!)
orth.x = -wall.y: orth.y = wall.x
END IF
CLS
_PUTIMAGE , back&
R2_Norm wall, wall, 200 ' draw wall legs 200 long
R2_Norm orth, orth, 50 ' draw orthogonals 50 long
LINE (wall.x, wall.y)-(-wall.x, -wall.y)
LINE (orth.x, orth.y)-(-orth.x, -orth.y), &HFF0000FF
CIRCLE (ball.x, ball.y), 30 ' draw incoming ball & vector
LINE (ball.x, ball.y)-(0, 0)
R2_Norm orth, orth, 1 ' reset orthogonal to unit length
R2_Norm tempv, orth, DotP(ball, orth) * 2 ' get ball projection to orthogonal * 2
reflec.x = tempv.x - ball.x ' compute reflection vector
reflec.y = tempv.y - ball.y
LINE (0, 0)-(reflec.x, reflec.y), &HFFFF0000 ' move reflection to impact point
CIRCLE (reflec.x, reflec.y), 30, &HFFFF0000 ' show reflected ball
_LIMIT 100
_DISPLAY
LOOP UNTIL _KEYDOWN(27)
_FREEIMAGE back&
END
FUNCTION DotP (a AS V2, b AS V2)
DotP = a.x * b.x + a.y * b.y
END FUNCTION 'DotP
SUB R2_Norm (re AS V2, v AS V2, scalar AS INTEGER)
x! = v.x: y! = v.y
m! = _HYPOT(x!, y!)
IF m! = 0 THEN
re.x = 0: re.y = 0
ELSE
re.x = (x! / m!) * scalar
re.y = (y! / m!) * scalar
END IF
END SUB 'R2_Norm