Make it so you can walk around. Add basic collision detection. Update Makefile to support Windows builds.
This commit is contained in:
+3
-3
@@ -10,14 +10,14 @@ private:
|
||||
glm::mat4 projection_matrix;
|
||||
float field_of_view;
|
||||
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
int width;
|
||||
int height;
|
||||
|
||||
void update_projection_matrix(int width, int height);
|
||||
public:
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
Camera();
|
||||
Camera(int width, int height);
|
||||
|
||||
|
||||
+6
-7
@@ -9,6 +9,9 @@
|
||||
#include <iostream>
|
||||
|
||||
class Chunk {
|
||||
public:
|
||||
static constexpr int horiz_size = 16;
|
||||
static constexpr int vert_size = 256;
|
||||
private:
|
||||
GLuint VAO = 0;
|
||||
GLuint VBOs[4];
|
||||
@@ -16,14 +19,7 @@ private:
|
||||
int mesh_version=0;
|
||||
int current_version=1;
|
||||
|
||||
static constexpr int horiz_size = 16;
|
||||
static constexpr int vert_size = 256;
|
||||
|
||||
int blocks[horiz_size * horiz_size * vert_size];
|
||||
|
||||
inline void set_block(int x, int y, int z, int block);
|
||||
inline int get_block(int x, int y, int z);
|
||||
|
||||
public:
|
||||
int x;
|
||||
int z;
|
||||
@@ -40,6 +36,9 @@ public:
|
||||
glm::mat4 get_transformation_matrix();
|
||||
|
||||
void select();
|
||||
|
||||
void set_block(int x, int y, int z, int block);
|
||||
int get_block(int x, int y, int z);
|
||||
};
|
||||
|
||||
#endif
|
||||
+54
-10
@@ -34,25 +34,69 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
|
||||
key_states[key] = 0;
|
||||
}
|
||||
|
||||
float vel_forward, vel_y, vel_right;
|
||||
void loop(float deltatime, GLFWwindow* w) {
|
||||
float speed = 5.0f;
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glClearColor(0.2f, 0.6f, 0.7f, 1.0f);
|
||||
std::cout << "Deltatime: " << deltatime << "\n";
|
||||
|
||||
float speed = 20.0f;
|
||||
glm::vec3 old_position = c->get_position();
|
||||
glm::vec3 new_position = old_position;
|
||||
|
||||
glm::vec3 forward(
|
||||
glm::sin(c->rotation.y),
|
||||
0,
|
||||
glm::cos(c->rotation.y)
|
||||
);
|
||||
|
||||
glm::vec3 right(
|
||||
glm::sin(c->rotation.y + 1.57f),
|
||||
0,
|
||||
glm::cos(c->rotation.y + 1.57f)
|
||||
);
|
||||
|
||||
new_position.y += vel_y * deltatime;
|
||||
if (world->collide(new_position.x, new_position.y-0.5f, new_position.z) || world->collide(new_position.x, new_position.y-1.5f, new_position.z)) {
|
||||
vel_y = 0.0f;
|
||||
new_position.y = old_position.y;
|
||||
}
|
||||
|
||||
vel_forward = 0.0f;
|
||||
vel_right = 0.0f;
|
||||
if (key_states[GLFW_KEY_W])
|
||||
c->move_by(glm::vec3(0.0f, 0.0f, -speed) * deltatime);
|
||||
if (key_states[GLFW_KEY_A])
|
||||
c->move_by(glm::vec3(-speed, 0.0f, 0.0f) * deltatime);
|
||||
vel_forward = -speed;
|
||||
if (key_states[GLFW_KEY_S])
|
||||
c->move_by(glm::vec3(0.0f, 0.0f, speed) * deltatime);
|
||||
vel_forward = speed;
|
||||
if (key_states[GLFW_KEY_A])
|
||||
vel_right = -speed;
|
||||
if (key_states[GLFW_KEY_D])
|
||||
c->move_by(glm::vec3(speed, 0.0f, 0.0f) * deltatime);
|
||||
if (key_states[GLFW_KEY_SPACE])
|
||||
c->change_position_by(glm::vec3(0.0f, speed, 0.0f) * deltatime);
|
||||
if (key_states[GLFW_KEY_LEFT_SHIFT])
|
||||
c->change_position_by(glm::vec3(0.0f, -speed, 0.0f) * deltatime);
|
||||
vel_right = speed;
|
||||
if (key_states[GLFW_KEY_SPACE] && vel_y == 0.0f)
|
||||
vel_y = 6.0f;
|
||||
|
||||
forward *= vel_forward;
|
||||
forward *= deltatime;
|
||||
|
||||
right *= vel_right;
|
||||
right *= deltatime;
|
||||
|
||||
new_position += forward;
|
||||
if (world->collide(new_position.x, new_position.y-0.5f, new_position.z) || world->collide(new_position.x, new_position.y-1.5f, new_position.z)) {
|
||||
new_position.x = old_position.x;
|
||||
new_position.z = old_position.z;
|
||||
}
|
||||
|
||||
new_position += right;
|
||||
if (world->collide(new_position.x, new_position.y-0.5f, new_position.z) || world->collide(new_position.x, new_position.y-1.5f, new_position.z)) {
|
||||
new_position.x = old_position.x;
|
||||
new_position.z = old_position.z;
|
||||
}
|
||||
|
||||
c->set_position(new_position);
|
||||
|
||||
if (vel_y >= -50.0f)
|
||||
vel_y -= 9.81f * deltatime;
|
||||
|
||||
double current_x_pos, current_y_pos;
|
||||
glfwGetCursorPos(w, ¤t_x_pos, ¤t_y_pos);
|
||||
|
||||
@@ -128,4 +128,27 @@ void World::render(Camera* c, Shader* s) {
|
||||
|
||||
glDrawElements(GL_TRIANGLES, chunk->vertex_count, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
}
|
||||
|
||||
bool World::collide(float x, float y, float z) {
|
||||
int chunk_x = x / 16;
|
||||
int chunk_z = z / 16;
|
||||
|
||||
int chunk_rel_x = (int)x % 16;
|
||||
int chunk_rel_z = (int)z % 16;
|
||||
|
||||
if (chunk_rel_x < 0) {
|
||||
chunk_rel_x += (Chunk::horiz_size-1);
|
||||
chunk_x--;
|
||||
}
|
||||
if (chunk_rel_z < 0) {
|
||||
chunk_rel_z += (Chunk::horiz_size-1);
|
||||
chunk_z--;
|
||||
}
|
||||
|
||||
Chunk* c;
|
||||
if (!(c = get_chunk(chunk_x, chunk_z)))
|
||||
return false;
|
||||
|
||||
return c->get_block(chunk_rel_x, y, chunk_rel_z);
|
||||
}
|
||||
@@ -44,6 +44,8 @@ public:
|
||||
static Chunk* get_chunk(int x, int z);
|
||||
|
||||
void render(Camera* c, Shader* s);
|
||||
|
||||
bool collide(float x, float y, float z);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user