Hello,
I previously posted a thread on this about 2.5 hours ago, and the topic is displayed on the forum but there's no content inside it.. *sigh..*
Basically im having trouble with basic movement and jumping, my current code allows me to jump properly and move forwards and backwards but rotation (left and right) and only works correctly if you rotate upon first playing without moving backwards or forwards, otherwise it goes out of sync and the speed gets increased..
I was just wondering if someone could help me either correct this or suggest other code that will work correctly.. My code is below and ive made the character a cube so it can be tested in DarkGDK with no modifications..
Thanks so much for your help!
#include "DarkGDK.h"
void DarkGDK ( void )
{
float ya;
float ac;
float dc;
float an;
float x;
float y;
float z;
float angle;
float distance;
float height;
float smooth;
float Vy=0.0f;
float Vx=0.0f;
float PosX=0.0f;
float PosY=0.0f;
float Gravity = 0.8f;
bool Ground=true;
int JumpLeft=2;
int SpacePressed=0;
char str[128]="";
dbAutoCamOff();
dbMakeObjectCube(1, 100);
dbMakeObjectCube(2, 100);
dbMakeObjectCube(3, 100);
dbMakeObjectCube(4, 100);
dbPositionObject(2,300,0,0);
dbPositionObject(3,700,0,0);
dbPositionObject(4,900,0,0);
dbSyncOn ( );
dbSyncRate ( 60 );
while ( LoopGDK ( ) )
{
if (dbLeftKey()==1)
{
ya=ya-2;
}
if (dbRightKey()==1)
{
ya=ya+2;
}
if (dbUpKey()==1)
{
ac=ac+10;
}
if (dbDownKey()==1)
{
ac=ac-10;
}
//JUMPING CODE STARTS HERE
if(dbSpaceKey())
{
SpacePressed++; //Pressed Jump Key
}
else
{
SpacePressed=0; //Did not press Jump Key
}
if(PosY < 0.1f) //Player is touching ground, reset jumpstate
{
Ground=true;
JumpLeft=2;
Vy=0.0f;
}
else
{
Ground=false; //Player not touching ground
}
if(SpacePressed==1)//We only want to detect when the Jumpkey was pressed.
{
if(JumpLeft>0)
{
Vy=20;JumpLeft--; //add vertical force, and will no longer be on ground
Ground=false;
}
}
if(Ground==false)
{
Vy=Vy-Gravity;
}
PosY=PosY+Vy;//Make changes to Y position
dbPositionObject(1,PosX,PosY,0);//position player
//JUMPING CODE ENDS HERE ^^
dbRotateObject( 1, 0,ya,0 );
dbMoveObject(1, ac);
angle=dbObjectAngleY(1)+Vx;
x=dbObjectPositionX(1);
y=dbObjectPositionY(1);
z=dbObjectPositionZ(1);
distance=350.0;
height=200.0 ;
smooth=12.0;
dbSetCameraToFollow( x,y,z,angle,distance,height,smooth,0);
sprintf_s( str, 128, "Position X: %f", dbObjectPositionX(1) );dbText( 0,dbScreenHeight()-40,str);
dbSync ( );
}
return;
}