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 {
161 heading = normalize(Velocity());
163 // use -Z (forward axis) of local view transform
164 heading = -glm::vec3(view_local[2]);
169 EntityController::~EntityController() {
173 bool EntityController::MaxOutForce(
175 const glm::vec3 &add,
178 if (iszero(add) || any(isnan(add))) {
181 float current = iszero(out) ? 0.0f : length(out);
182 float remain = max - current;
183 if (remain <= 0.0f) {
186 float additional = length(add);
187 if (additional > remain) {
188 out += normalize(add) * remain;
197 EntityState::EntityState()
201 , orient(1.0f, 0.0f, 0.0f, 0.0f)
207 void EntityState::AdjustPosition() noexcept {
208 while (block_pos.x >= Chunk::width) {
209 block_pos.x -= Chunk::width;
212 while (block_pos.x < 0) {
213 block_pos.x += Chunk::width;
216 while (block_pos.y >= Chunk::height) {
217 block_pos.y -= Chunk::height;
220 while (block_pos.y < 0) {
221 block_pos.y += Chunk::height;
224 while (block_pos.z >= Chunk::depth) {
225 block_pos.z -= Chunk::depth;
228 while (block_pos.z < 0) {
229 block_pos.z += Chunk::depth;
234 void EntityState::AdjustHeading() noexcept {
235 glm::clamp(pitch, -PI_0p5, PI_0p5);
244 glm::mat4 EntityState::Transform(const glm::ivec3 &reference) const noexcept {
245 const glm::vec3 translation = RelativePosition(reference);
246 glm::mat4 transform(toMat4(orient));
247 transform[3] = glm::vec4(translation, 1.0f);
252 Player::Player(Entity &e, ChunkIndex &c)
263 bool Player::SuitableSpawn(BlockLookup &spawn_block) const noexcept {
264 if (!spawn_block || spawn_block.GetType().collide_block) {
268 BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
269 if (!head_block || head_block.GetType().collide_block) {
276 void Player::Update(int dt) {
277 chunks.Rebase(entity.ChunkCoords());
281 World::World(const BlockTypeRegistry &types, const Config &config)
287 , light_direction(config.light_direction)
288 , fog_density(config.fog_density) {
297 Player *World::AddPlayer(const std::string &name) {
298 for (Player &p : players) {
299 if (p.Name() == name) {
303 Entity &entity = AddEntity();
305 entity.Bounds({ { -0.4f, -0.9f, -0.4f }, { 0.4f, 0.9f, 0.4f } });
306 entity.WorldCollidable(true);
307 ChunkIndex &index = chunks.MakeIndex(entity.ChunkCoords(), 6);
308 players.emplace_back(entity, index);
309 return &players.back();
312 Player *World::AddPlayer(const std::string &name, std::uint32_t id) {
313 for (Player &p : players) {
314 if (p.Name() == name) {
318 Entity *entity = AddEntity(id);
323 entity->Bounds({ { -0.4f, -0.9f, -0.4f }, { 0.4f, 0.9f, 0.4f } });
324 entity->WorldCollidable(true);
325 ChunkIndex &index = chunks.MakeIndex(entity->ChunkCoords(), 6);
326 players.emplace_back(*entity, index);
327 return &players.back();
330 Entity &World::AddEntity() {
331 if (entities.empty()) {
332 entities.emplace_back();
333 entities.back().ID(1);
334 return entities.back();
336 if (entities.back().ID() < std::numeric_limits<std::uint32_t>::max()) {
337 std::uint32_t id = entities.back().ID() + 1;
338 entities.emplace_back();
339 entities.back().ID(id);
340 return entities.back();
342 std::uint32_t id = 1;
343 auto position = entities.begin();
344 auto end = entities.end();
345 while (position != end && position->ID() == id) {
349 auto entity = entities.emplace(position);
354 Entity *World::AddEntity(std::uint32_t id) {
355 if (entities.empty() || entities.back().ID() < id) {
356 entities.emplace_back();
357 entities.back().ID(id);
358 return &entities.back();
361 auto position = entities.begin();
362 auto end = entities.end();
363 while (position != end && position->ID() < id) {
366 if (position != end && position->ID() == id) {
369 auto entity = entities.emplace(position);
374 Entity &World::ForceAddEntity(std::uint32_t id) {
375 if (entities.empty() || entities.back().ID() < id) {
376 entities.emplace_back();
377 entities.back().ID(id);
378 return entities.back();
381 auto position = entities.begin();
382 auto end = entities.end();
383 while (position != end && position->ID() < id) {
386 if (position != end && position->ID() == id) {
389 auto entity = entities.emplace(position);
402 bool CandidateLess(const Candidate &a, const Candidate &b) {
403 return a.dist < b.dist;
406 std::vector<Candidate> candidates;
410 bool World::Intersection(
413 const Chunk::Pos &reference,
418 for (Chunk &cur_chunk : chunks) {
420 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
421 candidates.push_back({ &cur_chunk, cur_dist });
425 if (candidates.empty()) return false;
427 std::sort(candidates.begin(), candidates.end(), CandidateLess);
429 coll.chunk = nullptr;
431 coll.depth = std::numeric_limits<float>::infinity();
433 for (Candidate &cand : candidates) {
434 if (cand.dist > coll.depth) continue;
435 WorldCollision cur_coll;
436 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
437 if (cur_coll.depth < coll.depth) {
446 bool World::Intersection(
449 const Entity &reference,
450 EntityCollision &coll
452 coll.entity = nullptr;
453 coll.depth = std::numeric_limits<float>::infinity();
454 for (Entity &cur_entity : entities) {
455 if (&cur_entity == &reference) {
459 glm::vec3 cur_normal;
460 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
461 // TODO: fine grained check goes here? maybe?
462 if (cur_dist < coll.depth) {
463 coll.entity = &cur_entity;
464 coll.depth = cur_dist;
465 coll.normal = cur_normal;
473 bool World::Intersection(const Entity &e, const EntityState &s, std::vector<WorldCollision> &col) {
474 AABB box = e.Bounds();
475 Chunk::Pos reference = s.chunk_pos;
476 glm::mat4 M = s.Transform(reference);
478 for (Chunk &cur_chunk : chunks) {
479 if (manhattan_radius(cur_chunk.Position() - reference) > 1) {
480 // chunk is not one of the 3x3x3 surrounding the entity
481 // since there's no entity which can extent over 16 blocks, they can be skipped
484 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
492 void World::Update(int dt) {
493 float fdt(dt * 0.001f);
494 for (Entity &entity : entities) {
497 for (Entity &entity : entities) {
500 for (Player &player : players) {
503 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
504 if (iter->CanRemove()) {
505 iter = RemoveEntity(iter);
512 void World::Update(Entity &entity, float dt) {
513 EntityState state(entity.GetState());
515 EntityDerivative a(CalculateStep(entity, state, 0.0f, EntityDerivative()));
516 EntityDerivative b(CalculateStep(entity, state, dt * 0.5f, a));
517 EntityDerivative c(CalculateStep(entity, state, dt * 0.5f, b));
518 EntityDerivative d(CalculateStep(entity, state, dt, c));
521 constexpr float sixth = 1.0f / 6.0f;
522 f.position = sixth * ((a.position + 2.0f * (b.position + c.position)) + d.position);
523 f.velocity = sixth * ((a.velocity + 2.0f * (b.velocity + c.velocity)) + d.velocity);
525 state.block_pos += f.position * dt;
526 state.velocity += f.velocity * dt;
527 state.AdjustPosition();
529 entity.SetState(state);
532 EntityDerivative World::CalculateStep(
533 const Entity &entity,
534 const EntityState &cur,
536 const EntityDerivative &delta
538 EntityState next(cur);
539 next.block_pos += delta.position * dt;
540 next.velocity += delta.velocity * dt;
541 next.AdjustPosition();
543 if (dot(next.velocity, next.velocity) > entity.MaxVelocity() * entity.MaxVelocity()) {
544 next.velocity = normalize(next.velocity) * entity.MaxVelocity();
547 EntityDerivative out;
548 out.position = next.velocity;
549 out.velocity = CalculateForce(entity, next); // by mass = 1kg
553 glm::vec3 World::CalculateForce(
554 const Entity &entity,
555 const EntityState &state
557 glm::vec3 force(ControlForce(entity, state) + CollisionForce(entity, state) + Gravity(entity, state));
558 if (dot(force, force) > entity.MaxControlForce() * entity.MaxControlForce()) {
559 return normalize(force) * entity.MaxControlForce();
565 glm::vec3 World::ControlForce(
566 const Entity &entity,
567 const EntityState &state
569 return entity.ControlForce(state);
574 std::vector<WorldCollision> col;
578 glm::vec3 World::CollisionForce(
579 const Entity &entity,
580 const EntityState &state
583 if (entity.WorldCollidable() && Intersection(entity, state, col)) {
584 // determine displacement for each cardinal axis and move entity accordingly
585 glm::vec3 min_pen(0.0f);
586 glm::vec3 max_pen(0.0f);
587 for (const WorldCollision &c : col) {
588 if (!c.Blocks()) continue;
589 glm::vec3 local_pen(c.normal * c.depth);
590 // swap if neccessary (normal may point away from the entity)
591 if (dot(c.normal, state.RelativePosition(c.ChunkPos()) - c.BlockCoords()) > 0) {
594 min_pen = min(min_pen, local_pen);
595 max_pen = max(max_pen, local_pen);
597 glm::vec3 correction(0.0f);
598 // only apply correction for axes where penetration is only in one direction
599 for (std::size_t i = 0; i < 3; ++i) {
600 if (min_pen[i] < -std::numeric_limits<float>::epsilon()) {
601 if (max_pen[i] < std::numeric_limits<float>::epsilon()) {
602 correction[i] = -min_pen[i];
605 correction[i] = -max_pen[i];
608 // correction may be zero in which case normalize() returns NaNs
609 if (dot(correction, correction) < std::numeric_limits<float>::epsilon()) {
610 return glm::vec3(0.0f);
612 glm::vec3 normal(normalize(correction));
613 glm::vec3 normal_velocity(normal * dot(state.velocity, normal));
614 // apply force proportional to penetration
615 // use velocity projected onto normal as damper
616 constexpr float k = 1000.0f; // spring constant
617 constexpr float b = 10.0f; // damper constant
618 const glm::vec3 x(-correction); // endpoint displacement from equilibrium in m
619 const glm::vec3 v(normal_velocity); // relative velocity between endpoints in m/s
620 return (((-k) * x) - (b * v)); // times 1kg/s, in kg*m/s²
622 return glm::vec3(0.0f);
626 glm::vec3 World::Gravity(
627 const Entity &entity,
628 const EntityState &state
630 return glm::vec3(0.0f);
633 World::EntityHandle World::RemoveEntity(EntityHandle &eh) {
635 for (auto player = players.begin(), end = players.end(); player != end;) {
636 if (&player->GetEntity() == &*eh) {
637 chunks.UnregisterIndex(player->GetChunks());
638 player = players.erase(player);
644 return entities.erase(eh);
648 void World::Render(Viewport &viewport) {
649 DirectionalLighting &entity_prog = viewport.EntityProgram();
650 entity_prog.SetLightDirection(light_direction);
651 entity_prog.SetFogDensity(fog_density);
653 for (Entity &entity : entities) {
654 entity.Render(entity.Transform(players.front().GetEntity().ChunkCoords()), entity_prog);