2 #include "EntityController.hpp"
3 #include "EntityDerivative.hpp"
4 #include "EntityState.hpp"
8 #include "ChunkIndex.hpp"
9 #include "EntityCollision.hpp"
10 #include "WorldCollision.hpp"
11 #include "../app/Assets.hpp"
12 #include "../graphics/Format.hpp"
13 #include "../graphics/Viewport.hpp"
19 #include <glm/gtx/io.hpp>
20 #include <glm/gtx/quaternion.hpp>
21 #include <glm/gtx/transform.hpp>
26 Entity::Entity() noexcept
33 , heading(0.0f, 0.0f, -1.0f)
37 , world_collision(false)
39 , owns_controller(false) {
43 Entity::~Entity() noexcept {
47 Entity::Entity(const Entity &other) noexcept
52 , bounds(other.bounds)
54 , max_vel(other.max_vel)
55 , max_force(other.max_force)
57 , world_collision(other.world_collision)
59 , owns_controller(false) {
63 void Entity::SetController(EntityController *c) noexcept {
66 owns_controller = true;
69 void Entity::SetController(EntityController &c) noexcept {
72 owns_controller = false;
75 void Entity::UnsetController() noexcept {
76 if (ctrl && owns_controller) {
82 glm::vec3 Entity::ControlForce(const EntityState &s) const noexcept {
83 if (HasController()) {
84 return GetController().ControlForce(*this, s);
90 void Entity::Position(const glm::ivec3 &c, const glm::vec3 &b) noexcept {
95 void Entity::Position(const glm::vec3 &pos) noexcept {
96 state.block_pos = pos;
97 state.AdjustPosition();
100 void Entity::TurnHead(float dp, float dy) noexcept {
101 SetHead(state.pitch + dp, state.yaw + dy);
104 void Entity::SetHead(float p, float y) noexcept {
107 // TODO: I feel like this could be delayed
111 glm::mat4 Entity::Transform(const glm::ivec3 &reference) const noexcept {
112 return state.Transform(reference);
115 glm::mat4 Entity::ViewTransform(const glm::ivec3 &reference) const noexcept {
116 glm::mat4 transform = view_local;
117 transform[3] += glm::vec4(state.RelativePosition(reference), 0.0f);
121 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
122 glm::mat4 transform = ViewTransform(chunk_offset);
123 return Ray{ glm::vec3(transform[3]), -glm::vec3(transform[2]) };
126 void Entity::UpdateModel() noexcept {
127 state.AdjustHeading();
129 Part::State &body_state = model.BodyState();
130 Part::State &eyes_state = model.EyesState();
131 if (&body_state != &eyes_state) {
132 body_state.orientation = glm::quat(glm::vec3(0.0f, state.yaw, 0.0f));
133 eyes_state.orientation = glm::quat(glm::vec3(state.pitch, 0.0f, 0.0f));
135 eyes_state.orientation = glm::quat(glm::vec3(state.pitch, state.yaw, 0.0f));
140 void Entity::Update(float dt) {
143 if (HasController()) {
144 GetController().Update(*this, dt);
148 void Entity::UpdateView() noexcept {
149 // create local transform
150 view_local = Transform(ChunkCoords());
151 // clear the translation part
152 view_local[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
153 // add the model's eyes translation, if any
155 view_local *= model.EyesTransform();
159 void Entity::UpdateHeading() noexcept {
160 speed = length(Velocity());
161 if (speed > std::numeric_limits<float>::epsilon()) {
162 heading = Velocity() / speed;
165 // use -Z (forward axis) of local view transform
166 heading = -glm::vec3(view_local[2]);
171 EntityController::~EntityController() {
175 bool EntityController::MaxOutForce(
177 const glm::vec3 &add,
180 if (iszero(add) || any(isnan(add))) {
183 float current = iszero(out) ? 0.0f : length(out);
184 float remain = max - current;
185 if (remain <= 0.0f) {
188 float additional = length(add);
189 if (additional > remain) {
190 out += normalize(add) * remain;
199 EntityState::EntityState()
203 , orient(1.0f, 0.0f, 0.0f, 0.0f)
209 void EntityState::AdjustPosition() noexcept {
210 while (block_pos.x >= Chunk::width) {
211 block_pos.x -= Chunk::width;
214 while (block_pos.x < 0) {
215 block_pos.x += Chunk::width;
218 while (block_pos.y >= Chunk::height) {
219 block_pos.y -= Chunk::height;
222 while (block_pos.y < 0) {
223 block_pos.y += Chunk::height;
226 while (block_pos.z >= Chunk::depth) {
227 block_pos.z -= Chunk::depth;
230 while (block_pos.z < 0) {
231 block_pos.z += Chunk::depth;
236 void EntityState::AdjustHeading() noexcept {
237 glm::clamp(pitch, -PI_0p5, PI_0p5);
246 glm::mat4 EntityState::Transform(const glm::ivec3 &reference) const noexcept {
247 const glm::vec3 translation = RelativePosition(reference);
248 glm::mat4 transform(toMat4(orient));
249 transform[3] = glm::vec4(translation, 1.0f);
254 Player::Player(Entity &e, ChunkIndex &c)
265 bool Player::SuitableSpawn(BlockLookup &spawn_block) const noexcept {
266 if (!spawn_block || spawn_block.GetType().collide_block) {
270 BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
271 if (!head_block || head_block.GetType().collide_block) {
278 void Player::Update(int dt) {
279 chunks.Rebase(entity.ChunkCoords());
283 World::World(const BlockTypeRegistry &types, const Config &config)
289 , light_direction(config.light_direction)
290 , fog_density(config.fog_density) {
299 Player *World::AddPlayer(const std::string &name) {
300 for (Player &p : players) {
301 if (p.Name() == name) {
305 Entity &entity = AddEntity();
307 entity.Bounds({ { -0.4f, -0.9f, -0.4f }, { 0.4f, 0.9f, 0.4f } });
308 entity.WorldCollidable(true);
309 ChunkIndex &index = chunks.MakeIndex(entity.ChunkCoords(), 6);
310 players.emplace_back(entity, index);
311 return &players.back();
314 Player *World::AddPlayer(const std::string &name, std::uint32_t id) {
315 for (Player &p : players) {
316 if (p.Name() == name) {
320 Entity *entity = AddEntity(id);
325 entity->Bounds({ { -0.4f, -0.9f, -0.4f }, { 0.4f, 0.9f, 0.4f } });
326 entity->WorldCollidable(true);
327 ChunkIndex &index = chunks.MakeIndex(entity->ChunkCoords(), 6);
328 players.emplace_back(*entity, index);
329 return &players.back();
332 Entity &World::AddEntity() {
333 if (entities.empty()) {
334 entities.emplace_back();
335 entities.back().ID(1);
336 return entities.back();
338 if (entities.back().ID() < std::numeric_limits<std::uint32_t>::max()) {
339 std::uint32_t id = entities.back().ID() + 1;
340 entities.emplace_back();
341 entities.back().ID(id);
342 return entities.back();
344 std::uint32_t id = 1;
345 auto position = entities.begin();
346 auto end = entities.end();
347 while (position != end && position->ID() == id) {
351 auto entity = entities.emplace(position);
356 Entity *World::AddEntity(std::uint32_t id) {
357 if (entities.empty() || entities.back().ID() < id) {
358 entities.emplace_back();
359 entities.back().ID(id);
360 return &entities.back();
363 auto position = entities.begin();
364 auto end = entities.end();
365 while (position != end && position->ID() < id) {
368 if (position != end && position->ID() == id) {
371 auto entity = entities.emplace(position);
376 Entity &World::ForceAddEntity(std::uint32_t id) {
377 if (entities.empty() || entities.back().ID() < id) {
378 entities.emplace_back();
379 entities.back().ID(id);
380 return entities.back();
383 auto position = entities.begin();
384 auto end = entities.end();
385 while (position != end && position->ID() < id) {
388 if (position != end && position->ID() == id) {
391 auto entity = entities.emplace(position);
404 bool CandidateLess(const Candidate &a, const Candidate &b) {
405 return a.dist < b.dist;
408 std::vector<Candidate> candidates;
412 bool World::Intersection(
415 const Chunk::Pos &reference,
420 for (Chunk &cur_chunk : chunks) {
422 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
423 candidates.push_back({ &cur_chunk, cur_dist });
427 if (candidates.empty()) return false;
429 std::sort(candidates.begin(), candidates.end(), CandidateLess);
431 coll.chunk = nullptr;
433 coll.depth = std::numeric_limits<float>::infinity();
435 for (Candidate &cand : candidates) {
436 if (cand.dist > coll.depth) continue;
437 WorldCollision cur_coll;
438 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
439 if (cur_coll.depth < coll.depth) {
448 bool World::Intersection(
451 const Entity &reference,
452 EntityCollision &coll
454 coll.entity = nullptr;
455 coll.depth = std::numeric_limits<float>::infinity();
456 for (Entity &cur_entity : entities) {
457 if (&cur_entity == &reference) {
461 glm::vec3 cur_normal;
462 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
463 // TODO: fine grained check goes here? maybe?
464 if (cur_dist < coll.depth) {
465 coll.entity = &cur_entity;
466 coll.depth = cur_dist;
467 coll.normal = cur_normal;
475 bool World::Intersection(const Entity &e, const EntityState &s, std::vector<WorldCollision> &col) {
476 AABB box = e.Bounds();
477 Chunk::Pos reference = s.chunk_pos;
478 glm::mat4 M = s.Transform(reference);
479 return Intersection(box, M, reference, col);
482 bool World::Intersection(
485 const glm::ivec3 &reference,
486 std::vector<WorldCollision> &col
489 for (Chunk &cur_chunk : chunks) {
490 if (manhattan_radius(cur_chunk.Position() - reference) > 1) {
491 // chunk is not one of the 3x3x3 surrounding the entity
492 // since there's no entity which can extent over 16 blocks, they can be skipped
495 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
502 void World::Update(int dt) {
503 float fdt(dt * 0.001f);
504 for (Entity &entity : entities) {
507 for (Entity &entity : entities) {
510 for (Player &player : players) {
513 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
514 if (iter->CanRemove()) {
515 iter = RemoveEntity(iter);
522 void World::Update(Entity &entity, float dt) {
523 EntityState state(entity.GetState());
525 EntityDerivative a(CalculateStep(entity, state, 0.0f, EntityDerivative()));
526 EntityDerivative b(CalculateStep(entity, state, dt * 0.5f, a));
527 EntityDerivative c(CalculateStep(entity, state, dt * 0.5f, b));
528 EntityDerivative d(CalculateStep(entity, state, dt, c));
531 constexpr float sixth = 1.0f / 6.0f;
532 f.position = sixth * ((a.position + 2.0f * (b.position + c.position)) + d.position);
533 f.velocity = sixth * ((a.velocity + 2.0f * (b.velocity + c.velocity)) + d.velocity);
535 state.block_pos += f.position * dt;
536 state.velocity += f.velocity * dt;
537 state.AdjustPosition();
539 entity.SetState(state);
542 EntityDerivative World::CalculateStep(
543 const Entity &entity,
544 const EntityState &cur,
546 const EntityDerivative &delta
548 EntityState next(cur);
549 next.block_pos += delta.position * dt;
550 next.velocity += delta.velocity * dt;
551 next.AdjustPosition();
553 if (dot(next.velocity, next.velocity) > entity.MaxVelocity() * entity.MaxVelocity()) {
554 next.velocity = normalize(next.velocity) * entity.MaxVelocity();
557 EntityDerivative out;
558 out.position = next.velocity;
559 out.velocity = CalculateForce(entity, next); // by mass = 1kg
563 glm::vec3 World::CalculateForce(
564 const Entity &entity,
565 const EntityState &state
567 glm::vec3 force(ControlForce(entity, state) + CollisionForce(entity, state) + Gravity(entity, state));
568 if (dot(force, force) > entity.MaxControlForce() * entity.MaxControlForce()) {
569 return normalize(force) * entity.MaxControlForce();
575 glm::vec3 World::ControlForce(
576 const Entity &entity,
577 const EntityState &state
579 return entity.ControlForce(state);
584 std::vector<WorldCollision> col;
588 glm::vec3 World::CollisionForce(
589 const Entity &entity,
590 const EntityState &state
593 if (entity.WorldCollidable() && Intersection(entity, state, col)) {
594 // determine displacement for each cardinal axis and move entity accordingly
595 glm::vec3 min_pen(0.0f);
596 glm::vec3 max_pen(0.0f);
597 for (const WorldCollision &c : col) {
598 if (!c.Blocks()) continue;
599 glm::vec3 local_pen(c.normal * c.depth);
600 // swap if neccessary (normal may point away from the entity)
601 if (dot(c.normal, state.RelativePosition(c.ChunkPos()) - c.BlockCoords()) > 0) {
604 min_pen = min(min_pen, local_pen);
605 max_pen = max(max_pen, local_pen);
607 glm::vec3 correction(0.0f);
608 // only apply correction for axes where penetration is only in one direction
609 for (std::size_t i = 0; i < 3; ++i) {
610 if (min_pen[i] < -std::numeric_limits<float>::epsilon()) {
611 if (max_pen[i] < std::numeric_limits<float>::epsilon()) {
612 correction[i] = -min_pen[i];
615 correction[i] = -max_pen[i];
618 // correction may be zero in which case normalize() returns NaNs
619 if (dot(correction, correction) < std::numeric_limits<float>::epsilon()) {
620 return glm::vec3(0.0f);
622 glm::vec3 normal(normalize(correction));
623 glm::vec3 normal_velocity(normal * dot(state.velocity, normal));
624 // apply force proportional to penetration
625 // use velocity projected onto normal as damper
626 constexpr float k = 1000.0f; // spring constant
627 constexpr float b = 10.0f; // damper constant
628 const glm::vec3 x(-correction); // endpoint displacement from equilibrium in m
629 const glm::vec3 v(normal_velocity); // relative velocity between endpoints in m/s
630 return (((-k) * x) - (b * v)); // times 1kg/s, in kg*m/s²
632 return glm::vec3(0.0f);
636 glm::vec3 World::Gravity(
637 const Entity &entity,
638 const EntityState &state
640 return glm::vec3(0.0f);
643 World::EntityHandle World::RemoveEntity(EntityHandle &eh) {
645 for (auto player = players.begin(), end = players.end(); player != end;) {
646 if (&player->GetEntity() == &*eh) {
647 chunks.UnregisterIndex(player->GetChunks());
648 player = players.erase(player);
654 return entities.erase(eh);
658 void World::Render(Viewport &viewport) {
659 DirectionalLighting &entity_prog = viewport.EntityProgram();
660 entity_prog.SetLightDirection(light_direction);
661 entity_prog.SetFogDensity(fog_density);
663 for (Entity &entity : entities) {
664 entity.Render(entity.Transform(players.front().GetEntity().ChunkCoords()), entity_prog);