• entries
72
38
• views
22334

# for all to enjoy

160 views

I'm do some cleaning of my code while thinking up the way to link all my current objects. But this isn't really about that so much. It is about the code taking out because its now been rewritten somewhere else.

This is my code to find the time of collision for two spheres. It solves the time, then moves objects to that time. When the two are at that point, make a vector from the two centers. Apply that as the new direction vector for each object, reversing when needed. Normalizes them, and then moves them the remaining time. It also marks them as moved because I was getting some strange results at first so I took some precautions. Anyways:
void	ObjectManager::CollideSS(DWORD ref1, DWORD ref2, float tcount){	Vector3	i1 = pIndex[ref1].internal;	Vector3 i2 = pIndex[ref2].internal;	Vector3 d1 = pIndex[ref1].direction;	Vector3 d2 = pIndex[ref2].direction;	float	s1 = pIndex[ref1].scalar / (1.0f/tcount);	float	s2 = pIndex[ref2].scalar / (1.0f/tcount);	Vector3 s = (d1 * s1) + (d2 * s2);	Vector3 e = i2 - i1;	float	r = pIndex[ref1].radius + pIndex[ref2].radius;	float	solve = ((e*s)*(e*s)) + (r*r) - (e*e);	if(solve < 0.0f)		return;		// no collision, or collision at 0	float	t = e*s - sqrt(solve);	// move internals to sync with t	// if solve == 0, then collision is at start.	// so (sN * t) == 0, the new vector will be	// calculated and moved for all of time	i1 += (d1 * (s1 * t));	i2 += (d2 * (s2 * t));	Vector3 D1 = i2 - i1;	// vector from 1 to 2 at collision	Vector3 D2 (-D1);		// exact oppisite for other sphere	d1 = -D1;	d2 = -D2;	d1 = Normalize3(d1);	d2 = Normalize3(d2);	// should prolly check for more collisins here, but oh well	// move the stuff the rest of the way	i1 += (d1 * (s1 * (1.0f - t)));	i2 += (d2 * (s2 * (1.0f - t)));	pIndex[ref1].internal = i1;	pIndex[ref2].internal = i2;	pIndex[ref1].direction = d1;	pIndex[ref2].direction = d2;	pIndex[ref1].moved = true;	pIndex[ref2].moved = true;}

Annnnnd...

Here is it's daddy. It started as just testing to see if we were moving at the object. But it was my first attempt at collision tests, so its pretty ugly I bet. :) Anyways, it was pretty fast for me. So another Ctrl-X from my file:
Vector3		ObjectManager::TestObjectRay(DWORD thisref, FLOAT tick){	Vector3		d (0.0f,0.0f,0.0f);		Vector3		testing (pIndex[thisref].internal);	Vector3		testdir (pIndex[thisref].direction);	FLOAT		testvel = pIndex[thisref].scalar / (1.0f/tick);	Vector3		versus;	Vector3		verdir;	FLOAT		vervel;	for(DWORD testRef = 0;testRef < m_dwNumWorldObjects;testRef++)		if( (testRef != thisref) && pIndex[thisref].moved == false )		// dont test vs self		{				versus = pIndex[testRef].internal;			verdir = pIndex[testRef].direction;			vervel = pIndex[testRef].scalar / (1.0f/tick);			Vector3	plot = testdir*testvel;			Vector3 tothis = versus-testing;	// vector from S1 to S2			// check to see if we are even close enough to			// hit this object in timeframe			// ran is the distance around ourself (thisref) to test for objects			// vst is the distance (if any) the 2nd object is moving			float	ran = Vector3Mag(plot) + pIndex[thisref].radius + pIndex[testRef].radius;			Vector3 vst = vervel * verdir;			// if versus is moving, and has not been moved			if(vervel && pIndex[testRef].moved == false)				ran += Vector3Mag(vst);			// throw in the versus movement							if(ran > Vector3Mag(tothis))	// traveling more than distance to test			{				// test of we are heading to this object				// dot product resulted in less than 0 = no				FLOAT resulted = Normalize3(plot)*Normalize3(tothis);				if(resulted >= 0.0f)				{					Vector3 t2 = testing + ( plot );					Vector3 v2 = versus + ( vst );					tothis = v2-t2;					if(Vector3Mag(tothis) < pIndex[testRef].radius + pIndex[thisref].radius)					{						if(pIndex[thisref].ColType == COL_SPHERE && pIndex[testRef].ColType == COL_SPHERE)                            CollideSS(thisref, testRef, tick);	// sphere on sphere collision						else if(pIndex[thisref].ColType == COL_SPHERE && pIndex[testRef].ColType == COL_PLANE)							CollideSP(thisref, testRef, tick);	// sphere on plane collision					}				}			}		}			return d;}

They both used the pIndex array. I could/should have kept going with this, because I was getting nice results. Now I'm redesigning stuff, and decided I really am shooting for a slightly more complicated scene (than pong).

So there is my gift to all of the people who have never seen collision code. It should be helpful for someone who does not know how to start the tests.

Blah blah back to work, I'll let you know if I find anything else juicy.

There are no comments to display.

## Create an account

Register a new account