AtoB is a unit vector from the center of A to the center of B.
So if A was at (0, 0) and B was at (30, 0), AtoB would be (1, 0).
If A was at (10, 6) and B was at (16, 6), AtoB would still be (1, 0).
If A was at (0, 4) and B was at (0, 10) then AtoB would be (0, 1).
et cetera.
*
Do you have an understanding of vectors? They're essential for physics simulations, and especially collisions. Gamedev has a section on them:
http://www.gamedev.net/reference/list.asp?categoryid=28#260
Especially look over the sections on vectors and physics.
Vectors let you express a value as a magnitude and a direction. A 2D vector has two components (x, y) ; a 3D vector has 3 components (x, y, z).
Most programmers define a vector class:
Type VECTOR ' 3D Vector datatypex as single ' Each component is a single precision floaty as singlez as singleEnd Type
Then you could reference the components declare a vector (v) and reference the components as v.x, v.y, v.z.
*
To take the magnitude of v:
Magnitude = SQR(v.x*v.x + v.y*v.y + v.z*v.z)
*
Adding vectors is simple, just add the components.
' c = a + bc.x = a.x + b.xc.y = a.y + b.y
Subtraction works exactly the same way.
*
The dot product of two vectors is a scalar value:
DotProduct = a.x*b.x + a.y*b.y + a.z*b.z
( The cross product of two vectors is a bit more complex, and returns a vector that is perpendicular to the original two. You won't need it for collisions. But it is detailed in the tutorials I linked to above. )
*
You can also scale a vector by a value, simply by multiplying each component by that value:
' b = v*kb.x = v.x * kb.y = v.y * kb.z = v.z * k
*
To normalize a vector is to set its magnitude to exactly one. A vector with magnitude = 1 is a unit vector. Divide each component by the vector's magnitude:
' n = unit vector of vMagnitude = SQR(v.x*v.x + v.y*v.y + v.z*v.z)n.x = v.x / Magnituden.y = v.y / Magnituden.z = v.z / Magnitude
*
Velocity is a vector, speed is a scalar value -- so the values you have defined as velocities in your code are actually speeds.
Here's the useful vector functions:
Type VECTOR ' 3D Vector datatypex as single ' Each component is a single precision floaty as singlez as singleEnd TypeFUNCTION vecNormalize (v as VECTOR) AS VECTOR ' n = vector of magnitude 1 pointing in the same direction as v DIM AS SINGLE Magnitude = SQR(v.x*v.x + v.y*v.y + v.z*v.z) DIM AS VECTOR n n.x = v.x / Magnitude n.y = v.y / Magnitude n.z = v.z / Magnitude RETURN nEND FUNCTION FUNCTION vecAdd (a as VECTOR, B AS VECTOR) AS VECTOR ' c = a + b DIM AS VECTOR c n.x = a.x + b.x n.y = a.y + b.y n.z = a.z + b.z RETURN cEND FUNCTION FUNCTION vecSubtract (a as VECTOR, B AS VECTOR) AS VECTOR ' c = a - b DIM AS VECTOR c n.x = a.x - b.x n.y = a.y - b.y n.z = a.z - b.z RETURN cEND FUNCTION FUNCTION vecScale (v as VECTOR, k as single) AS VECTOR ' n = v*k DIM AS VECTOR n n.x = v.x *k n.y = v.y *k n.z = v.z *k RETURN nEND FUNCTIONFUNCTION vecDotProduct (a as Vector, b as vector) AS SINGLE RETURN a.x*b.x + a.y*b.y + a.z*b.zEND FUNCTION
ANd here's the same code from the first post,
using those functions. Last time I used overloaded operators, so it was hard to tell what was a vector and what was a scalar.
Using the
new functions, it should be easier to distinguish between the two.
'
This is the code that should be executed when a collision is detected.
' aCoords, bCoords : locations of objects a and b, stored as position vectors
' aMass, bMass: masses of a and b
' aVelocity, bVelocity: vectors representing the velocities of A and B.
' AtoB: unit vector from a to b.
DIM AS VECTOR AtoB = vecNormalize(vecSubtract(bCoords, aCoords))
DIM AS SINGLE c = vecDotProduct(AtoB, vecSubtract(aVelocity,bVelocity))
' Calculate the
new velocities
aVelocity = vecAdd(aVelocity, vecScale(AtoB, (-
2 * (bMass*c) / (aMass + bMass)))
bVelocity = vecAdd(bVelocity, vecScale(AtoB, (
2 * (aMass*c) / (aMass + bMass)))
Again, I'd highly recommend reviewing vectors, and how they relate to physics.