[ClientPlayer] Now using AABB for collision checks.

This commit is contained in:
Quentin Bazin 2020-01-27 18:10:20 +09:00
parent 42232049e0
commit 7ce3321cb8
3 changed files with 22 additions and 22 deletions

View File

@ -60,7 +60,7 @@ class ClientPlayer : public Player {
const gk::Camera &camera() { return m_camera; }
private:
void testPoint(const ClientWorld &world, glm::vec3 pos, glm::vec3 &speed);
void testPoint(const ClientWorld &world, float x, float y, float z, glm::vec3 &speed);
static ClientPlayer *s_instance;
@ -78,6 +78,8 @@ class ClientPlayer : public Player {
const float m_gravity = 0.001;
const float m_jumpSpeed = 0.06f;
gk::FloatBox m_hitbox;
};
#endif // CLIENTPLAYER_HPP_

View File

@ -81,8 +81,6 @@ void PlayerBox::updateVertexBuffer() {
void PlayerBox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform();
states.vertexAttributes = gk::VertexAttribute::Coord3d | gk::VertexAttribute::Color;
target.draw(m_vbo, GL_QUADS, 0, 24, states);
}

View File

@ -32,6 +32,8 @@ ClientPlayer::ClientPlayer(gk::Camera &camera) : m_camera(camera) {
m_angleH = -90.0;
m_angleV = 0.01;
m_hitbox = gk::FloatBox{-0.2, -1.8, -0.2, 0.4, 1.8, 0.4};
m_camera.setPosition(m_x, m_y, m_z);
m_camera.setTargetPosition(pointTargetedX(), pointTargetedY(), pointTargetedZ());
}
@ -134,19 +136,17 @@ void ClientPlayer::updatePosition(const ClientWorld &world) {
m_velocity.y = 0;
}
// FIXME: Use AABB for more precision
void ClientPlayer::checkCollisions(const ClientWorld &world) {
const float PLAYER_HEIGHT = 1.8;
float eyeheight = m_y + PLAYER_HEIGHT - 1.4;
// testPoint(world, glm::vec3(m_x, m_y, m_z), m_velocity);
testPoint(world, glm::vec3(m_x - 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_z - 0.2), m_velocity);
testPoint(world, glm::vec3(m_x + 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_z - 0.2), m_velocity);
testPoint(world, glm::vec3(m_x - 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_z + 0.2), m_velocity);
testPoint(world, glm::vec3(m_x + 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_z + 0.2), m_velocity);
testPoint(world, glm::vec3(m_x - 0.2, eyeheight - 0.4, m_z - 0.2), m_velocity);
testPoint(world, glm::vec3(m_x + 0.2, eyeheight - 0.4, m_z - 0.2), m_velocity);
testPoint(world, glm::vec3(m_x - 0.2, eyeheight - 0.4, m_z + 0.2), m_velocity);
testPoint(world, glm::vec3(m_x + 0.2, eyeheight - 0.4, m_z + 0.2), m_velocity);
for (float x = m_x + m_hitbox.x ; x <= m_x + m_hitbox.x + m_hitbox.width + 0.1f ; x += 0.2f) {
for (float y = m_y + m_hitbox.y ; y <= m_y + m_hitbox.y + m_hitbox.height + 0.1f ; y += 0.9f) {
for (float z = m_z + m_hitbox.z ; z <= m_z + m_hitbox.z + m_hitbox.depth + 0.1f ; z += 0.2f) {
if (x == m_x + m_hitbox.x || x == m_x + m_hitbox.x + m_hitbox.width
|| y == m_y + m_hitbox.y || y == m_y + m_hitbox.y + m_hitbox.height
|| z == m_z + m_hitbox.z || z == m_z + m_hitbox.z + m_hitbox.depth)
testPoint(world, x, y, z, m_velocity);
}
}
}
}
bool passable(const ClientWorld &world, float x, float y, float z) {
@ -154,15 +154,15 @@ bool passable(const ClientWorld &world, float x, float y, float z) {
return !block || block == 8 || block == BlockType::Flower;
}
void ClientPlayer::testPoint(const ClientWorld &world, glm::vec3 pos, glm::vec3 &speed) {
void ClientPlayer::testPoint(const ClientWorld &world, float x, float y, float z, glm::vec3 &speed) {
// FIXME: Temporary fix, find the real problem!!!
if (pos.x < 0) --pos.x;
if (pos.y < 1) --pos.y;
if (pos.z < 0) --pos.z;
if (x < 0) --x;
if (y < 1) --y;
if (z < 0) --z;
if(!passable(world, pos.x + speed.x, pos.y, pos.z)) speed.x = 0;
if(!passable(world, pos.x, pos.y, pos.z + speed.z)) speed.z = 0;
if(!passable(world, pos.x, pos.y + speed.y, pos.z)) {
if(!passable(world, x + speed.x, y, z)) speed.x = 0;
if(!passable(world, x, y, z + speed.z)) speed.z = 0;
if(!passable(world, x, y + speed.y, z)) {
if(speed.y < 0 && m_isJumping) m_isJumping = false;
speed.y = 0;
}