Hi all... I posted a thread about a different Sparky issue, but this time it's even worse!
Whenever I try to use SC_ObjectCollision the game just freezes up when I crash. It seems that probably SC_ObjectCollision returns something different than SC_SphereCast, or something else. Either way, here's my code. I hope someone will help me.
#include "DarkGDK.h"
#include "SC_Collision.h"
#include <math.h>
#include <stdio.h>
#include <list>
#include <hash_map>
using namespace std;
using namespace stdext;
// pi and conversion function
#define PI 3.14159265358979323846
#define DEG2RAD(DEG) ((DEG)*((PI)/(180.0)))
#define RAD2DEG(RAD) (RAD*180.0/PI)
class Ship;
class ShipCollection {
public:
hash_map<int, Ship*> ShipList;
int addShip(int i, Ship* in_Ship) {
ShipList[i] = in_Ship;
return i;
}
// removeShip(int i);
};
class vect2 {
// Generic Vector Class
public:
float x, y; // seperate component coordinates
// ctor
vect2( float in_x = 0, float in_y = 0 ) {
x = in_x;
y = in_y;
}
void setXY( float i, float j ) { x = i; y = j; } // set (x, y)
void normalize() {
// normalize to unit vector (direction only)
// if length > 0 you divide each side by length
float l = getLength(); // get the current length
if ( l != 0 ) {
x /= l;
y /= l;
}
}
float getAngle() { return (float) RAD2DEG(atan( y / ((x == 0)? .0001 : x) )); } // returns angle of vector based on x, y
float getLength() { return (float) sqrt( ((x*x) + (y*y)) ); } // returns magnitude
// set x, y based on angle , magnitude
void setXYFromAM( float a, float m ) {
x = m * cos( DEG2RAD(a) );
y = m * sin( DEG2RAD(a) );
}
void scaleTo ( float s ) {
normalize();
x *= s;
y *= s;
}
vect2 operator+ ( vect2 v ) { return vect2( x + v.x, y + v.y ); } // add 2 vectors
vect2 operator+= ( vect2 v ) { return vect2( x += v.x, y += v.y ); } // add 2 vectors
};
class Ship {
private:
int id; // id of the ship and form object
char *meshfile; // "Ship.x" or whatever the model file is used
ShipCollection *CollectionObject; // To do Actions on others
float x, y, z; // position in space
// vect2 movementVector; // current movement vector due to inertia (used to update ship position)
float aim; // direction aiming
float thrustspeed; // thrust speed
float turnspeed; // turning speed
float maxspeed; // maximum speed
public:
vect2 movementVector; // current movement vector due to inertia (used to update ship position)
Ship (int i, char in_meshfile[], ShipCollection *in_collection); // constructor
~Ship () {
// dtor
dbDeleteObject ( id ); // cleanup PlayerShip
delete [] meshfile;
}
void reset();
float getX() { return x; } // accessors
float getY() { return y; }
float getZ() { return z; }
float getAim() { return aim; }
int getid() { return id; }
void setX( float i ) { x = i; } // set functions
void setY( float i ) { y = i; }
void setZ( float i ) { z = i; }
void setXYZ( float i, float j, float k ) { x = i; y = j; z = k; }
void setTurnspeed( float i ) { turnspeed = i; } // set turnspeed, thrustspeed, and maximum speed
void setThrustspeed( float i ) { thrustspeed = i; }
void setMaxspeed( float i ) { maxspeed = i; }
void thrust( float i ); // thrust forward or backward
void turn ( float i ) { aim = dbWrapValue(aim + i*turnspeed); } // change facing direction of ship
void update(); // updates position of the ship based on dir, speed
float getAngle() { return movementVector.getAngle(); } // returns current movement angle
};
Ship::Ship( int i, char in_meshfile[], ShipCollection *in_collection ) {
id = i; // get ship id
CollectionObject = in_collection; // set collection it belongs to
reset(); // call the reset function to init
meshfile = new char[strlen(in_meshfile)+1];
strcpy( meshfile, in_meshfile );
// Load the ship and place it at 0,0,0
dbLoadObject(meshfile, id);
// dbLoadObject("Ship.x", id);
dbPositionObject(id, x, y, z);
// collision setup
SC_SetupObject( id,1,0 ); //setup object id, into group 1 (Ships), with collision type sphere (1)
// SC_DrawObjectBounds( id ); // show object boundaries
}
void Ship::reset() {
x = y = z = 0; // position to be 0,0,0
aim = 0; // faces 90
movementVector.setXY(0, 0); // no movement
}
void Ship::thrust(float i) {
// add a thrust vector based on aim, thrust to the current movement vector
// declare vT, vM
vect2 vT; // declare Thrust, Cur Movement, and Result (new move) vector
float l1, l2; // length temp variables
vT.setXYFromAM( aim, i*thrustspeed ); // initialize Thrust vector from aim, i
l1 = movementVector.getLength(); // get original speed it's moving at
movementVector += vT; // result vector = adding thrust to current movement
l2 = movementVector.getLength();
// speed limiting code
if ( ( l2 > l1 ) && ( l1 > maxspeed ) ) {
// if it was already going faster than maxspeed, make it go at original speed with current vector
movementVector.scaleTo( l1 - thrustspeed );
}
}
void Ship::update() {
float collide = 0;
// collision code
// if they collide, then bounce
// save old coordinates
float oldx = x;
float oldy = y;
float oldz = 0;
// update the movement
x += movementVector.x;
y += movementVector.y;
dbRotateObject(id, 0, 0, aim);
//little spheres collide with all (0)
collide = SC_SphereCast( 0, oldx,oldy,oldz, x,y,z, 100, id );
// collide = SC_ObjectCollision ( id, 0 );
// if ( collide > 0 ) dbText( 100, 100, "COLLIDE!" );
if ( collide > 0 )
{
Ship* HitShip;
HitShip = CollectionObject->ShipList[collide];
HitShip->movementVector.setXY( movementVector.x * 0.5, movementVector.y * 0.5 );
movementVector.x = SC_GetCollisionBounceX() * 0.5;
movementVector.y = SC_GetCollisionBounceY() * 0.5;
}
// update the position
dbPositionObject(id, x, y, z);
// collision update
SC_UpdateObject(id);
}
// the main entry point for the application is this function
void DarkGDK ( void )
{
char* displaystring = new char [1024]; // initialize display string
ShipCollection gameCollection;
// ObjectList m_MasterList(100, 1000);
dbSyncOn ( ); // sync
dbSyncRate ( 60 );
dbAutoCamOff(); // turn off autofollow camera
// Init Collision system
SC_Start( );
// make full screen 1920 x 1080 x 32 resolution
dbSetWindowOff();
dbMaximizeWindow();
dbSetDisplayMode ( 1920, 1080, 32 );
dbRandomize ( dbTimer ( ) ); // set our random seed to a value from the timer
// create, load and scale the skybox -
dbMakeObjectSphere(2,-30000);
// dbLoadImage("skyspheregrey.png", 3);
dbLoadImage("ss1.jpg", 3);
dbTextureObject(2,3);
dbSetObjectTexture(2,3,1);
// dbScaleObject ( 2, 1, 1, 1 );
dbPositionObject(2,0,0,0);
// Create Playership and Set Variables
Ship PlayerShip(1, "Ship.x", &gameCollection); // instance Player
gameCollection.addShip(PlayerShip.getid(), &PlayerShip);
PlayerShip.setThrustspeed(.5); // set Thrust applied per cycle
PlayerShip.setTurnspeed(3); // set turn applied per cycle
PlayerShip.setMaxspeed(20); // set max speed allowed by thrust
// Create Other Ship and Set Variables
Ship EnemyShip(9, "Ship.x", &gameCollection); // instance EnemyShip
gameCollection.addShip(EnemyShip.getid(), &EnemyShip);
EnemyShip.setThrustspeed(.5); // set Thrust applied per cycle
EnemyShip.setTurnspeed(3); // set turn applied per cycle
EnemyShip.setMaxspeed(20); // set max speed allowed by thrust
EnemyShip.setXYZ( 500, 500, 0 ); // move it somewhere
// EnemyShip.movementVector.setXY( 1, 1 ); // move it a bit
// Set Lighting
dbSetAmbientLight ( 50 ); // percentage value of ambient light
// Position the Camera
dbSetCameraRange ( 1.0f, 30000.0f );
dbPositionCamera ( 0, 0, 2500 );
dbRotateCamera ( 0, 180, -90 );
// dbPointCamera ( 0, 0, 0 );
// dbRotateCamera ( 0, 0, 45 );
// void dbSetCameraToFollow ( float fX, float fY, float fZ, float fAngle, float fDistance, float fHeight, float fSmooth, int iCollision )
// load images for particles
int ImageNumber = 4;
dbLoadImage ( "fire.bmp", ImageNumber );
// dbLoadImage ( "sky\\spacesphere.jpg", ImageNumberB );
// Add Particle Effects!!!!
// create particles
int ParticleNumber = 6;
float fPEmitValue = 0;
dbMakeParticles ( ParticleNumber, ImageNumber, 50, 100.0f );
dbPositionParticles ( ParticleNumber, PlayerShip.getX(), PlayerShip.getY(), 0 );
dbColorParticles ( ParticleNumber, 255, 128, 0 );
dbSetParticleEmissions ( ParticleNumber, 0 );
dbSetParticleSpeed ( ParticleNumber, 0.01f );
dbSetParticleVelocity ( ParticleNumber, 0.0f );
dbSetParticleGravity ( ParticleNumber, 0 );
dbSetParticleFloor ( ParticleNumber, 0 );
dbSetParticleLife ( ParticleNumber, 20 );
// dbGhostParticlesOn ( ParticleNumber, 5 ); // ghost mode 0 to 5
// GDK Loop
while ( LoopGDK() ) {
// dbControlCameraUsingArrowKeys(0, 0.5, 0.5);
int KeyScanCode = dbScanCode();
// display some text on screen
// custom text
sprintf( displaystring,
"Aim: %d\nShip Angle: %f\nCamR X: %d CamR Y: %d CamR Z: %d\nPartX: %d PartY: %d PartZ: %d\nShipX: %d ShipY: %d ShipZ: %d\nScanCode: %d"
, (int)PlayerShip.getAim(), PlayerShip.getAngle()
, (int)dbCameraAngleX(), (int)dbCameraAngleY(), (int)dbCameraAngleZ()
, (int)dbParticlesPositionX(ParticleNumber), (int)dbParticlesPositionY(ParticleNumber), (int)dbParticlesPositionZ(ParticleNumber)
, (int)PlayerShip.getX(), (int)PlayerShip.getY(), (int)PlayerShip.getZ()
, KeyScanCode
);
dbText ( 0, 0, displaystring );
vect2 vFollowParticle;
vFollowParticle.setXYFromAM( PlayerShip.getAim(), -150 );
// move particles with ship
dbPositionParticleEmissions ( ParticleNumber, PlayerShip.getX() + vFollowParticle.x, PlayerShip.getY() + vFollowParticle.y, 0 );
// dbPositionParticleEmissions ( ParticleNumber, PlayerShip.getX()-PlayerShip.movementVector.x, PlayerShip.getY()-PlayerShip.movementVector.y, PlayerShip.getZ() );
// dbPositionParticles ( ParticleNumber, PlayerShip.getX(), PlayerShip.getY(), 0 );
// Set Particles to DEGRADE (set on when thrusting
if ( fPEmitValue > 0 )
fPEmitValue -= 0.2;
dbSetParticleEmissions ( ParticleNumber, (int)fPEmitValue );
dbSetParticleVelocity ( ParticleNumber, 0.01f );
// add thrust to the ship
if ( dbUpKey ( ) ) {
PlayerShip.thrust( 1 );
// Set Particles to ON (set on when thrusting)
// dbSetParticleVelocity ( ParticleNumber, 5.0f );
// dbSetParticleEmissions ( ParticleNumber, 5 );
fPEmitValue = 3;
}
// subtract thrust to the ship
if ( dbDownKey ( ) )
PlayerShip.thrust( -1 );
// turn ship left
if ( dbLeftKey ( ) )
PlayerShip.turn( -1 );
// turn ship right
if ( dbRightKey ( ) )
PlayerShip.turn( 1 );
// reset ship
if ( dbReturnKey ( ) ) {
PlayerShip.reset();
// EnemyShip.reset();
}
// camera set to Space
if ( dbSpaceKey ( ) ) {
// dbRotateCamera( 0, 0, PlayerShip.getAim() );
// dbSetCameraToFollow ( PlayerShip.getX(), PlayerShip.getY(), PlayerShip.getZ(), PlayerShip.getAim(), 1000, 1000, 50, 0 );
// dbSetCameraToFollow ( PlayerShip.getX(), PlayerShip.getY(), PlayerShip.getZ(), 180, 1000, 1000, 100, 0 );
// dbSetCameraToObjectOrientation ( PlayerShip.getid() );
vect2 vFollowCam;
vFollowCam.setXYFromAM ( PlayerShip.getAim(), -2000 );
dbPositionCamera ( PlayerShip.getX() + vFollowCam.x , PlayerShip.getY() + vFollowCam.y, 1000 );
dbXRotateCamera ( -70 );
dbZRotateCamera ( PlayerShip.getAim() - 90 );
}
// add thrust to the EnemyShip
if ( dbKeyState(17) ) {
EnemyShip.thrust( 1 );
// Set Particles to ON (set on when thrusting)
// dbSetParticleVelocity ( ParticleNumber, 5.0f );
// dbSetParticleEmissions ( ParticleNumber, 5 );
}
// subtract thrust to the ship
if ( dbKeyState(31) )
EnemyShip.thrust( -1 );
// turn ship left
if ( dbKeyState(30) )
EnemyShip.turn( -1 );
// turn ship right
if ( dbKeyState(32) )
EnemyShip.turn( 1 );
// Update Ship Position
PlayerShip.update();
// m_MasterList.update();
// Update EnemyShip Position
EnemyShip.update();
// Perform Sync
dbSync();
}
delete[] displaystring; // cleanup displaystring;
// dbDeleteObject ( PlayerShip.getid() ); // cleanup PlayerShip
return;
}