Posts Tagged ‘physics’

Posted by Kent at 5 March 2010

Category: Uncategorized

Tags: , , ,

An AABB is an axis aligned bounding box. It’s mainly used in broadphase physics detection.

Assume this are the basic properties of an AABB.

struct AABB
{
	Point c;		// center point
	float r[3];	// halfwidths
};

Then we can use this simple test to check if there is an overlap between the bounding boxes.

bool testAABBAABB(const AABB &a, const AABB &b)
{
	float t;
	if ( Abs(a.c[0] - b.c[0]) > (a.r[0] + b.r[0]) return false;
	if ( Abs(a.c[1] - b.c[1]) > (a.r[1] + b.r[1]) return false;
	if ( Abs(a.c[2] - b.c[2]) > (a.r[2] + b.r[2]) return false;
	// We have an overlap
	return true;
};

Popularity: 1% [?]

Posted by Kent at 13 January 2007

Category: Dev, Interessant, Mathematics

Tags: , , , , ,

Collision Detection

To determine if two spheres are colliding, we take the sum of the radiuses and compare it with the length from the centers of the spheres. If the lenght is smaller than the sum of the radiuses, we have a collision.

Difference vector (the length is the distance between those two spheres):
vec{d} = s1.pos - s2.pos

Then the length is computed:
distance = vec{d}.length = sqrt{vec{d}.x^2 + vec{d}.y^2 + vec{d}.z^2}

Sum of the radiuses:
sumradius = s1.radius + s2.radius

If distance<sumradius the we have a collision to take care of.

Collision Response

This is the little more trickier part, but with some basics explained, it should be pretty straight forward.

First, find the vector which will serve as a basis vector (x-axis), in an arbiary direction. It have to be normalized to get realistic results.
vec{x} = s1.pos - s2.pos
vec{x}.normalize()

Then we calculate the x-direction velocity vector and the perpendicular y-vector.
vec{v1} = s1.vel
x1 = x.dot(vec{v1})
vec{v1x} = vec{x} * x1
vec{v1y} = vec{v1} - vec{v1x}
m1 = s1.mass

Same procedure for the other sphere.
vec{x} = vec{x}*-1
vec{v2} = s2.vel
x2 = vec{x}.dot(vec{v2})
vec{v2x} = vec{x} * x2
vec{v2y} = vec{v2} - vec{v2x}
m2 = s2.mass

Then we mix and play around with some of Newtons laws to obtain a formula for the speed (in vector format) after the collision.
s1.vel = vec{v1x}{(m1-m2)/(m1+m2)} + vec{v2x}{(2*m2)/(m1+m2)} + vec{v1y}
s2.vel = vec{v1x}{(2*m1)/(m1+m2)} + vec{v2x}{(m2-m1)/(m1+m2)} + vec{v2y}

And it actually works! :)

Sample application:

Source code in subversion repository.

Popularity: 20% [?]