# Collision Response - sphere/plane & finding plane normal.

1. Jul 2, 2013

### gogreengo

Following on from my (still not quite solved) question here: Calculating event time using gsl_poly_solve_quadratic

I'm now looking to calculate the collision response (new linear velocity vector) for the sphere after a collision with a line (rail).

The current code I'm extending has two cases of rail collision implemented, that is for the N & S rails and the E & W rails such that the collision response is just a case of inverting one of either the X or the Y such that the pseudo code for new velocity for the N and S rails becomes (ignoring any damping coefficient):

Vector newV = Vector(oldV.x, -oldV.y);

and for the East and West rails:

Vector newV = Vector(-oldV.x, oldV.y);

I guess that these are obvious special cases for the rails that are either parallel or perpendicular to the X and Y axis.

In my question linked above, I'm extending this implementation to include 'jaws' - rails at the pockets that are at ±45° to the N,E,S &W rails. This has two parts, predicting the time of a collision (as discussed in the linked question) and then the handling of the collision event.

So my question is how to calculate the Collision Response for the South West pocket East Jaw as shown in the diagram below:

Let's assume that I want to detect and respond to a collision anywhere on that dashed red line (the infinite east jaw line). I've found various interpretations of an equation to use and in pseudo code is (EQ1):

reflected = 2 * plane.normal * (plane.normal * sphere.velocity)
sphere.velocity -= reflected

All looks simple except I'm not getting the expected results so I'm thinking I must be interpreting that equation incorrectly. Actually I'm pretty sure I'm not calculating the plane normal correctly.

1. Is EQ1 shown above the correct way to determine the collision reaction I'm after?
2. Am I correctly calculating the plane normal by doing the following:

Taken from how-do-i-calculate-the-normal-vector-of-a-line-segment

I'm trying to use, if dx=x2-x1 and dy=y2-y1, then the normals are (-dy, dx) and (dy, -dx).

In this case given my definition of the line in the diagram above that will give:

x1 = 0;
y1 = -pocket_width;

x2 = pocket_width;
y2 = 0;

dx=x2-x1;
dx=pocket_width - 0;
dx=pocket_width;

dy=0 -(-pocket_width)
dy=pocket_width

∴ the normals are (-pocket_width, pocket_width) and (pocket_width, -pocket_width)

I assume these are both perpendicular to the line in each direction so one will be the one I want.

Is this the correct way to calculate the plane.normal EQ1 above?
3. Is this a correct implementation of EQ1?

EQ1 = reflected = 2 * plane.normal * (plane.normal * sphere.velocity)
sphere.velocity -= reflected

Here I'm assuming that a vector3 * vector3 is in fact the cross product? I'm limited to implement this given the operators my Vector class provides and as such I get:

double dx = Table::CORNER_POCKET_WIDTH;
double dy = Table::CORNER_POCKET_WIDTH;

Vector plane_normal = Vector(dy, -dx, 0);

Vector ball_velocity = ball.getVelocity();

Vector reflected = plane_normal.cross(plane_normal.cross(ball_velocity)) * 2;
newVelocity = ball_velocity - reflected;

Is that code equivalent to EQ1?
4. Is there a better way to do what I want? Given that all the jaws will be ±45° to one of the rails is there a similar special case operation I can do like is being done for the N,E,S & W rails?

Sorry for so much information and so many questions..

Any ideas are greatly appreciated.

Cheers,
Lark

Last edited: Jul 2, 2013
2. Jul 2, 2013

### Staff: Mentor

1.: It is
2.: Right. It does not matter which normal vector you use, they just differ by a sign and that sign cancels in the formula. You have to normalize that vector, however.

Right

4.: For 45°-collisions, the final velocity has a nice, simple formula. Just find out what happens to a ball moving in x-direction, and a ball moving in (negative) y-direction. The general motion is just a sum of both motions, and the collision is linear (you can add them). That explanation is more complicated than the final formula now.

3. Jul 2, 2013

### gogreengo

@mfb, excellent, many thanks!