I wrote a class to handle gravity and help me with collision. im not sure how to reposition myself in the map based on collision though. Here is the code for my class:
#define FALLING 0000
#define BOTTOM_REACHED 0001
#define NO_SUCH_OBJ 0002
#define NOT_FALLING 0003
class gravity
{
private:
int y_pos;
int min_pos;
int fall_rate;
int object;
bool falling;
public:
gravity ();
gravity (int, float, float, float);
void InitGrav (int, float, float, float);
int runGrav ();
void SetFallRate ();
void IsFalling (bool);
};
#endif
Simple. Now here is my main program:
#include "DarkGDK.h"
#include "SC_Collision.h"
#include "gravity.h"
#define WORLD 0001
#define TEXTURE_1 0002
#define PLAYER 0003
void ControlPlayer (int object)
{
dbSetObjectToCameraOrientation (object);
if (dbUpKey () || dbKeyState (17))
{
dbMoveObject (object, 20.0f);
}
if (dbDownKey () || dbKeyState (31))
{
dbMoveObject (object, -20.0f);
}
if (dbLeftKey () || dbKeyState (30))
{
dbYRotateCamera (0, dbCameraAngleY () - 90);
dbSetObjectToCameraOrientation (object);
dbMoveObject (object, 20.0f);
dbYRotateCamera (0, dbCameraAngleY () + 90);
}
if (dbRightKey () || dbKeyState (32))
{
dbYRotateCamera (0, dbCameraAngleY () + 90);
dbSetObjectToCameraOrientation (object);
dbMoveObject (object, 20.0f);
dbYRotateCamera (0, dbCameraAngleY () - 90);
}
}
void DarkGDK ()
{
gravity player_grav;
dbSyncOn ();
dbSyncRate (30);
dbLoadObject ("universe.dbo", WORLD);
dbPositionObject (WORLD, 0, 0, 0);
dbMakeObjectBox (PLAYER, 10, 10, 10);
dbHideObject (PLAYER);
player_grav.InitGrav (PLAYER, dbObjectPositionY (PLAYER), -600, 10.0f);
SC_Start ();
SC_SetupObject (WORLD, 1, 0);
SC_SetupObject (PLAYER, 1, 0);
dbLoadImage ("grass.bmp", TEXTURE_1);
dbTextureObject (WORLD, TEXTURE_1);
dbPositionCamera ( 434, 4000, -400);
dbPositionObject (PLAYER, dbCameraPositionX (), dbCameraPositionY ()-20, dbCameraPositionZ ());
SC_UpdateObject (PLAYER);
// camera variables
float fCameraAngleX = 0.0f;
float fCameraAngleY = 0.0f;
// our main loop
while ( LoopGDK ( ) )
{
// create a rotation axis based on mouse movement
fCameraAngleX = dbWrapValue ( fCameraAngleX + dbMouseMoveY ( ) * 0.4f );
fCameraAngleY = dbWrapValue ( fCameraAngleY + dbMouseMoveX ( ) * 0.4f );
// rotate camera
dbXRotateCamera ( fCameraAngleX );
dbYRotateCamera ( fCameraAngleY );
if (SC_ObjectCollision (WORLD, PLAYER))
{
float CollideY = SC_GetStaticCollisionY ();
dbText (10, 30, "You collided with the world!");
player_grav.IsFalling (false);
dbPositionObject (1, dbObjectPositionX (PLAYER), CollideY, dbObjectPositionZ (PLAYER));
}
else
{
player_grav.IsFalling (true);
}
if (player_grav.runGrav () == BOTTOM_REACHED)
dbText (10, 10, "You reached the bottom of the map!");
dbPositionCamera (dbObjectPositionX (PLAYER), dbObjectPositionY (PLAYER)+20, dbObjectPositionZ (PLAYER));
ControlPlayer (PLAYER);
SC_UpdateObject (PLAYER);
dbSync ();
}
}
I am using a map that i made with fps creator. Now, the problem is that i keep moving through the level walls for some reason. when i initially fall on top of the level, im fine, but after i'm done falling and begin to move around, i somehow go through the level. can anyone help me modify this to take care of that problem? Or, if anyone can help me in creating collision, i really need it. i have no idea how to simulate an object being solid. i want to be able to stop players from going through walls and such as well, but i dont know how to do it with fps creator maps. actually, i dont know how to do it with any map