(10-22-2022, 10:29 PM)james2464 Wrote: Yeah I think the math is there now, but programming is a different story. So far I've failed to get this to work. The bounces sometimes go in the wrong direction, the ball goes through the line sometimes. The problem seems to be that the calculations are sometimes using wrong info, backwards +/- values etc so I'll need to sort all that out. One thing I'll be thrilled to get right is breaking up a frame. Sometimes the ball is moving at say 10 pixels per frame and it could go past the line by 7 pixels for example. I'd like to somehow correctly break that into two parts so that the ball bounces back those 7 pixels in the new direction. If that makes sense.
Makes absolute sense and backing a ball up is easy if you maintain a variable for the ball heading, say ba (in radians to avoid conversions for sin and cos)
Then
Code: (Select All)
while (intersect is reported as true) ' back up ball
bx = bx + ballSpeed * cos(ba - pi) ' reversed ball heading = ba +- pi
by = by + ballSpeed * sin(ba - pi) ' ditto
wend
On old games of Ping Pong, Break Out, Air Hockey, maybe even Pool, I did not back up balls making that old code less than perfect.
I use to add dx and dy to ball location and if that was beyond a boundary I'd just move the ball back inside the boundary x, or y not both (wrong) and then just change dx = -dx for left and right side and dy = -dy for upper or lower boundary. It works but crude.
Also never make ballSpeed > ball radius for best result max ball speed = .5 * ball radius, that way ball doesn't jump past any obstacle like a wall that is supposed to stop it.
Here is how I use to code bouncing a ball around inside a rectangular frame:
Code: (Select All)
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
b = b + ...