# Real-Time Collision Detection (The Morgan Kaufmann Series in Interactive 3-D Technology)

All
Stack Overflow 18

This Year
Stack Overflow 2

This Month
Stack Overflow 1

In order to implement more or less realistic bouncing it is not enough to have only positions and bounding boxes of colliding objects. You need to have some kind of kinematic history: to be able to tell how objects move

aftercollision you should also know how they were movingbeforethe collision.In simplest case, you want to have at least a

velocity (speed) vector(direction and magnitude) and, probably, a mass for every object. When collision is detected you may simply invert the direction of the velocity vector of one of the objects (for example, smaller and lighter one).The pseudocode will be something like:

This would not look very realistic, but is very simple to implement. For more realism you could use

momentum conservationprinciple orelastic collisions. For even more realistic bouncing, implementacceleration vectorand re-calculate both, velocity and acceleration after collision is detected. Having acceleration you may now add arbitrary forces influencing your objects (gravity, friction, maybe even electromagnetic forces etc.). Adding more attributes make it more interesting: for example adding elasticity and viscosity may be used to calculate deformations.As you see there is more in object interactions involved than just positions.

Additionally, collision detection and bouncing code could (and should) be decoupled. That means that you would want to implement them in different parts of code (different set of functions and classes): collision subsystem will detect the collision event, providing information like: whether collision happened or not and if yes, by how far objects currently intersect with each other. After that, bouncing code, based on this information, will decide how to change objects traveling, while deformation code will decide how to change objects shapes etc. Thus, there will be no silly problems like which "if" statement in collision code came first decides the direction of bouncing.

Overall I would suggest you:

To look up some physics. For example:

To get this excellent book: Real-Time Collision Detection

To read through some popular questions on gamedev.stackexchange.com, for example on collision (sort by votes)

_{ Finally, you should know that DirectX 9 API is wildly obsolete, not supported and should not be used for new projects. It is notoriously hard to port D3DX* stuff to modern APIs. You may want to take a look at DirectX 11 and OpenGL 4, along with their support library ecosystems, or maybe even a good rendering or game engine. Specifically you may want to use some good math library (with vectors, matrices, linear algebra, geometry). }[1]https://www.amazon.com/Engine-Architecture-Third-Jason-Grego... [2]https://www.amazon.com/Real-Time-Rendering-Fourth-Tomas-Aken... [3]https://www.amazon.com/Real-Time-Collision-Detection-Interac...

12pm - I can't think of any immediately obvious, simple, way to do this in iOS. I'd look around for a package and if you're lucky there will be one! Otherwise it's just a case of plain hard programming.

You may possibly have to use beziers (Find the tangent of a point on a cubic bezier curve (on an iPhone)) to define the travel path. Or maybe, "simply" two half-circles.

From there use a "normal" slider concept to get the X position of the finger,

but just position the "y" value of the red ball, per your equations. That will work OK.For a better approach (

I doubt it will be necessary), the NEXT more complicated approach is: ALSO note the Y value of the finger. WHEN THE FINGER IS NEAR the "difficult" parts", THEN INSTEAD consider the Y value. Do you see what I mean?Finally the "ultimate solution" .. you need to get in to finding the "closest point on a curve" (i.e., give some point). This is classic game programming math. So follow something like

http://hal.archives-ouvertes.fr/docs/00/51/83/79/PDF/Xiao-DiaoChen2007c.pdf

Check out any of these classic books for that type of thing

http://www.amazon.com/Mathematics-Programming-Computer-Graphics-Edition/dp/1435458869

http://www.amazon.com/Real-Time-Collision-Detection-Interactive-Technology/dp/1558607323

However, IMO it would be "thoughtless engineering" .. .solution "A" or "B" is typically perfect!

The concept of

interval notationcomes up inbothMathematics and Computer Science. The Mathematical notation`[`

,`]`

,`(`

,`)`

denotes thedomain(orrange) of an interval.The brackets

`[`

and`]`

means:included,closed,The parenthesis

`(`

and`)`

means:excluded,open.An interval with mixed states is called

"half-open".For example, the range of consecutive integers from 1 .. 10 (inclusive) would be

notatedas such:Notice how the word

`inclusive`

