diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index b5ac860..ad61da7 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -78,19 +78,21 @@ void CrystalPoint::update() if (player->rotation.x < -90) player->rotation.x = -90; - float speed = 20; + float speed = 2; Vec3f oldPosition = player->position; if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false); if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false); if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false); if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false); - if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true); - if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true); + //if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true); + //if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true); if (!worldhandler->isPlayerPositionValid()) player->position = oldPosition; + player->position.y = worldhandler->getHeight(player->position.x, player->position.z); + worldhandler->update(deltaTime); mousePosition = mousePosition + mouseOffset; diff --git a/HeightMap.cpp b/HeightMap.cpp index 7bad77b..018ad4d 100644 --- a/HeightMap.cpp +++ b/HeightMap.cpp @@ -7,8 +7,10 @@ #include -HeightMap::HeightMap(const std::string &file) +HeightMap::HeightMap(const std::string &file, float scale) { + this->scale = scale; + int bpp; unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4); @@ -90,7 +92,7 @@ void HeightMap::Draw() glDisableClientState(GL_NORMAL_ARRAY); } -float HeightMap::GetHeigth(float x, float y) +float HeightMap::GetHeight(float x, float y) { x /= scale; y /= scale; @@ -105,14 +107,14 @@ float HeightMap::GetHeigth(float x, float y) float lowervalue = ((b.y - c.y)*(a.x - c.x) + (c.x - b.x)*(a.y - c.y)); - float labda1 = ((b.y - c.y)*(x - c.x) + (c.x - b.x)*(y - c.y))/ lowervalue; + float labda1 = ((b.y - c.y)*(x - c.x) + (c.x - b.x)*(y - c.y)) / lowervalue; float labda2 = ((c.y - a.y)*(x - c.x) + (a.x - c.x)*(y - c.y)) / lowervalue; float labda3 = 1 - labda1 - labda2; Vec3f z = Vec3f(a.x, a.y, a.z) * labda1 + Vec3f(b.x, b.y, b.z) * labda2 + Vec3f(c.x, c.y, c.z) * labda3; - + return z.y; } diff --git a/HeightMap.h b/HeightMap.h index a6066d8..4c3c445 100644 --- a/HeightMap.h +++ b/HeightMap.h @@ -12,13 +12,13 @@ private: int width; GLuint imageIndex; - int scale = 1; + int scale; public: - HeightMap(const std::string &file); + HeightMap(const std::string &file, float scale); ~HeightMap(); void Draw(); - float GetHeigth(float x, float y); + float GetHeight(float x, float y); void SetTexture(const std::string &file); std::vector vertices; diff --git a/World.cpp b/World.cpp index 61bc9eb..35cea9f 100644 --- a/World.cpp +++ b/World.cpp @@ -25,7 +25,11 @@ World::World(const std::string &fileName) if (v["objects"].isNull()) std::cout << "Invalid world file: objects - " << fileName << "\n"; - heightmap = new HeightMap(v["world"]["heightmap"].asString()); + float scale = 1.0f; + if (!v["world"]["scale"].isNull()) + scale = v["world"]["scale"].asFloat(); + + heightmap = new HeightMap(v["world"]["heightmap"].asString(), scale); if(!v["world"]["texture"].isNull()) heightmap->SetTexture(v["world"]["texture"].asString()); @@ -86,6 +90,11 @@ World::~World() delete heightmap; } +float World::getHeight(float x, float y) +{ + return heightmap->GetHeight(x, y); +} + void World::draw() { player->setCamera(); diff --git a/World.h b/World.h index a558aa1..0df2a06 100644 --- a/World.h +++ b/World.h @@ -22,5 +22,6 @@ public: void draw(); void update(float elapsedTime); bool isPlayerPositionValid(); + float getHeight(float x, float y); }; diff --git a/WorldHandler.cpp b/WorldHandler.cpp index 917972c..97fa7a2 100644 --- a/WorldHandler.cpp +++ b/WorldHandler.cpp @@ -99,6 +99,14 @@ bool WorldHandler::isPlayerPositionValid(void) return world->isPlayerPositionValid(); } +float WorldHandler::getHeight(float x, float y) +{ + if (!loadingWorld) + return world->getHeight(x, y); + else + return 0.0f; +} + void WorldHandler::Navigate(const std::string &fileName) { diff --git a/WorldHandler.h b/WorldHandler.h index 7688cda..0cde251 100644 --- a/WorldHandler.h +++ b/WorldHandler.h @@ -25,6 +25,7 @@ public: void update(float deltaTime); bool isPlayerPositionValid(void); + float getHeight(float x, float y); void Navigate(const std::string &fileName); void NextWorld(); diff --git a/worlds/fire.json b/worlds/fire.json index 3da48f5..5d1368a 100644 --- a/worlds/fire.json +++ b/worlds/fire.json @@ -1,20 +1,21 @@ { "world": { - "heightmap": "worlds/hell.png", - "texture": "worlds/helltexture.png", - "object-templates": [ - { - "color": 25, - "file": "models/boom/Boom.obj" - }, - { - "color": 23, - "file": "models/boom/Boom.obj" - } - ] + "heightmap": "worlds/hell.png", + "texture": "worlds/helltexture.png", + "scale": 1, + "object-templates": [ + { + "color": 25, + "file": "models/boom/Boom.obj" + }, + { + "color": 23, + "file": "models/boom/Boom.obj" + } + ] }, "player": { - "startposition": [ 100, 1.7, 100 ] + "startposition": [ 200, 40, 200 ] }, "objects": [ { diff --git a/worlds/ice.json b/worlds/ice.json index f0f8673..44b0137 100644 --- a/worlds/ice.json +++ b/worlds/ice.json @@ -1,10 +1,11 @@ { "world": { - "heightmap": "worlds/hmcs.png", - "texture": "worlds/hmcstexture.png" + "heightmap": "worlds/hmcs.png", + "texture": "worlds/hmcstexture.png", + "scale": 2 }, "player": { - "startposition": [ -100, 1.7, -100 ] + "startposition": [ 100, 20, 100 ] }, "objects": [ { diff --git a/worlds/worlds.json b/worlds/worlds.json index 5516fea..085ff36 100644 --- a/worlds/worlds.json +++ b/worlds/worlds.json @@ -1,6 +1,6 @@ { "worlds": [ - "worlds/fire.json", - "worlds/ice.json" + "worlds/ice.json", + "worlds/fire.json" ] } \ No newline at end of file