Angle Collisions - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10) +---- Thread: Angle Collisions (/showthread.php?tid=972) |
RE: Angle Collisions - Pete - 10-20-2022 Looks more like Gary from Sponge Bob. Pete RE: Angle Collisions - james2464 - 10-20-2022 (10-20-2022, 02:36 AM)bplus Wrote: Congrats James, Thanks! Now I need to figure out the actual collision part. RE: Angle Collisions - bplus - 10-20-2022 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. RE: Angle Collisions - james2464 - 10-20-2022 Thanks for this info...I was hoping this next part wouldn't be as difficult as the reflection. I'm going to keep this really simple for now. I'll start with a single angled wall (set by mouse) and then let a ball drop onto it. And see if I can get the collision point worked out at any angle. RE: Angle Collisions - bplus - 10-20-2022 Here is my circle intersect line segment detector, thanks to james2464 Random Line Segment Container Problem which I had to create to detect when a ball enters a line segment: Code: (Select All) ' return count of how many points overlap segment RE: Angle Collisions - james2464 - 10-21-2022 I have a line detector worked out using the same vector info from the reflection. There are two detections involved. One is to detect how far the ball is from the side of the line, and the other checks the distance from the end of the line. You can see these lines by holding the mouse button, or just check the collision without any lines. The side checking method uses the vector dot product, which was a calculation already used to get the reflection angle. If this distance (line N in program notes) is less than the ball radius, there is a collision with the line. Code: (Select All) 'vector reflection and line detection demo RE: Angle Collisions - Pete - 10-21-2022 Looks like a good start of a billiards game. I would assume round on round surface collisions with the balls instead of just a ball into a cushion, would require even more calculations. Pete RE: Angle Collisions - bplus - 10-21-2022 Yeah! can't wait to see it applied to your random container. RE: Angle Collisions - OldMoses - 10-22-2022 Looks like everything needed is there, incoming vector, relfection vector, line detection and edge detection. Nice job. RE: Angle Collisions - james2464 - 10-22-2022 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. |