This is a VERY basic sphere collision program I wrote. If you're looking for something more complex and better, get Sparky's Collision lib.
This program creates 2 randomly sized/colored spheres and lets you control one.
Copy & paste this code into VC++ and run it (takes no media files):
/*
Basic sphere collision, program written by Anthony Eden (Frostysnowman4@hotmail.com)
Feel free to use/distribute code :D
*/
#include <DarkGDK.h>
//Some of this code is borrowed from Sparky's dll example, but I fully understand what every line does.
void positionCameraToObject( int objId, int thirdPerson)
{
dbPositionCamera(dbObjectPositionX(objId),dbObjectPositionY(objId) + 5.0f,dbObjectPositionZ(objId));
dbRotateCamera(dbObjectAngleX(objId),dbObjectAngleY(objId),dbObjectAngleZ(objId));
if ( thirdPerson == 1 )
{
dbPitchCameraDown(25);
dbMoveCamera(-18);
}
}
double absoluteV(double input)
{
if (input < 0)
{
return (-1*input);
}
else
{
return input;
}
}
bool checkForSphereCollision(int sphereId1, double sphere1Diameter, int sphereId2, double sphere2Diameter)
{
double distance, dx, dy, dz;
dx = dbObjectPositionX(sphereId1) - dbObjectPositionX(sphereId2);
dy = dbObjectPositionY(sphereId1) - dbObjectPositionY(sphereId2);
dz = dbObjectPositionZ(sphereId1) - dbObjectPositionZ(sphereId2);
distance = sqrt((dx*dx) + (dy*dy) + (dz*dz));
if (distance <= (sphere1Diameter + sphere2Diameter)/2)
{
return true;
}
else
{
return false;
}
}
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
// set our random seed to a value from the timer, this will help
// to ensure each time we run our program the random values appear
// more random
dbRandomize ( dbTimer ( ) );
int sphereDiam1;
sphereDiam1 = dbRND(8) + 1;
int sphereDiam2;
sphereDiam2 = dbRND(8) + 1;
dbMakeObjectSphere ( 1, sphereDiam1, 30, 30);
dbMakeObjectSphere ( 2, sphereDiam2, 30, 30);
dbPositionObject ( 1, 10, 2, 0);
dbPositionObject ( 2, 10, 2, 20);
//random colors- yay!
dbColorObject ( 1, dbRgb ( dbRnd ( 255 ), dbRnd ( 255 ), dbRnd ( 255 ) ) );
dbColorObject ( 2, dbRgb ( dbRnd ( 255 ), dbRnd ( 255 ), dbRnd ( 255 ) ) );
//LUV THAT SPEC
dbSetObjectSpecularPower ( 1, 255 );
dbSetObjectSpecularPower ( 2, 255 );
// turn off ambient lighting for this object
dbSetObjectAmbient ( 1, 0 );
dbSetObjectAmbient ( 2, 0 );
//initial camera position
dbPositionCamera ( 10, 10, -20 );
dbFixObjectPivot(1);
dbFixObjectPivot(2);
while ( LoopGDK ( ) )
{
dbText ( 0, 0, "Basic sphere collision" );
dbText ( 0, 10, "Use the arrow keys to move:\n Up/back: move forward/backwards \n Left/right: turn left/right." );
dbText ( 0, 460, "Frostysnowman4@hotmail.com" );
if (dbRightKey() == 1){
dbYRotateObject(1, dbObjectAngleY (1) + 1.8f);
}
if (dbLeftKey() == 1){
dbYRotateObject(1, dbObjectAngleY (1) - 1.8f);
}
//check for "w" key
if (dbUpKey() == 1){
dbMoveObject(1, .1f);
if (checkForSphereCollision(1, sphereDiam1, 2, sphereDiam2) == true)
{
//changes the values BEFORE dbSync ( ) is called so you don't see the flicker of the sphere moving back to its pos before.
dbMoveObject(1, -.1f);
}
}
if (dbDownKey() == 1){
dbMoveObject(1, -.1f);
if (checkForSphereCollision(1, sphereDiam2, 2, sphereDiam2) == true)
{
//changes the values BEFORE dbSync ( ) is called so you don't see the flicker of the sphere moving back to its pos before.
dbMoveObject(1, +.1f);
}
}
positionCameraToObject(1, 1);
dbSync ( );
}
// before quitting delete our objects
dbDeleteObject (1);
dbDeleteObject (2);
}
One thing I found when creating this function is that the input for dbMakeObjectSphere (2rd arguement) is stated incorrectly as the radius. It is ACTUALLY the diameter.