Collision Response Question - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Game Programming Fundamentals (/forum-7.html) +--- Thread: Collision Response Question (/thread-4949.html) Pages: 1 2 3 Collision Response Question - blobbo - Oct 2, 2005 03:05 PM 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. Collision Response Question - OneSadCookie - Oct 2, 2005 03:14 PM http://mathworld.wolfram.com/Reflection.html Collision Response Question - unknown - Oct 2, 2005 03:21 PM maybe try and implement this with the new rational trigonometry, http://web.maths.unsw.edu.au/~norman/papers/Chapter1.pdf Just a thought, it might make things easier. Collision Response Question - willThimbleby - Oct 2, 2005 03:39 PM 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 Collision Response Question - blobbo - Oct 2, 2005 08:12 PM 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. Collision Response Question - willThimbleby - Oct 3, 2005 01:34 AM 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. Collision Response Question - blobbo - Oct 3, 2005 07:02 AM 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. Collision Response Question - Andrew - Oct 3, 2005 08:08 AM 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)? Collision Response Question - unknown - Oct 3, 2005 08:17 AM 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) Collision Response Question - Andrew - Oct 3, 2005 08:37 AM 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? Collision Response Question - unknown - Oct 3, 2005 09:45 AM 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. Collision Response Question - willThimbleby - Oct 3, 2005 10:48 AM 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. Collision Response Question - Andrew - Oct 3, 2005 10:31 PM my brother just told me it's probably better to write it like (Aâ€¢B)B in order to avoid confusion Collision Response Question - unknown - Oct 4, 2005 05:06 AM 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! Collision Response Question - blobbo - Oct 5, 2005 09:19 PM 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