|
Post by bplus on Oct 25, 2022 14:50:56 GMT -5
Here is way I use to do it for Pong, Breakout, Air Hockey, Pool???
Option _Explicit _Title "Standard Reflection in a Rectangle Container, spacebar to restart" ' b+ 2022-10-21 ' A quick demo of the wrong way I was doing bouncing balls and circles on rectangular borders ' Air Hockey, Ping Pong, Break Out games
Screen _NewImage(800, 600, 32) _ScreenMove 250, 50 Randomize Timer Dim Top, Left, Bottom, Right ' borders for rectangle Top = 10: Bottom = _Height - 10: Left = 10: Right = _Width - 10 Dim x, y, r, a, speed, dx, dy ' for ball x, y position moving with heading a and speed so dx, dy with radius r Dim i restart: x = _Width / 2: y = _Height / 2: speed = 5 r = 15 ' make ball radius (br) at least 2* speed a = Rnd * _Pi(2) ' setup up ball in middle of screen/container random heading = ba (ball angle) dx = speed * Cos(a): dy = speed * Sin(a) Color &HFFFFFFF, &HFF008844 Do Cls Line (Left, Top)-(Right, Bottom), &HFF000000, BF For i = 0 To r Step .25 Circle (x, y), i, &HFFFFFFFF Next x = x + dx ' test x, y is new ball position if dont run into wall If x < Left + r Then x = Left + r: dx = -dx If x > Right - r Then x = Right - r: dx = -dx y = y + dy If y < Top + r Then y = Top + r: dy = -dy If y > Bottom - r Then y = Bottom - r: dy = -dy _Display _Limit 60 Loop Until _KeyDown(27) Or _KeyDown(32) If _KeyDown(32) Then GoTo restart
|
|
|
Post by bplus on Oct 25, 2022 14:56:21 GMT -5
A more ideal way to reflect balls from border is this:
Option _Explicit _Title "Standard Reflection Fix, spacebar to restart" ' b+ 2022-10-24 ' A quick demo of the wrong way I was doing bouncing balls and circles on rectangular borders ' Air Hockey, Ping Pong, Break Out games
' try to fix Standard Refelction by backing up ball no more dx, dy! you have to chnge ball angle a each reflection
DefDbl A-Z Screen _NewImage(800, 600, 32) _ScreenMove 250, 50
Randomize Timer Const pi = _Pi Dim Top, Left, Bottom, Right ' borders for rectangle Top = 10: Bottom = _Height - 10: Left = 10: Right = _Width - 10 Dim x, y, r, a, speed ' for ball x, y position moving with heading a and speed so dx, dy with radius r Dim i restart: x = _Width / 2: y = _Height / 2: speed = 10 r = 20 ' make ball radius (br) at least 2* speed a = Rnd * 2 * pi ' setup up ball in middle of screen/container random heading = ba (ball angle)
Color &HFFFFFFF, &HFF008844 Do Color &HFFFFFFF, &HFF008844 Cls Line (Left, Top)-(Right, Bottom), &HFF000000, BF For i = 0 To r Step .25 Circle (x, y), i, &HFFFFFFFF Next x = x + speed * Cos(a) ' test x, y is new ball position if dont run into wall If x < Left + r Then While x < Left + r x = x + Cos(a + pi) y = y + Sin(a + pi) GoSub see Wend
a = pi - a End If If x > Right - r Then While x > Right - r x = x + Cos(a + pi) y = y + Sin(a + pi) GoSub see Wend a = pi - a End If
y = y + speed * Sin(a) If y < Top + r Then While y < Top + r x = x + Cos(a + pi) y = y + Sin(a + pi) GoSub see Wend a = -a End If If y > Bottom - r Then While y > Bottom - r x = x + Cos(a + pi) y = y + Sin(a + pi) GoSub see Wend a = -a End If
' keep a in 2* pi range While a < 0 a = a + 2 * pi Wend While a >= 2 * pi a = a - 2 * pi Wend _Display _Limit 60 Loop Until _KeyDown(27) Or _KeyDown(32) If _KeyDown(32) Then GoTo restart
see: Color &HFFFFFFF, &HFF008844 Cls Line (Left, Top)-(Right, Bottom), &HFF000000, BF For i = 0 To r Step .25 Circle (x, y), i, &HFFFFFFFF Next
Color &HFFFFFFFF, &HFF000000 Locate 8, 10: Print "Backing out ball from border:" Locate 10, 10: Print "angle:"; Int(_R2D(a)), "x:"; Int(x), "y"; Int(y) _Display _Delay 1.5 Return
This backs the ball out of the border in the opposite way it came to penetrate it, then readjusts the ball heading "a" for angle and sends it on it's way.
So what do you think, is it worth the extra effort to be a little more accurate with resetting the ball after it penetrates the boundary?
I am pretty sure I am talking to the dead but WTH? it's almost Halloween!
WTH? it's a nice demo anyway!
|
|
|
Post by charliejv on Oct 26, 2022 22:25:10 GMT -5
Yup, it is a nice demo!
|
|
|
Post by bplus on Oct 26, 2022 22:50:20 GMT -5
Oh hey Charlie, thanks, good to know someone still visits here
|
|
|
Post by charliejv on Oct 27, 2022 15:56:23 GMT -5
Yeah, in my mind, totally worth giving anything BASIC at least a resting/occasional heart beat to try and keep it from disappearing. Anything BASIC is good for all things BASIC, I cheer.
|
|