Now I finished jumping. Still not perfect but it works.
@Sydbod and all the others
Do you think this code is good enough to post it in
<code snippets> ?
// Dark GDK - The Game Creators - www.thegamecreators.com
// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application
// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
#include "math.h"
#define W 17
#define A 30
#define D 32
#define S 31
void MovementCorrection();
bool ObjWasLooped;
bool WASD;
bool PlayerOnGround;
bool PlayerJumped;
float PlayerInAir;
float MovementForward;
float MovementSide;
float PlayerAngleX;
float PlayerAngleY;
float GravityAngleXZ;
float x_pos;
float z_pos;
float x_delta;
float z_delta;
float objHeight;
float CorrectionAngle;
float HeightDifference;
float correction;
float terrainHeight;
int correctionStep;
bool KeyStateWASD()
{
WASD = false;
if (dbKeyState(W))
WASD=true;
if (dbKeyState(A))
WASD=true;
if (dbKeyState(S))
WASD=true;
if (dbKeyState(D))
WASD=true;
return WASD;
}
void IsGroundSteep()
{
float x_temp[8];
float z_temp[8];
float y_temp[8];
float angle_temp[8];
float Hyp=15; //länge des Fühlers
float min =90000000;
float max = 0;
int i, min_pos, max_pos;
min_pos=0;
angle_temp[0]= dbObjectAngleY(3) ;
for (i=1;i<8;i++)
angle_temp[i]= angle_temp[i-1] +45;
for (i=0;i<8;i++)
{
x_delta = dbSIN(angle_temp[i])*Hyp;
z_delta = dbCos(angle_temp[i])*Hyp;
x_temp[i]= x_pos + x_delta;
z_temp[i]=z_pos + z_delta;
y_temp[i]= dbGetTerrainGroundHeight(1,x_temp[i], z_temp[i]);
}
for (i=0;i<8;i++)
{
if (y_temp[i]<= min)
{
min= y_temp[i];
min_pos=i;
}
if (y_temp[i]>= max)
{
max = y_temp[i];
max_pos=i;
}
}
if (y_temp[min_pos] != y_temp[max_pos])
{
HeightDifference= y_temp[max_pos] - y_temp[min_pos];
CorrectionAngle = min_pos*45 + 180;
//CorrectionAngle = angle_temp[min_pos];
if (CorrectionAngle>360)CorrectionAngle = dbWrapValue(CorrectionAngle);
dbPositionObject (4,x_temp[max_pos],y_temp[max_pos],z_temp[max_pos]);
dbPositionObject (5,x_temp[min_pos],y_temp[min_pos],z_temp[min_pos]);
// Added colored balls for placing to the feeler locations
dbPositionObject (10,x_temp[0],y_temp[0]+10,z_temp[0]);//raise the balls by 10 units to see better
dbPositionObject (11,x_temp[1],y_temp[1]+10,z_temp[1]);
dbPositionObject (12,x_temp[2],y_temp[2]+10,z_temp[2]);
dbPositionObject (13,x_temp[3],y_temp[3]+10,z_temp[3]);
dbPositionObject (14,x_temp[4],y_temp[4]+10,z_temp[4]);
dbPositionObject (15,x_temp[5],y_temp[5]+10,z_temp[5]);
dbPositionObject (16,x_temp[6],y_temp[6]+10,z_temp[6]);
dbPositionObject (17,x_temp[7],y_temp[7]+10,z_temp[7]);
}
dbText(150,0,"Hoehendifferenz:" );
dbText(280,0,dbStr(HeightDifference));
//dbText(150,20,"Korrekturwinkel" );
//dbText(290,20,dbStr(CorrectionAngle));
dbText(150,10,"Playerwinkel" );
dbText(290,10,dbStr(PlayerAngleX));
}
void GravityPlayer()
{
float Forward_delta=0;
float Side_delta=0;
if (HeightDifference > 20)
{
correction = HeightDifference * 0.3;
Forward_delta = dbCOS(CorrectionAngle)*correction;
Side_delta = dbSIN(CorrectionAngle)*correction;
MovementForward = MovementForward - Forward_delta;
MovementSide = MovementSide - Side_delta;
dbText(150,20,"Korrekturwinkel" );
dbText(290,20,dbStr(CorrectionAngle));
}
else
{ x_pos = dbObjectPositionX ( 3 );
z_pos = dbObjectPositionZ ( 3 );
}
}
// the main entry point for the application is this function
void DarkGDK ( void )
{ //Variablen für Objekt-Position
dbSetDisplayMode ( 1024, 768, 32 );
x_pos = 1600.0f;
z_pos = 1100.0f;
ObjWasLooped = false;
// switch on sync rate and set to a maximum of 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
dbSetCameraRange ( 1.0f, 30000.0f );
SetCurrentDirectory ( "Map" );
dbLoadImage ( "aktuelltext.bmp", 1 );
dbLoadImage ( "detail.jpg", 2 );
dbSetupTerrain ( );
dbMakeObjectTerrain ( 1 );
dbSetTerrainHeightMap (1,"aktuell.bmp");
dbSetTerrainScale ( 1, 19.0f, 2.0f, 19.0f );
dbSetTerrainLight ( 1, 1.0f, -0.25f, 0.0f, 1.0f, 1.0f, 0.78f, 0.9f );
dbSetTerrainTexture ( 1, 1, 2 );
dbBuildTerrain ( 1 );
dbLoadObject ( "skybox2.x", 2 );
dbLoadObject ("Colonel-x.X",3);
dbYRotateObject ( 3, 180.0 );
dbFixObjectPivot ( 3 );
dbSetObjectLight ( 2, 0 );
dbScaleObject ( 2, 30000, 30000, 30000 );
dbPositionCamera ( 385, 23, 100 );
dbSetObjectTexture ( 2, 3, 1 );
objHeight= dbGetTerrainGroundHeight ( 1, x_pos,z_pos);
dbPositionObject (3, x_pos, objHeight, z_pos);
dbMakeObjectCone (4,20);
dbMakeObjectCube (5,10);
dbMakeObjectSphere (10 , 2);// New Maker
dbColorObject (10, dbRGB (255,255,255));
dbMakeObjectSphere (11 , 2);
dbColorObject (11, dbRGB (255,0,0));
dbMakeObjectSphere (12 , 2);
dbColorObject (12, dbRGB (255,255,0));
dbMakeObjectSphere (13 , 2);
dbColorObject (13, dbRGB (0,255,0));
dbMakeObjectSphere (14 , 2);
dbColorObject (14, dbRGB (0,255,255));
dbMakeObjectSphere (15 , 2);
dbColorObject (15, dbRGB (0,0,255));
dbMakeObjectSphere (16 , 2);
dbColorObject (16, dbRGB (255,0,255));
dbMakeObjectSphere (17 , 2);
dbColorObject (17, dbRGB (0,0,0));
while ( LoopGDK ( ) )
{
if (PlayerOnGround==true)
{
MovementForward=0;
MovementSide=0;
}
objHeight= dbGetTerrainGroundHeight ( 1, dbObjectPositionX(3),dbObjectPositionZ(3));
// find the ground height of the terrain
float fHeight = dbGetTerrainGroundHeight ( 1, dbCameraPositionX ( ), dbCameraPositionZ ( ) );
PlayerAngleX = dbWrapValue ( PlayerAngleX + dbMouseMoveX ( ) * 0.4 );
PlayerAngleY = dbWrapValue ( PlayerAngleY + dbMouseMoveY ( ) * 0.4);
dbYRotateObject (3,PlayerAngleX);
if (PlayerAngleY < 80)
dbXRotateCamera (PlayerAngleY);
if (PlayerAngleY>260)
dbXRotateCamera (PlayerAngleY);
dbText(0,0,dbStr(PlayerAngleY));
dbText (0,20,dbStr(dbScanCode()));
if (PlayerOnGround==true)
{
if (dbKeyState(W))
{
MovementForward = 10;
ObjWasLooped = true;
}
if (PlayerOnGround==1 && PlayerInAir==0)
{
if (dbKeyState(57))
{
//PlayerJump();
dbMoveObjectUp(3,100);
}
}
if (dbKeyState(A))
{
MovementSide=-10;
ObjWasLooped = true;
}
if (dbKeyState(D))
{
MovementSide = 10;
ObjWasLooped = true;
}
if (dbKeyState(S))
{
MovementForward = -10;
ObjWasLooped = true;
}
IsGroundSteep();
GravityPlayer();
}
if (MovementForward != 0)
{
dbSetObjectSpeed ( 3,55 );
dbLoopObject ( 3, 300 , 318);
dbMoveObject (3,MovementForward);
//x_pos = dbObjectPositionX ( 3 );
//z_pos = dbObjectPositionZ ( 3 );
ObjWasLooped = true;
}
if (MovementSide != 0)
{
dbSetObjectSpeed ( 3,80 );
dbLoopObject ( 3, 260 , 279);
dbMoveObjectRight (3,MovementSide);
//x_pos = dbObjectPositionX ( 3 );
//z_pos = dbObjectPositionZ ( 3 );
ObjWasLooped = true;
}
x_pos = dbObjectPositionX ( 3 );
z_pos = dbObjectPositionZ ( 3 );
/*objHeight = dbGetTerrainGroundHeight (1,x_pos,z_pos);
dbPositionObject (3, x_pos, objHeight, z_pos);*/
if (KeyStateWASD()==false)
if (ObjWasLooped == true)
{
dbLoopObject(3,210, 234);
dbStopObject(3);
ObjWasLooped = false;
}
terrainHeight=dbGetTerrainGroundHeight (1,x_pos,z_pos);
if (terrainHeight>dbObjectPositionY(3))
dbPositionObject (3, x_pos, terrainHeight, z_pos);
if ((dbObjectPositionY(3)-terrainHeight)>3)
{
PlayerOnGround = false;
PlayerInAir= PlayerInAir+2;
dbMoveObjectDown(3,5*PlayerInAir);
terrainHeight=dbGetTerrainGroundHeight (1,x_pos,z_pos);
if (terrainHeight>dbObjectPositionY(3))
dbPositionObject (3, x_pos, terrainHeight, z_pos);
}
else
PlayerOnGround = true;
PlayerInAir=0;
float angle=dbObjectAngleY(3);
dbSetCameraToFollow( x_pos,objHeight+50,z_pos,angle,90,30,10,0);
// update the terrain
dbUpdateTerrain ( );
// update the screen
dbSync ( );
}
}