English 中文(简体)
2D Ball Collisions with Corners
原标题:

I m trying to write a 2D simulation of a ball that bounces off of fixed vertical and horizontal walls. Simulating collisions with the faces of the walls was pretty simple--just negate the X-velocity for a vertical wall or the Y-velocity for a horizontal wall. The problem is that the ball can also collide with the corners of the walls, where a horizontal wall meets with a vertical wall. I have already figured out how to detect when a collision with a corner is occurring. My question is how the ball should react to this collision--that is, how its X and Y velocities will change as a result.

Here s a list of what I already know or know how to find:

  • The X and Y coordinates of the ball s center during the frame when a collision is detected
  • The X and Y components of the ball s velocity
  • The X and Y coordinates of the corner
  • The angle between the ball s center and the corner
  • The angle in which the ball is traveling just before the collision
  • The amount that the ball is overlapping the corner when the collision is detected

I m guessing that it s best to pretend that the corner is an infinitely small circle, so I can treat a collision between the ball and that circle as if the ball were colliding with a wall that runs tangent to the circles at the point of collision. It seems to me that all I need to do is rotate the coordinate system to line up with this imaginary wall, reverse the X component of the ball s velocity under this system, and rotate the coordinates back to the original system. The problem is that I have no idea how to program this.

By the way, this is an ideal simulation. I m not taking anything like friction or the ball s rotation into account. I m using Objective-C, but I d really just like a general algorithm or some advice.

问题回答

As you say you can treat the corner as a circle with infinitely small radius. The normal of the collision plane in this case is given by a unit vector from the contact point to the center of the ball:

float nx = ballX - cornerX;
float ny = ballY - cornerY;
const float length = sqrt(nx * nx + ny * ny);
nx /= length;
ny /= length;

to reflect the velocity vector you do this:

const float projection = velocityX * nx + velocityY * ny;
velocityX = velocityX - 2 * projection * nx;
velocityY = velocityY - 2 * projection * ny;

If it s a sharp (without any fillet) right-angled corner it will act as a retroreflector and bounce it back along the path it came in on.





相关问题
c# wpf 2d graphics - Looking for tutorial / examples [closed]

Could you please point me to a good C# tutorial for drawing 2d graphics like Ellipse and Rectangle (that inherit from Shape) on a Canvas using WPF ? I m also interested later to click on shapes and ...

How to multiply two sprites in SpriteBatch Draw XNA (2D)

I am writing simple hex engine for action-rpg in XNA 3.1. I want to light ground near hero and torches just as they were lighted in Diablo II. I though the best way to do so was to calculate field-of-...

2D Ball Collisions with Corners

I m trying to write a 2D simulation of a ball that bounces off of fixed vertical and horizontal walls. Simulating collisions with the faces of the walls was pretty simple--just negate the X-velocity ...

2D Mathematics Geographical Directions

I am trying to check the location of a point(px, py) on 2D graph in relation to a line segment (lx1, ly1) (lx2, ly2), using logic of North South East West directions. The logic I have implemented is ...

Perpendicular on a line from a given point

How can I draw a perpendicular on a line segment from a given point? My line segment is defined as (x1, y1), (x2, y2), If I draw a perpendicular from a point (x3,y3) and it meets to line on point (x4,...

How to select a line

So I m trying to figure out how to implement a method of selecting lines or edges in a drawing area but my math is a bit lacking. This is what I got so far: A collection of lines, each line has two ...

How do I generate a contour graph?

How do I generate a contour graph like this: contour http://www.fz-juelich.de/vislab/software/gsharp/Gsharp/userguide/interpolate/ex6.gif It s easy enough if the points are on a regular grid, but ...

Mapping points from Euclician 2-space onto a Poincare disc

For some reason it seems that everyone writing webpages about Poincare discs is only concerned with how to represent lines and measure distances. I d like to morph a collection of 2D points (as ...

热门标签