10-20-2022, 02:52 PM
OK a ball with a heading of ba (Radians or Degrees) moves like this: (ba = ball Angle)
newX = oldX + speed * Cos(ba in Radians = _D2R(ba in degrees))
newY = oldY + speed * Sin(ba in Radians = _D2R(ba in degrees))
or
dx = change in x = Cos(ba in Radians = _D2R(ba in degrees))
dy = change in y = Sin(ba in Radians = _D2R(ba in degrees))
and
newX = oldX + dx
newY = oldY + dy
When ball is a radius away from a rectangular wall it gets reflected off wall such that:
if wall is left side dx = -dx
if wall is right side dx = -dx
if wall is top dy = - dy
if wall is bottom dy = -dy
That's easy to code and why Ping Pong so easy to code.
But you need to update ball heading too, ba = _Atan2(dy,dx), if you use ba for collisions with say other balls.
A little more tricky with _Atan2 that returns an angle in Radians and between -Pi and Pi usually.
newX = oldX + speed * Cos(ba in Radians = _D2R(ba in degrees))
newY = oldY + speed * Sin(ba in Radians = _D2R(ba in degrees))
or
dx = change in x = Cos(ba in Radians = _D2R(ba in degrees))
dy = change in y = Sin(ba in Radians = _D2R(ba in degrees))
and
newX = oldX + dx
newY = oldY + dy
When ball is a radius away from a rectangular wall it gets reflected off wall such that:
if wall is left side dx = -dx
if wall is right side dx = -dx
if wall is top dy = - dy
if wall is bottom dy = -dy
That's easy to code and why Ping Pong so easy to code.
But you need to update ball heading too, ba = _Atan2(dy,dx), if you use ba for collisions with say other balls.
A little more tricky with _Atan2 that returns an angle in Radians and between -Pi and Pi usually.
b = b + ...