I've been working on a simple 3D physics engine, and have run into a bit of a problem. Variables defined outside of my main loop don't retain their values once the loop starts. However, if defined in the loop, they do.
I'll give the full source if necessary, but here are the relevant parts:
Types:
typedef struct vector{
double x;
double y;
double z;
} velocity, position, displacement;
typedef struct physicsobject{
position pos;
velocity vel;
displacement disp;
bool isGrounded;
double facing;
double moveSpeed;
double height;
int id;
} propPhysics,propWeapon;
Code:
propWeapon weapon_shotgun;
propPhysics player;
weapon_shotgun.pos.x=100;
weapon_shotgun.pos.z=100;
weapon_shotgun.pos.y=dbGetTerrainGroundHeight(terrain,weapon_shotgun.pos.x,weapon_shotgun.pos.z)+35.75;
weapon_shotgun.height=1;
weapon_shotgun.id=shotgun;
weapon_shotgun.isGrounded=false;
player.pos.x=20;
player.pos.z=20;
player.height=10;
player.pos.y=dbGetTerrainGroundHeight(terrain,player.pos.x,player.pos.z)+player.height;
player.isGrounded=true;
while(LoopGDK()){
if(dbReturnKey()==1){
weapon_shotgun.pos.x=100;
weapon_shotgun.pos.z=100;
weapon_shotgun.pos.y=dbGetTerrainGroundHeight(terrain,weapon_shotgun.pos.x,weapon_shotgun.pos.z)+35.75;
weapon_shotgun.height=1;
weapon_shotgun.id=shotgun;
weapon_shotgun.isGrounded=false;
player.pos.x=20;
player.pos.z=20;
player.height=10;
player.pos.y=dbGetTerrainGroundHeight(terrain,player.pos.x,player.pos.z)+player.height;
player.isGrounded=true;
}
player.moveSpeed=loopTime*.1;
if((dbKeyState(17)==1)&&(player.isGrounded)){
player.disp.x+=(player.moveSpeed*sin(player.facing*toRadian));
player.disp.z+=(player.moveSpeed*cos(player.facing*toRadian));
}
if((dbKeyState(31)==1)&&(player.isGrounded)){
player.disp.x+=(-player.moveSpeed*sin(player.facing*toRadian));
player.disp.z+=(-player.moveSpeed*cos(player.facing*toRadian));
}
if((dbKeyState(30)==1)&&(player.isGrounded)){
player.disp.x+=(player.moveSpeed*sin((player.facing-90)*toRadian));
player.disp.z+=(player.moveSpeed*cos((player.facing-90)*toRadian));
}
if((dbKeyState(32)==1)&&(player.isGrounded)){
player.disp.x+=(player.moveSpeed*sin((player.facing+90)*toRadian));
player.disp.z+=(player.moveSpeed*cos((player.facing+90)*toRadian));
}
if((dbSpaceKey()==1)&&(player.isGrounded)){
player.vel.y=25;
}
if(dbKeyState(18)==1){
player.facing+=.1*loopTime;
if(player.facing>=360)player.facing-=360;
}
if(dbKeyState(16)==1){
player.facing-=.1*loopTime;
if(player.facing<0)player.facing+=360;
}
//Update facing and stuff
player=gravityEffect(player,loopTime);
weapon_shotgun=gravityEffect(weapon_shotgun,loopTime);
player.pos.x+=player.disp.x;
if(player.isGrounded)player.disp.x=0;
player.pos.z+=player.disp.z;
if(player.isGrounded)player.disp.z=0;
dbRotateCamera(dbCameraAngleX(),player.facing,dbCameraAngleZ());
dbPositionCamera(player.pos.x,player.pos.y,player.pos.z);
dbPositionObject(weapon_shotgun.id,weapon_shotgun.pos.x,weapon_shotgun.pos.y,weapon_shotgun.pos.z);
loopTime=dbTimer()-loopTimeCheck;
loopTimeCheck=dbTimer();
dbSync();
}
And finally, should it be relevant (though I don't believe it is) the gravityEffect() function:
propPhysics gravityEffect(propPhysics propToTest, double timeElapsed){
double timeElapsedS=timeElapsed/timeScale;
if(propToTest.pos.y>dbGetTerrainGroundHeight(terrain,propToTest.pos.x,propToTest.pos.z)+propToTest.height){
propToTest.isGrounded=false;
}
if((propToTest.pos.y)<(dbGetTerrainGroundHeight(terrain,propToTest.pos.x,propToTest.pos.z)+propToTest.height)){
propToTest.pos.y=dbGetTerrainGroundHeight(terrain,propToTest.pos.x,propToTest.pos.z)+propToTest.height;
propToTest.vel.y=0;
propToTest.disp.y=0;
propToTest.isGrounded=true;
}
if(!propToTest.isGrounded)propToTest.vel.y=propToTest.vel.y+((-1*gravity)*timeElapsedS);
propToTest.disp.y=propToTest.vel.y*timeElapsedS;
propToTest.pos.y+=propToTest.disp.y;propToTest.disp.y=0;
return propToTest;
}
The values should be set correctly, because they're assigned right before the start of the loop. However, all values for weapon_shotgun and player equal zero. If enter is pressed during the loop, the values are properly assigned, and the program can run as expected. Why would it matter if the values are assigned before or during the loop?
My site, for various stuff that I make.