DynamiX vs Sparkys
I was wondering if there was any difference in performace between DynamiX and Sparkys.
I have attached a comparison, it's the sparkys sliding collision example put on a bigger level with more objects(200).
I then altered the demo to use a DynamiX character controller and it ran about twice as fast, the code it also a little smaller.
I guess most of the increase is due to DynamiX being multi-threaded, I think if sparkys was multi-threaded then there would not be alot of difference.
DynamiX:
#include "DarkGDK.h"
#include "DarkDynamiX_Lite.h"
const int playerCount = 200;
float yVel[playerCount];
const int NXCC_COLLISION_DOWN = 4;
float radius = 7.0f;
void makeLevel()
{
dbLoadObject( "../../TestLevel/TestMap.dbo",1 );
//-- Run once to create triangle mesh
//dbMakeMeshFromObject(1, 1);
//dbMakeObject(1000, 1, 0);
//phyCookTriangleMesh(1000, "../../TestLevel/TestMap.TMesh");
phyLoadTriangleMesh("../../TestLevel/TestMap.TMesh", 1);
phyMakeTriangleMesh(1, 1);
}
void makePlayer()
{
for(int i = 2; i < playerCount + 2; i++){
dbMakeObjectSphere( i,radius*2.0f );
dbPositionObject( i, dbRND(1000) - 500,50,dbRND(1000) - 500 );
dbColorObject(i, dbRGB(dbRND(255), dbRND(255), dbRND(255)));
phyMakeControllerCapsule(i, 0, 45, 1);
yVel[i - 2] = 0;
}
}
void movePlayer(int id)
{
float angy = dbObjectAngleY(id);
float vx = 0;
float vz = 0;
if (dbKeyState(32) == 1 ) { vx = vx + dbCos(angy); vz = vz - dbSin(angy); }
if (dbKeyState(30) == 1 ) { vx = vx - dbCos(angy); vz = vz + dbSin(angy); }
if (dbKeyState(31) == 1 ) { vx = vx - dbSin(angy); vz = vz - dbCos(angy); }
if (dbKeyState(17) == 1 ) { vx = vx + dbSin(angy); vz = vz + dbCos(angy); }
int collisionFlag = phyControllerMove(id, vx, yVel[id - 2], vz);
if(collisionFlag & NXCC_COLLISION_DOWN){
yVel[id - 2] = 0.0f;
}
else{
yVel[id - 2] = yVel[id - 2] - 9.8 / 60;
}
}
void positionCameraToObject( int obj, int thirdPerson)
{
dbPositionCamera( dbObjectPositionX(2),dbObjectPositionY(2),dbObjectPositionZ(2) );
dbRotateCamera( dbObjectAngleX(2),dbObjectAngleY(2),dbObjectAngleZ(2) );
if ( thirdPerson == 1 )
{
dbPitchCameraDown( 10 );
dbMoveCamera( -30 );
}
}
void DarkGDK( )
{
dbSyncOn ();
dbSetDisplayModeVSync(640, 480, 32, false);
dbSyncRate( 0 );
dbAutoCamOff( );
dbPositionCamera(0, 400, -1250);
dbPointCamera(0, 0, 0);
dbRandomize( dbTimer() );
phyStart();
makeLevel();
makePlayer();
phySimulate();
while( LoopGDK( ) )
{
phyFetchResults();
phyUpdate();
for(int i = 2; i < playerCount + 2; i++){
movePlayer(i);
}
phySimulate();
static float camX = 0.0f;
static float camY = 0.0f;
camX += dbMouseMoveY() * 0.2;
camY += dbMouseMoveX() * 0.2;
dbRotateCamera(camX, camY, 0);
dbText( 0,0,"Use W/A/S/D to move" );
char str [ 128 ];
sprintf_s( str, 128, "FPS: %d", dbScreenFPS( ) ); dbText( 0,80,str );
dbSync( );
}
phyStop();
}
Sparkys:
#include "DarkGDK.h"
#include "SC_Collision.h"
const int playerCount = 200;
float radius = 7.0f;
float gravity = -0.1f;
float slope = 0.5f;
int ground = 1;
int jumptimer = 0;
//player movement vector
float vx = 0;
float vy = 0;
float vz = 0;
void makeLevel()
{
dbLoadObject( "../../TestLevel/TestMap.dbo",1 );
SC_SetupComplexObject( 1,1,2 );
}
void makePlayer()
{
for(int i = 2; i < playerCount + 2; i++){
dbMakeObjectSphere( i,radius*2.0f);
dbPositionObject( i, dbRND(1000) - 500,50,dbRND(1000) - 500 );
dbColorObject(i, dbRGB(dbRND(255), dbRND(255), dbRND(255)));
SC_SetupObject( i,0,1 );
}
//player has no group
}
void movePlayer(int id)
{
float oldx = dbObjectPositionX(id);
float oldy = dbObjectPositionY(id);
float oldz = dbObjectPositionZ(id);
//apply gravity, and user changes to movement
float angy = dbObjectAngleY(id);
vx = 0;
vz = 0;
//if player is jumping or falling then apply 'normal' gravity
//if not attempt to keep the player stuck to the floor
if ( vy == 0 && jumptimer == 0 ) vy = vy + 10*gravity;
else vy = vy + gravity;
if (dbKeyState(32) == 1 ) { vx = vx + dbCos(angy); vz = vz - dbSin(angy); }
if (dbKeyState(30) == 1 ) { vx = vx - dbCos(angy); vz = vz + dbSin(angy); }
if (dbKeyState(31) == 1 ) { vx = vx - dbSin(angy); vz = vz - dbCos(angy); }
if (dbKeyState(17) == 1 ) { vx = vx + dbSin(angy); vz = vz + dbCos(angy); }
//this would be the player's final position without collision
float x = oldx + vx;
float y = oldy + vy;
float z = oldz + vz;
//first handle gravity - seperated from horizontal movement
//to achieve a more realistic effect
//Method: simple - cast a sphere vertically down, if it hits the level then
// position the object there (sticky collision) then move
// on to horizontal movement
// more complex - if we were to only use the simple method the player would be
// allowed to climb almost vertical slopes. Alternative is to
// get the normalY direction to work out how flat the gorund
// below the player is, 0-slope# is flatter, slope#-1 is steeper.
// if it's flat, use sticky collision, if it's steep slide the
// player down the slope. Changing slope# determines how steep the
// player can climb. NOTE: this also effects stairs.
int collide = SC_SphereCastGroup( 1, oldx,oldy,oldz, oldx,oldy+vy,oldz, radius,0 );
if ( collide > 0 )
{
//how flat is this ground
float ny = SC_GetCollisionNormalY();
if ( dbAbs(ny) > slope )
{
//FLAT, stick
oldy = SC_GetStaticCollisionY();
}
else
{
//STEEP, slide
x = x - oldx; z = z - oldz;
oldx = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
oldz = SC_GetCollisionSlideZ();
x = x + oldx; z = z + oldz;
}
//ny#<0 means the player has hit a ceiling rather than a floor
if ( ny > slope )
{
//only on ground if standing on flat ground
ground = 1;
vy = 0;
}
else
{
ground = 0;
//if player has hit a flat ceiling then stop vy# movement
if ( ny < -slope ) vy = gravity;
}
}
else
{
//nothing below player, not on ground, add vertical speed to player
oldy = oldy + vy;
ground = 0;
}
//handle horizontal movement as sliding
//player only collides with group 1 (level) objs and moves freely through others
collide = SC_SphereSlideGroup( 1, oldx,oldy,oldz, x,oldy,z, radius,0 );
if ( collide > 0 )
{
//if hit, reposition player, halt movement vector
x = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
z = SC_GetCollisionSlideZ();
vx = 0;
vz = 0;
//possible code for giving the player a jumping help up stairs...
//might be useful if slope# is set very high but stairs are still required
//dy = oldy - SC_GetStaticCollisionY()
//if ( dy < slope && dy > 0 && ground == 1 ) vy = 0.5;
}
//position the player
dbPositionObject( id,x,oldy,z );
SC_UpdateObject( id );
}
void positionCameraToObject( int obj, int thirdPerson)
{
dbPositionCamera( dbObjectPositionX(2),dbObjectPositionY(2),dbObjectPositionZ(2) );
dbRotateCamera( dbObjectAngleX(2),dbObjectAngleY(2),dbObjectAngleZ(2) );
if ( thirdPerson == 1 )
{
dbPitchCameraDown( 10 );
dbMoveCamera( -30 );
}
}
void DarkGDK( )
{
dbSyncOn( );
dbSetDisplayModeVSync(640, 480, 32, false);
dbSyncRate( 0 );
dbAutoCamOff( );
dbPositionCamera(0, 400, -1250);
dbPointCamera(0, 0, 0);
dbRandomize( dbTimer() );
SC_Start( );
makeLevel();
makePlayer();
while( LoopGDK( ) )
{
for(int i = 2; i < playerCount + 2; i++){
movePlayer(i);
}
static float camX = 0.0f;
static float camY = 0.0f;
camX += dbMouseMoveY() * 0.2;
camY += dbMouseMoveX() * 0.2;
dbRotateCamera(camX, camY, 0);
dbText( 0,0,"Use W/A/S/D to move" );
char str [ 128 ];
sprintf_s( str, 128, "FPS: %d", dbScreenFPS( ) ); dbText( 0,80,str );
dbSync( );
}
}
Anyway, I have finished the raycast, I am just adding a few more character controller commands and I will release the next version. I also have an example of a character pushing dynamic objects and jumping etc.
Quote: "it too was a bit quick to flip cars when making a turn "
I could have added more downforce to alter this so the car sticks to the road a bit more