BlockManager.h
class BlockManager
{
public:
int Multiplier;
int StartObjID;
int PosX[1000];
int PosY[1000];
int PosZ[1000];
bool Used[1000];
BlockManager();
~BlockManager();
void GeneratePlaceholders(int multiplier, int startbbjid);
void ExclusionCheck(int ModelNumber);
};
BlockManager::BlockManager()
{
for ( int i = 0; i <= 1000; i++ )
{
this->Used[i] = false;
}
}
BlockManager::~BlockManager()
{
}
void BlockManager::GeneratePlaceholders(int multiplier, int startobjid)
{
int BlockNumber = startobjid;
this->Multiplier = multiplier;
this->StartObjID = startobjid;
for ( int X = 0; X < Multiplier; X++ )
{
for ( int Y = 0; Y < Multiplier; Y++ )
{
for ( int Z = 0; Z < Multiplier; Z++ )
{
dbMakeObjectBox ( BlockNumber, 1, 1, 1 );
dbPositionObject ( BlockNumber, X, Y, Z );
myPhysics.makeBox ( BlockNumber, true, 1.0f );
myPhysics.kinematicOn ( BlockNumber );
dbColorObject ( BlockNumber, dbRgb ( dbRnd ( 255 ), dbRnd ( 255 ), dbRnd ( 255 ) ) );
dbSetObjectCull ( BlockNumber, 1 );
this->PosX[BlockNumber-StartObjID] = X;
this->PosY[BlockNumber-StartObjID] = Y;
this->PosZ[BlockNumber-StartObjID] = Z;
this->Used[BlockNumber-StartObjID] = true;
BlockNumber++;
}
}
}
}
void BlockManager::ExclusionCheck(int ModelNumber)
{
int SurroundingBlock[5];
int ArraySpot = ModelNumber - this->StartObjID;
int Mult1 = this->Multiplier * this->Multiplier;
SurroundingBlock[0] = ArraySpot - this->Multiplier;//left
SurroundingBlock[1] = ArraySpot + this->Multiplier;//right
SurroundingBlock[2] = ArraySpot - 1;//back
SurroundingBlock[3] = ArraySpot + 1;//front
SurroundingBlock[4] = ArraySpot - ( Mult1 );//below
SurroundingBlock[5] = ArraySpot + ( Mult1 );//above
if ( SurroundingBlock[0] >= 0 &&
SurroundingBlock[1] >= 0 &&
SurroundingBlock[2] >= 0 &&
SurroundingBlock[3] >= 0 &&
SurroundingBlock[4] >= 0 &&
SurroundingBlock[5] >= 0 )
{
if ( SurroundingBlock[0] > 0 &&
SurroundingBlock[1] <= 2000 &&
SurroundingBlock[2] <= 2000 &&
SurroundingBlock[3] <= 2000 &&
SurroundingBlock[4] <= 2000 &&
SurroundingBlock[5] <= 2000 )
{
if ( this->Used[SurroundingBlock[0]] == true &&
this->Used[SurroundingBlock[1]] == true &&
this->Used[SurroundingBlock[2]] == true &&
this->Used[SurroundingBlock[3]] == true &&
this->Used[SurroundingBlock[4]] == true &&
this->Used[SurroundingBlock[5]] == true )
{
excluded++;
dbHideObject ( ModelNumber );
}
}
}
}
Main.cpp
#include "DarkGDK.h"
#include "physics.h"
physics myPhysics;
int excluded = 0;
#include "BlockManager.h"
BlockManager myBlocks;
void SetupWorld();
void displayData();
void Create_Character ( unsigned int ModelNumber, unsigned int CameraModel );
void Camera_Control ( unsigned int ModelNumber, unsigned int CameraModel );
void KeyControl();
void DarkGDK ( void )
{
myPhysics.start(true, 0.5, 0.5, 0.5);
myPhysics.setGravity(0, -9.8, 0);
dbSyncOn ( );
dbSyncRate ( 60 );
dbRandomize ( dbTimer ( ) );
SetupWorld ( );
Create_Character ( 99, 98 );
myPhysics.simulate();
while ( LoopGDK ( ) )
{
myPhysics.update();
KeyControl ( );
Camera_Control ( 99, 98 );
displayData();
dbSync ( );
dbWait ( 1 );
}
myPhysics.stop();
return;
}
void SetupWorld ( )
{
dbMakeObjectBox ( 100, 250, 1, 250 );
myPhysics.makeBox ( 100, true, 1 );
myPhysics.kinematicOn ( 100 );
dbColorObject ( 100, dbRgb ( dbRnd ( 255 ), dbRnd ( 255 ), dbRnd ( 255 ) ) );
dbPositionObject ( 100, 0, -10, 0 );
myBlocks.GeneratePlaceholders ( 10, 1000 );
for ( int B = 1200; B <= 1800; B++ )
{
myBlocks.ExclusionCheck ( B );
}
}
void displayData()
{
dbText(0, 0, dbStr(dbObjectPositionX(99)));
dbText(0, 10, dbStr(dbObjectPositionY(99)));
dbText(0, 20, dbStr(dbObjectPositionZ(99)));
dbText(0, 40, dbStr(excluded));
//dbText(0, 50, dbStr(triede));
dbText(0, 60, "FPS: ");
dbText(150, 60, dbStr(dbScreenFPS()));
dbText(0, 75, "Poly Count: ");
dbText(150, 75, dbStr(dbStatistic(1)));
}
void Create_Character ( unsigned int ModelNumber, unsigned int CameraModel )
{
dbMakeObjectBox ( ModelNumber, 1, 1, 1 );
dbPositionObject ( ModelNumber, 30, 1, 30 );
myPhysics.makeCharacterController ( ModelNumber, 1, 45 );
dbRotateObject ( ModelNumber, 0, 0, 0 );
dbMakeObjectSphere ( CameraModel, 1 );
dbScaleObject ( CameraModel, 0.0001f, 0.0001f, 0.0001f );
dbPositionObject ( CameraModel, dbObjectPositionX(ModelNumber),
dbObjectPositionY(ModelNumber) + 500,
dbObjectPositionZ(ModelNumber) );
}
void Camera_Control ( unsigned int ModelNumber, unsigned int CameraModel )
{
int beforeclick1 = dbMouseMoveX();
int beforeclick2 = dbMouseMoveY();
if ( dbMouseClick() == 2 )
{
dbHideMouse();
dbXRotateObject ( CameraModel, dbObjectAngleX(CameraModel) - (dbMouseMoveY() - beforeclick2)/4.0f );
dbYRotateObject ( ModelNumber, dbObjectAngleY(ModelNumber) - (dbMouseMoveX() - beforeclick1)/4.0f );
} else {
dbShowMouse();
}
dbPositionObject ( CameraModel, dbObjectPositionX(ModelNumber), dbObjectPositionY(ModelNumber) + 2, dbObjectPositionZ(ModelNumber) );
dbPositionCamera ( dbObjectPositionX(CameraModel), dbObjectPositionY(CameraModel), dbObjectPositionZ(CameraModel) );
dbRotateCamera ( dbObjectAngleX(CameraModel), dbObjectAngleY(ModelNumber), dbObjectAngleZ(CameraModel) );
dbMoveCamera ( -5 );
}
void KeyControl()
{
float speed = 0;
if (dbUpKey())
{
speed = 0.2;
}
if (dbDownKey())
{
speed = -0.2;
}
float gravity = -0.5;
float angleY = dbObjectAngleY(99);
float moveX = dbSin(angleY);
float moveZ = dbCos(angleY);
myPhysics.moveCharacterController(99, moveX * speed, gravity, moveZ * speed);
}
I can't quite figure out why my code doesnt work BUT I have narrowed it down to the area that is triggering it.
This code from the main.cpp's SetupWorld() Function
for ( int B = 1000; B <= 2000; B++ )
{
myBlocks.ExclusionCheck ( B );
}
You can remove the for loop and manually set B to any number between 1000 and 2000 and it works just fine, put the for loop back in there and it throws an exception. I've been pouring over this for 3 days
The world is setup by creating one large square out of 10x10x10 little squares, the ExclusionCheck looks at each block individually and it's 6 surrounding blocks, if all 6 surrounding blocks are present it hide's the block it was looking at since the player can't see it and that dramatically reduces poly count in my game. If someone has a better way of doing this than mine please do say
Exception:
First-chance exception at 0x0040147d in Dark GDK - 3D Game1.exe: 0xC0000005: Access violation reading location 0x00000128.
Unhandled exception at 0x0040147d in Dark GDK - 3D Game1.exe: 0xC0000005: Access violation reading location 0x00000128.
The program '[904] Dark GDK - 3D Game1.exe: Native' has exited with code 0 (0x0).
Disassembly:
0040147D mov eax,dword ptr [ebp-4]
I hope that helps.
EDIT: I solved the problem, it was when i initialized the variable int SurroundingBlock[5], i thought that would give me array places between 0-5. but actually it only made it 0-4 so i increased the size of the array by 1 and no more problems!
Add me to your MSN!