http://www.personal.psu.edu/users/n/c/ncc5012/ball/ball.html
I’m creating a simple game, and I’m having some trouble with the ‘physics’. The main object that the user will control is a simple ball (circle) and the platforms on which it will bounce are all rectangles, some oriented horizontally and some vertically. While collision detection for the ball and the sides of the platforms is working sufficiently, the corners are quite problematic. I’m to the point now where I can successfully identify when the ball has collided with any of the platform corners. I do this simply by comparing the anticipated distance between the ball’s center and the corners of the platform with the radius of the ball. For each corner, top-left, top-right, bottom-right, bottom-left (1, 2, 3, and 4, respectively), I’ve included additional if statements so the x and y velocities can also factor into the action taken by the ball.
Anyway, I can’t decide what rules should dictate how the ball reacts when hitting a corner. Take this specific example (if I understand this, then I can probably apply it to everything else without much difficulty)…
The ball is travelling downward (positive y velocity) and to the right (positive x velocity). The ball collides with the upper left corner of a platform. Now, while there would be many outcomes to this situation in the real world, I’ve narrowed it down to two or three for the game:
-
The ball reacts as if it has hit the left side of the platform - basically, some friction is applied and the x velocity is reversed.
-
The ball reacts as if it has hit the top side of the platform - some friction is applied and the y velocity is reversed, bouncing the ball across the top of the platform.
-
[optional, I’m not sure if I even want to include this possibility in the game] The ball reacts as it would (I think) in real life if it directly hit the corner of a table - experiencing some friction and then reversing both the x and y velocities, sending the ball away from the platform on both axes.
The relevant variables I have to work with are:
mc_ball._insert_property_here - constant and/or current properties of the ball
xVel - x velocity (before collision detection)
yVel - y velocity (before cd)
moveBallToX - anticipated x position (before cd)
moveBallToY - anticipated y position (before cd)
minX, maxX, minY, maxY - properties of the platform that is being checked
Once again, my progress so far can be seen here:
http://www.personal.psu.edu/users/n/c/ncc5012/ball/ball.html
There are still many, many, many bugs to work out, and at the moment, when you come in contact with a corner, no actions are taken because the if statements are left empty, so that’s obviously a problem in the current version. The controls for the game are the arrow keys:
up - jump
left/right - roll left/right
down - apply hard friction
Thanks for any help! Merry Christmas!