was used. If we want toexcludethe end point but "cover" the same range we need to move the end-point:For both left and right edges of the interval there are actually 4 permutations:

How does this relate to Mathematics and Computer Science?

Array indexes tend to use a

differentoffset depending on which field are you in:one-based.zero-based, such as C, C++, Javascript, Python, while other languages such as Mathematica, Fortran, Pascal are one-based.These differences can lead to subtle

fence post errors, aka, off-by-one bugs when implementing Mathematical algorithms such as for-loops.## Integers

If we have a set or array, say of the first few primes

`[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]`

, Mathematicians would refer to the first element as the`1st`

absoluteelement. i.e. Using subscript notation to denote the index:_{1}= 2_{2}= 3_{10}= 29Some programming languages, in contradistinction, would refer to the first element as the

`zero'th`

relativeelement.Since the array indexes are in the range [0,N-1] then for clarity purposes it would be "nice" to keep the same numerical value for the range 0 .. N instead of adding textual

noisesuch as a`-1`

bias.For example, in C or JavaScript, to iterate over an array of N elements a programmer would write the common idiom of

`i = 0, i < N`

with the interval [0,N) instead of the slightly more verbose [0,N-1]:Mathematicians, since they start counting at 1, would instead use the

`i = 1, i <= N`

nomenclature but now we need to correct the array offset in a zero-based language.e.g.

Aside:In programming languages that are 0-based you might need a

kludgeof a dummy zero'th element to use a Mathematical 1-based algorithm. e.g. Python Index Start## Floating-Point

Interval notation is also important for floating-point numbers to avoid subtle bugs.

When dealing with floating-point numbers especially in Computer Graphics (color conversion, computational geometry, animation easing/blending, etc.) often times normalized numbers are used. That is, numbers between 0.0 and 1.0.

It is important to know the edge cases if the endpoints are

inclusiveorexclusive:Where M is some machine epsilon. This is why you might sometimes see

`const float EPSILON = 1e-#`

idiom in C code (such as`1e-6`

) for a 32-bit floating point number. This SO question Does EPSILON guarantee anything? has some preliminary details. For a more comprehensive answer see`FLT_EPSILON`

and David Goldberg's What Every Computer Scientist Should Know About Floating-Point ArithmeticSome implementations of a random number generator,

`random()`

may produce values in the range 0.0 .. 0.999... instead of the more convenient 0.0 .. 1.0. Proper comments in the code will document this as [0.0,1.0) or [0.0,1.0] so there is no ambiguity as to the usage.Example:

`random()`

colors. You convert three floating-point values to unsigned 8-bit values to generate a 24-bit pixel with red, green, and blue channels respectively. Depending on the interval output by`random()`

you may end up with`near-white`

(254,254,254) or`white`

(255,255,255).`+--------+-----+ |random()|Byte | |--------|-----| |0.999...| 254 | <-- error introduced |1.0 | 255 | +--------+-----+`

For more details about floating-point precision and robustness with intervals see Christer Ericson's Real-Time Collision Detection, Chapter 11

Numerical Robustness, Section 11.3Robust Floating-Point Usage.Not really a website, but this book Real-Time Collision Detection is well worth it for what you are looking for.

If you get the chance you should really check out the Collision Detection bible, "Real Time Collision Detection" if you plan on doing anything non-trivial. I'm not a professional game programmer and I understood and could apply the concepts in it with little trouble.

alt text http://msinilo.pl/blog/img/RealTimeCollisionDetection_13330/cover_rtcd.png

Amazon - Real Time Collision Detection

Basically, doing a set of line intersection tests is expensive no matter what. What you do is use things like Bounding Boxes (axis aligned or oriented) over your complex polygons. This will allow you to quickly do a worst case O(N^2) check of collision between each "object". You can then speed things up even further by using spatial trees (Binary Partitioning or QuadTrees) by only checking intersections of objects close to eachother.

This allows you to prune many, many collision tests. The best optimization is not doing something at all. Only once you have a collision between bounding boxes do you do your expensive line intersections to determine if the objects truly do intersect or not. This allows you to scale the number of objects up while still keeping the speed reasonable.

Real-Time Collision Detection by Christer Ericson could be a good choice for you since it covers many basic geometry intersection tests. Take a look at its table of contents.