diff --git a/.gitignore b/.gitignore index b7924b9d..173d8d58 100755 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ build *~ KubKraft *.swp - +*.swo +lib diff --git a/Makefile b/Makefile index 88fecc46..20db3f12 100755 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ LDFLAGS := -g -Wl #--------------------------------------------------------------------------------- # Any extra libraries you wish to link with your project #--------------------------------------------------------------------------------- +#LIBS := ../lib/libGLEW.a -lGL -lGLU -lSDL -lXrandr -lpng LIBS := -lGLEW -lGL -lGLU -lSDL -lXrandr -lpng #--------------------------------------------------------------------------------- diff --git a/TODO b/TODO index 54a615b8..3c3661a0 100755 --- a/TODO +++ b/TODO @@ -1,6 +1,6 @@ - Physics: - Improve jump and collisions. - - Improve game speed. + - Improve game speed. ( Temporary solution found in mapManager.cpp ) - Caves - Pause menu - Main menu diff --git a/include/config.h b/include/config.h index 7be368a1..9f03ef00 100644 --- a/include/config.h +++ b/include/config.h @@ -27,7 +27,7 @@ #define APP_LABEL "KubKraft" #define DIST_NEAR 0.1 -#define DIST_FAR 1000. +#define DIST_FAR 1000.0 #define CHUNK_WIDTH 8 #define CHUNK_DEPTH 8 @@ -41,7 +41,7 @@ #define PLAYER_HEIGHT 1.8 #define MOVEMENT_SPEED 3.5 #define FLY_SPEED 0.05 -#define JUMP_SPEED 0.15 -#define GRAVITY 0.00981 +#define JUMP_SPEED 15.5 +#define GRAVITY 0.0981 #endif // CONFIG_H diff --git a/include/map.h b/include/map.h index ee0ed1c4..6547b2c2 100644 --- a/include/map.h +++ b/include/map.h @@ -20,15 +20,6 @@ #ifndef MAP_H #define MAP_H -struct vect3D { - float x; - float y; - float z; - - vect3D(float xx = 0, float yy = 0, float zz = 0) - : x(xx), y(yy), z(zz) {} -}; - class Map { public: Map(u16 width, u16 depth, u16 height); diff --git a/include/player.h b/include/player.h index a298e8c9..6c79e1b9 100644 --- a/include/player.h +++ b/include/player.h @@ -27,11 +27,12 @@ class Player { Player(float x, float y, float z, float angle); void move(float distance, float direction); + void update(); void jump(); - void turnH(float angle); - void turnV(float angle); void fly(); void land(); + void turnH(float angle); + void turnV(float angle); void watch(); @@ -41,7 +42,7 @@ class Player { bool isJumping() const { return m_isJumping; } - void setJumpSpeed(float jumpSpeed) { m_jumpSpeed = jumpSpeed; } + void setJumpSpeed(float jumpSpeed) { m_speed.z = jumpSpeed; } void setJumping(bool isJumping) { m_isJumping = isJumping; } float pointTargetedx() { return m_x + cos(m_angleH * RADIANS_PER_DEGREES) * cos(m_angleV * RADIANS_PER_DEGREES); } @@ -57,8 +58,11 @@ class Player { float m_angleH; float m_angleV; - float m_jumpSpeed; bool m_isJumping; + + vect3D m_speed; + + void testPoint(vect3D pos, vect3D *speed); }; #endif // PLAYER_H diff --git a/include/types.h b/include/types.h index 3ebe290c..da2abbc0 100644 --- a/include/types.h +++ b/include/types.h @@ -27,4 +27,13 @@ typedef signed short s16; typedef unsigned long u32; typedef signed long s32; +struct vect3D { + float x; + float y; + float z; + + vect3D(float xx = 0, float yy = 0, float zz = 0) + : x(xx), y(yy), z(zz) {} +}; + #endif // TYPES_H diff --git a/source/game.cpp b/source/game.cpp index 53f16848..01db9e7e 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -47,7 +47,7 @@ unsigned int Game::mapDepth = 16 << 3; unsigned int Game::mapHeight = 8 << 3; Game::Game() { - player = new Player(2, 2, 48, 90); + player = new Player(2.2, 2.2, 48, 90); map = new Map(mapWidth, mapDepth, mapHeight); } @@ -189,6 +189,8 @@ void Game::animate() { } player->jump(); + + player->update(); } void Game::draw() { diff --git a/source/map.cpp b/source/map.cpp index fdae22e1..c3839395 100644 --- a/source/map.cpp +++ b/source/map.cpp @@ -70,10 +70,10 @@ Map::Map(u16 width, u16 depth, u16 height) { for (int xC = 0; xC < CHUNK_WIDTH; xC++) { int x = xx * CHUNK_WIDTH + xC; int y = yy * CHUNK_DEPTH + yC; - float perlin = get2DPerlinNoiseValue(x, y, 64) * 3 // 3 - 0 - + get2DPerlinNoiseValue(x, y, 32) * 2 // 0 - 4 - + get2DPerlinNoiseValue(x, y, 16) * 2 // 0.5 - 3 - + get2DPerlinNoiseValue(x, y, 8) * 1; // 0.1 - 2 + float perlin = get2DPerlinNoiseValue(x, y, 64) * 0 // 3 // 3 - 0 + + get2DPerlinNoiseValue(x, y, 32) * 4 // 2 // 0 - 4 + + get2DPerlinNoiseValue(x, y, 16) * 3 // 2 // 0.5 - 3 + + get2DPerlinNoiseValue(x, y, 8) * 2; // 1; // 0.1 - 2 int heightValue = int((perlin * float(CHUNK_HEIGHT)) + float(m_height / 2)); if (heightValue < 0) heightValue = 0; diff --git a/source/player.cpp b/source/player.cpp index e9076883..106843ae 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include @@ -29,7 +31,11 @@ #include "config.h" #include "types.h" #include "mapManager.h" +#include "cube.h" +#include "chunk.h" +#include "map.h" #include "player.h" +#include "game.h" using namespace std; @@ -37,60 +43,81 @@ Player::Player(float x, float y, float z, float angle) { m_x = x; m_y = y; - m_eyeheight = z + 0.8; + m_eyeheight = z + PLAYER_HEIGHT - 1; m_angleH = angle; m_angleV = 0.0; - m_jumpSpeed = 0.0; m_isJumping = false; + + m_speed = vect3D(0, 0, 0); +} + +void Player::testPoint(vect3D pos, vect3D *speed) { + if(!passable(pos.x + speed->x, pos.y, pos.z)) speed->x = 0; + if(!passable(pos.x, pos.y + speed->y, pos.z)) speed->y = 0; + if(!passable(pos.x, pos.y, pos.z + speed->z)) { + if(speed->z < 0 && m_isJumping) m_isJumping = false; + speed->z = 0; + } } void Player::move(float distance, float direction) { direction += m_angleH; - float vx = distance * cos(direction * M_PI / 180.0); - if((passable(m_x + ((vx > 0) ? vx + 0.2 : vx - 0.2) , m_y, m_eyeheight)) && - (passable(m_x + ((vx > 0) ? vx + 0.2 : vx - 0.2) , m_y, m_eyeheight - PLAYER_HEIGHT + 0.3))) { - m_x += vx; - } + m_speed.x = distance * cos(direction * M_PI / 180.0); + m_speed.y = distance * sin(direction * M_PI / 180.0); +} + +void Player::update() { + testPoint(vect3D(m_x - 0.2, m_y - 0.2, m_eyeheight - PLAYER_HEIGHT), &m_speed); + testPoint(vect3D(m_x + 0.2, m_y - 0.2, m_eyeheight - PLAYER_HEIGHT), &m_speed); + testPoint(vect3D(m_x - 0.2, m_y + 0.2, m_eyeheight - PLAYER_HEIGHT), &m_speed); + testPoint(vect3D(m_x + 0.2, m_y + 0.2, m_eyeheight - PLAYER_HEIGHT), &m_speed); + testPoint(vect3D(m_x - 0.2, m_y - 0.2, m_eyeheight + (2 - PLAYER_HEIGHT - 0.01)), &m_speed); + testPoint(vect3D(m_x + 0.2, m_y - 0.2, m_eyeheight + (2 - PLAYER_HEIGHT - 0.01)), &m_speed); + testPoint(vect3D(m_x - 0.2, m_y + 0.2, m_eyeheight + (2 - PLAYER_HEIGHT - 0.01)), &m_speed); + testPoint(vect3D(m_x + 0.2, m_y + 0.2, m_eyeheight + (2 - PLAYER_HEIGHT - 0.01)), &m_speed); - float vy = distance * sin(direction * M_PI / 180.0); - if((passable(m_x, m_y + ((vy > 0) ? vy + 0.2 : vy - 0.2), m_eyeheight)) && - (passable(m_x, m_y + ((vy > 0) ? vy + 0.2 : vy - 0.2), m_eyeheight - PLAYER_HEIGHT + 0.3))) { - m_y += vy; - } + m_x += m_speed.x; + m_y += m_speed.y; + m_eyeheight += m_speed.z; + + m_speed = vect3D(0, 0, 0); } void Player::jump() { - if((m_isJumping) && - (passable(m_x, m_y, m_eyeheight + m_jumpSpeed + 0.5))) { + /*if((m_isJumping) && + (passable(m_x, m_y, m_eyeheight + m_jumpSpeed))) { m_eyeheight += m_jumpSpeed; m_jumpSpeed -= GRAVITY; if((m_jumpSpeed < 0) && - ((!passable(m_x, m_y, m_eyeheight - PLAYER_HEIGHT - m_jumpSpeed - 0.3)) /* || - (!passable(m_x + 0.25, m_y, m_eyeheight - PLAYER_HEIGHT - m_jumpSpeed - 0.1)) || - (!passable(m_x, m_y + 0.25, m_eyeheight - PLAYER_HEIGHT - m_jumpSpeed - 0.1)) || - (!passable(m_x + 0.25, m_y + 0.25, m_eyeheight - PLAYER_HEIGHT - m_jumpSpeed - 0.1)) */ )) { + ((!passable(m_x, m_y, m_eyeheight - PLAYER_HEIGHT - m_jumpSpeed - 0.3)))) { m_jumpSpeed = 0.0; m_isJumping = false; } else if((m_jumpSpeed >= 0) && - ((!passable(m_x, m_y, m_eyeheight + m_jumpSpeed + 0.5)) /* || - (!passable(m_x + 0.25, m_y, m_eyeheight + m_jumpSpeed + 0.5)) || - (!passable(m_x, m_y + 0.25, m_eyeheight + m_jumpSpeed + 0.5)) || - (!passable(m_x + 0.25, m_y + 0.25, m_eyeheight + m_jumpSpeed + 0.5)) */ )) { + ((!passable(m_x, m_y, m_eyeheight + m_jumpSpeed)))) { m_jumpSpeed = 0.0; } } - else if((passable(m_x, m_y, m_eyeheight - PLAYER_HEIGHT - m_jumpSpeed - 0.3)) /*&& - (passable(m_x, m_y, m_eyeheight - PLAYER_HEIGHT - m_jumpSpeed - 0.1))*/) { + else if((passable(m_x, m_y, m_eyeheight - PLAYER_HEIGHT - m_jumpSpeed - 0.3))) { m_jumpSpeed = 0.0; m_isJumping = true; + }*/ + if(m_isJumping) { + m_speed.z -= GRAVITY; } - +} + +void Player::fly() { + m_speed.z = FLY_SPEED; +} + +void Player::land() { + m_speed.z = -FLY_SPEED; } void Player::turnH(float angle) { @@ -115,28 +142,17 @@ void Player::turnV(float angle) { } } -void Player::fly() { - if(m_eyeheight < 256) m_eyeheight += FLY_SPEED; -} - -void Player::land() { - m_eyeheight -= FLY_SPEED; -} - void Player::watch() { gluLookAt( - // Eye position - m_x, m_y, m_eyeheight, - - // Point targeted - pointTargetedx(), - pointTargetedy(), - pointTargetedz(), - - // z is the vertical - 0, 0, 1); - - //cout << "Eyepos: (" << int(m_x) << " ; " << int(m_y) << " ; " << int(m_eyeheight) << ")" << endl; - //cout << "Pt targeted: (" << pointTargetedx() << " ; " << pointTargetedy() << " ; " << pointTargetedz() << ")" << endl; + // Eye position + m_x, m_y, m_eyeheight, + + // Point targeted + pointTargetedx(), + pointTargetedy(), + pointTargetedz(), + + // z is the vertical + 0, 0, 1); }