## Collision Response Question

Member
Posts: 196
Joined: 2003.10
Post: #1
Ok, so I need some help with the math of a collision response. The detection part is finished and works properly - but that's the easy part. The following is a diagram:

Basically the ball has a direction vector of Vector A, and it's going to bounce off of the inside of the circle. I understand that it needs to reflect off the vector drawn between the large circle's origin and the point of collision (Vector B), thus becoming Vector C. I think Vector D gets involved somewhere in the calculations - that's why I've included it.

What's the easiest way to do this? Seems a bit complicated, but I'm sure there's an easy solution to this one... Thanks in advance.
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
maybe try and implement this with the new rational trigonometry,
http://web.maths.unsw.edu.au/~norman/pap...apter1.pdf
Just a thought, it might make things easier.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Member
Posts: 78
Joined: 2002.06
Post: #4
Or more succinctly:

perpendicular = (A â€¢ B) Ã— B
parallel = A â€“ perpendicular

C = parallel Ã— friction + perpendicular Ã— â€“bounce

where friction and bounce > 0 and < 1
and B is a unit vector
Member
Posts: 196
Joined: 2003.10
Post: #5
Ok, the Mathematica page looks great - but very confusing to someone who hasn't taken math courses in 6 years.

Will: that explanation looks decent, but I have to wrap my mind around it for a bit. It's important to me that I *understand* this - makes debugging easier. Assuming there is no friction and the bounce is constant, would I use values of 1.0 for each?

And when you suggest to multiply floats by vectors, do you mean simply to multiply the value by both elements of the vector?

Excuse my math skills, or lack thereof. It's been a long time since high school math, and I find this all a bit confusing.
Member
Posts: 78
Joined: 2002.06
Post: #6
Yes when you multiply vectors by floats just multiply both elements of the vector. A bounce of 1 and the ball never stops bouncing. A bounce greater than 1 and it gets exponentially faster until something goes wrong.

The maths above splits the balls velocity vector into two orthogonal components, one along the vector B perpendicular (pp) to the collision point, and one along the vector D parallel (p) to the collision point. It is easy to then combine these back to get the reflection vector.

Member
Posts: 196
Joined: 2003.10
Post: #7
Wow, thanks Will! Great diagram - what did you use to make it? I grow quickly tired of Sketch.app!

I'll try to implement that tonight.
Member
Posts: 208
Joined: 2005.04
Post: #8
I was just reading on this page that the cross product can only be computed for 3D vectors. Since this is 2D space, what does (A â€¢ B) Ã— B mean (how do you calculate it)?
Sage
Posts: 1,403
Joined: 2005.07
Post: #9
A dot B
the dot product is
a.x*b.x + a.y*b.y
in 3D
a.x*b.x + a.y*b.y + a.z*b.z

an important property is that

a.b/|ab| = cos (the angle between a and b)

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Member
Posts: 208
Joined: 2005.04
Post: #10
unknown Wrote:A dot B
the dot product is
a.x*b.x + a.y*b.y
in 3D
a.x*b.x + a.y*b.y + a.z*b.z

an important property is that

a.b/|ab| = cos (the angle between a and b)

But what about the cross product between the resulting scalar and B? Do I just multiply each of B's components by the scalar?

For example, say the dot product turned out to be 3. Would I just stretch each of B's components by a factor of 3?
Sage
Posts: 1,403
Joined: 2005.07
Post: #11
Quote:But what about the cross product between the resulting scalar and B?
You can only do the cross product in 3D.

Quote:Would I just stretch each of B's components by a factor of 3?
That would give you (A â€¢ B) x B.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Member
Posts: 78
Joined: 2002.06
Post: #12
Technically it is not a cross product. Cross-products are only between vectors. It is just a multiply. So yes just multiply each of B's components by the scalar. So (A â€¢ B) Ã— B is just: B scaled to the magnitude of the dot product of A and B.

Quote:Wow, thanks Will! Great diagram - what did you use to make it? I grow quickly tired of Sketch.app!
thanks. I used Imprint, my new-as-yet-unreleased drawing program. http://will.thimbleby.net/draw If you are interested in beta-testing let me know.
Member
Posts: 208
Joined: 2005.04
Post: #13
my brother just told me it's probably better to write it like (Aâ€¢B)B in order to avoid confusion
Sage
Posts: 1,403
Joined: 2005.07
Post: #14
Well the x means multiply, and you can also write it like that or * or â€¢, the last one is probably quite confusing if your using it for dot product as well!

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Member
Posts: 196
Joined: 2003.10
Post: #15
ARIGHT! It works! Thanks so much, Will!

Code:
PHP Code:
`/* COLLISION RESPONSEGeneral mathematical idea:define vector B as vector from origin through point of collisionD = (A â€¢ B) Ã— Bparallel = A â€“ DC = parallel + D Ã— â€“bounce */    // Define 1 temporary vector for calculations ...vector A = [ball direction];// Calculate the point where the ball is colliding with the play areavector pointOfCollision = (vector){ ([ball position].x + (A.x * [ball radius])),                                    ([ball position].y + (A.y * [ball radius]))};// ... continue by defining 3 more temporary Vectors ...vector B = (vector){ (pointOfCollision.x - 250.0), (pointOfCollision.y - 250.0) };B = vectorNormalise(B);vector D = vectorMultiply(B, vectorDotProduct(A, B));vector parallel = vectorSubtract(A, D);// ... and finish by calculating the last "temporary" vector, which is the resulting vectorvector C = vectorAdd(parallel, vectorMultiply(D, -BOUNCE));C = vectorNormalise(C);// Apply the change to the ball object's direction vector[ball setDirection:C]; `

Binary (): http://www.owlnet.rice.edu/~ajm4979/BG.zip