]> git.localhorst.tv Git - blank.git/blobdiff - src/world/world.cpp
ignore obscured faces in collision response
[blank.git] / src / world / world.cpp
index 4d01af394a6d37e386cec343449d1f03ae3e0d82..199343c33103d2c7377fff7338f0594e6852f96b 100644 (file)
@@ -220,8 +220,8 @@ void Entity::OrientBody(float dt) noexcept {
 }
 
 void Entity::OrientHead(float dt) noexcept {
-       // maximum yaw of head (90°)
-       constexpr float max_head_yaw = PI_0p5;
+       // maximum yaw of head (60°)
+       constexpr float max_head_yaw = PI / 3.0f;
        // use local Y as up
        const glm::vec3 up(model_transform[1]);
        // if yaw is bigger than max, rotate the body to accomodate
@@ -605,8 +605,8 @@ void World::Update(Entity &entity, float dt) {
 
        EntityDerivative f;
        constexpr float sixth = 1.0f / 6.0f;
-       f.position = sixth * ((a.position + 2.0f * (b.position + c.position)) + d.position);
-       f.velocity = sixth * ((a.velocity + 2.0f * (b.velocity + c.velocity)) + d.velocity);
+       f.position = sixth * (a.position + 2.0f * (b.position + c.position) + d.position);
+       f.velocity = sixth * (a.velocity + 2.0f * (b.velocity + c.velocity) + d.velocity);
 
        state.pos.block += f.position * dt;
        state.velocity += f.velocity * dt;
@@ -699,11 +699,19 @@ glm::vec3 World::CombinedInterpenetration(
        glm::vec3 max_pen(0.0f);
        for (const WorldCollision &c : col) {
                if (!c.Blocks()) continue;
-               glm::vec3 local_pen(c.normal * c.depth);
+               glm::vec3 normal(c.normal);
                // swap if neccessary (normal may point away from the entity)
-               if (dot(c.normal, state.RelativePosition(c.ChunkPos()) - c.BlockCoords()) > 0) {
-                       local_pen *= -1;
+               if (dot(normal, state.RelativePosition(c.ChunkPos()) - c.BlockCoords()) < 0) {
+                       normal = -normal;
                }
+               // check if block surface is "inside"
+               Block::Face coll_face = Block::NormalFace(normal);
+               BlockLookup neighbor(c.chunk, c.BlockPos(), coll_face);
+               if (neighbor && neighbor.FaceFilled(Block::Opposite(coll_face))) {
+                       // yep, so ignore this contact
+                       continue;
+               }
+               glm::vec3 local_pen(normal * c.depth);
                min_pen = min(min_pen, local_pen);
                max_pen = max(max_pen, local_pen);
        }
@@ -725,7 +733,22 @@ glm::vec3 World::Gravity(
        const Entity &entity,
        const EntityState &state
 ) {
-       return glm::vec3(0.0f);
+       glm::vec3 force(0.0f);
+       ExactLocation::Coarse begin(state.pos.chunk - 1);
+       ExactLocation::Coarse end(state.pos.chunk + 2);
+
+       for (ExactLocation::Coarse pos(begin); pos.z < end.z; ++pos.z) {
+               for (pos.y = begin.y; pos.y < end.y; ++pos.y) {
+                       for (pos.x = begin.x; pos.x < end.x; ++pos.x) {
+                               Chunk *chunk = chunks.Get(pos);
+                               if (chunk) {
+                                       force += chunk->GravityAt(state.pos);
+                               }
+                       }
+               }
+       }
+
+       return force;
 }
 
 World::EntityHandle World::RemoveEntity(EntityHandle &eh) {