Added height at location
This commit is contained in:
+5
-3
@@ -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;
|
||||
|
||||
+6
-4
@@ -7,8 +7,10 @@
|
||||
#include <string>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -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<Vertex> vertices;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -22,5 +22,6 @@ public:
|
||||
void draw();
|
||||
void update(float elapsedTime);
|
||||
bool isPlayerPositionValid();
|
||||
float getHeight(float x, float y);
|
||||
};
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
+14
-13
@@ -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": [
|
||||
{
|
||||
|
||||
+4
-3
@@ -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": [
|
||||
{
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"worlds": [
|
||||
"worlds/fire.json",
|
||||
"worlds/ice.json"
|
||||
"worlds/ice.json",
|
||||
"worlds/fire.json"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user