diff --git a/src/animation/rigging.cpp b/src/animation/rigging.cpp index 522ff7b12..47996b426 100644 --- a/src/animation/rigging.cpp +++ b/src/animation/rigging.cpp @@ -160,22 +160,26 @@ static glm::mat4 build_matrix( void SkeletonConfig::update( Skeleton& skeleton, const glm::mat3& rotation, - const glm::vec3& position, + const worldvec3& position, const glm::vec3& scale, const glm::ivec3& origin ) const { - glm::vec3 pos = position; + worldvec3 pos = position; const auto& interpolation = skeleton.interpolation; if (interpolation.isEnabled() && interpolation.isRefreshed()) { pos = interpolation.getCurrent(); } - pos = util::to_local(pos, origin); glm::mat3 rot = rotation; const auto& rotationInterpolation = skeleton.rotationInterpolation; if (rotationInterpolation.isEnabled() && rotationInterpolation.isRefreshed()) { rot = glm::mat3_cast(rotationInterpolation.getCurrent()); } - update(0, skeleton, root.get(), build_matrix(rot, pos, scale)); + update( + 0, + skeleton, + root.get(), + build_matrix(rot, util::to_local(pos, origin), scale) + ); } static float max_component(const glm::vec3& v) { @@ -254,7 +258,7 @@ void SkeletonConfig::render( Skeleton& skeleton, ModelLightingMode lightingMode, const glm::mat3& rotation, - const glm::vec3& position, + const worldvec3& position, const glm::vec3& scale, const glm::ivec3& origin, bool translucent @@ -282,7 +286,7 @@ void SkeletonConfig::updateBounds( const Assets& assets, Skeleton& skeleton, const glm::mat3& rotation, - const glm::vec3& position, + const worldvec3& position, const glm::vec3& scale, const glm::ivec3& origin ) const { diff --git a/src/animation/rigging.hpp b/src/animation/rigging.hpp index 939e7eaf4..65d77eec7 100644 --- a/src/animation/rigging.hpp +++ b/src/animation/rigging.hpp @@ -104,7 +104,7 @@ namespace rigging { glm::vec4 tint {1.0f, 1.0f, 1.0f, 1.0f}; std::vector boneTints; - util::VecInterpolation<3, float> interpolation {false}; + util::VecInterpolation<3, double> interpolation {false}; util::RotationInterpolation rotationInterpolation {false}; Skeleton(std::shared_ptr config); @@ -151,7 +151,7 @@ namespace rigging { void update( Skeleton& skeleton, const glm::mat3& rotation, - const glm::vec3& position, + const worldvec3& position, const glm::vec3& scale, const glm::ivec3& origin ) const; @@ -162,7 +162,7 @@ namespace rigging { Skeleton& skeleton, ModelLightingMode lightingMode, const glm::mat3& rotation, - const glm::vec3& position, + const worldvec3& position, const glm::vec3& scale, const glm::ivec3& origin, bool translucent = false @@ -172,7 +172,7 @@ namespace rigging { const Assets& assets, Skeleton& skeleton, const glm::mat3& rotation, - const glm::vec3& position, + const worldvec3& position, const glm::vec3& scale, const glm::ivec3& origin ) const; diff --git a/src/data/dv_util.hpp b/src/data/dv_util.hpp index ffec61dd2..cf48fed03 100644 --- a/src/data/dv_util.hpp +++ b/src/data/dv_util.hpp @@ -34,8 +34,8 @@ namespace dv { return values; } - template - void get_vec(const dv::value& list, glm::vec& vec) { + template + void get_vec(const dv::value& list, glm::vec& vec) { for (size_t i = 0; i < n; i++) { vec[i] = list[i].asNumber(); } diff --git a/src/frontend/debug_panel.cpp b/src/frontend/debug_panel.cpp index 2e10d55c4..5e79a7f6c 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -292,7 +292,7 @@ std::shared_ptr create_debug_panel( if (allowDebugCheats) { box->setTextConsumer([&player, ax](const std::wstring& text) { try { - glm::vec3 position = player.getPosition(); + worldvec3 position = player.getPosition(); position[ax] = std::stoi(text); player.teleport(position); } catch (std::exception& _) { diff --git a/src/graphics/render/EntitiesRenderer.cpp b/src/graphics/render/EntitiesRenderer.cpp index 70b51e8b0..a94f6ac16 100644 --- a/src/graphics/render/EntitiesRenderer.cpp +++ b/src/graphics/render/EntitiesRenderer.cpp @@ -147,7 +147,11 @@ void EntitiesRenderer::renderDebug( for (auto& sensor : rigidbody.sensors) { if (sensor.type != SensorType::AABB) continue; batch.box( - util::to_local(sensor.calculated.aabb.center(), origin), + util::to_local( + sensor.position + + worldvec3(sensor.calculated.aabb.center()), + origin + ), sensor.calculated.aabb.size(), glm::vec4(1.0f, 1.0f, 0.0f, 1.0f) ); diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index edc57959e..f064a9494 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -59,7 +59,7 @@ LevelController::LevelController( confirmed++; continue; } - glm::vec3 position = player->getPosition(); + const auto& position = player->getPosition(); player->chunks->configure( std::floor(position.x), std::floor(position.z), 1 ); @@ -82,7 +82,7 @@ void LevelController::update(float delta, bool pause) { continue; } player->rotationInterpolation.updateTimer(delta); - glm::vec3 position = player->getPosition(); + const auto& position = player->getPosition(); player->chunks->configure( glm::floor(position.x), glm::floor(position.z), diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index e75370ce5..81d6cc50a 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -54,7 +54,7 @@ CameraControl::CameraControl( } void CameraControl::refreshPosition() { - camera->position = player.getPosition() + offset; + camera->position = player.getPosition() + worldvec3(offset); } void CameraControl::refreshRotation() { @@ -461,8 +461,7 @@ void PlayerController::processRightClick( if (def.obstacle) { const auto& hitboxes = def.getHitboxes(state); for (const AABB& blockAABB : hitboxes) { - if (level.entities->hasBlockingInside(blockAABB.translated(coord) - )) { + if (level.entities->hasBlockingInside(coord, blockAABB)) { return; } } diff --git a/src/logic/scripting/lua/libs/lib__transform.cpp b/src/logic/scripting/lua/libs/lib__transform.cpp index dbe2d8c75..0f2c6345d 100644 --- a/src/logic/scripting/lua/libs/lib__transform.cpp +++ b/src/logic/scripting/lua/libs/lib__transform.cpp @@ -2,7 +2,7 @@ #include "maths/util.hpp" -static void check_valid(const glm::vec3& vec) { +static void check_valid(const worldvec3& vec) { if (util::is_nan_or_inf(vec)) { throw std::invalid_argument( "invalid vector: " + std::to_string(vec.x) + ", " + @@ -21,14 +21,14 @@ static void check_valid(const glm::mat3& vec) { static int l_get_pos(lua::State* L) { if (auto entity = get_entity(L, 1)) { - return lua::pushvec3(L, entity->getTransform().pos); + return lua::pushvec(L, entity->getTransform().pos); } return 0; } static int l_set_pos(lua::State* L) { if (auto entity = get_entity(L, 1)) { - auto vec = lua::tovec3(L, 2); + auto vec = lua::tovec<3, double>(L, 2); check_valid(vec); entity->getTransform().setPos(vec); entity->getRigidbody().hitbox.setPos(vec); diff --git a/src/logic/scripting/lua/libs/libentity.cpp b/src/logic/scripting/lua/libs/libentity.cpp index 96e26f2f8..cc889c023 100644 --- a/src/logic/scripting/lua/libs/libentity.cpp +++ b/src/logic/scripting/lua/libs/libentity.cpp @@ -72,7 +72,7 @@ static int l_spawn(lua::State* L) { auto level = controller->getLevel(); auto defname = lua::require_string(L, 1); auto& def = content->entities.require(defname); - auto pos = lua::tovec3(L, 2); + auto pos = lua::tovec<3, double>(L, 2); dv::value args = nullptr; if (lua::gettop(L) > 2) { args = lua::tovalue(L, 3); @@ -133,9 +133,9 @@ static int l_set_skeleton(lua::State* L) { } static int l_get_all_in_box(lua::State* L) { - auto pos = lua::tovec<3>(L, 1); - auto size = lua::tovec<3>(L, 2); - auto found = level->entities->getAllInside(AABB(pos, pos + size)); + auto pos = lua::tovec<3, double>(L, 1); + auto size = lua::tovec<3, double>(L, 2); + auto found = level->entities->getAllInside(pos, pos + size); lua::createtable(L, found.size(), 0); for (size_t i = 0; i < found.size(); i++) { lua::pushinteger(L, found[i]->getUID()); @@ -145,7 +145,7 @@ static int l_get_all_in_box(lua::State* L) { } static int l_get_all_in_radius(lua::State* L) { - auto pos = lua::tovec<3>(L, 1); + auto pos = lua::tovec<3, double>(L, 1); auto radius = lua::tonumber(L, 2); auto found = level->entities->getAllInRadius(pos, radius); lua::createtable(L, found.size(), 0); diff --git a/src/logic/scripting/lua/libs/libplayer.cpp b/src/logic/scripting/lua/libs/libplayer.cpp index 2ad96e7b2..e76d39007 100644 --- a/src/logic/scripting/lua/libs/libplayer.cpp +++ b/src/logic/scripting/lua/libs/libplayer.cpp @@ -47,7 +47,7 @@ static int l_set_pos(lua::State* L) { auto x = lua::tonumber(L, 2); auto y = lua::tonumber(L, 3); auto z = lua::tonumber(L, 4); - player->teleport(glm::vec3(x, y, z)); + player->teleport(worldvec3(x, y, z)); return 0; } @@ -256,7 +256,7 @@ static int l_set_spawnpoint(lua::State* L) { auto x = lua::tonumber(L, 2); auto y = lua::tonumber(L, 3); auto z = lua::tonumber(L, 4); - player->setSpawnPoint(glm::vec3(x, y, z)); + player->setSpawnPoint(worldvec3(x, y, z)); } return 0; } @@ -359,7 +359,7 @@ static int l_set_suspended(lua::State* L) { static int l_get_all_in_radius(lua::State* L) { auto& level = require_level(); - auto center = lua::tovec3(L, 1); + auto center = lua::tovec<3, double>(L, 1); auto radius = static_cast(lua::tonumber(L, 2)); auto players = level.players->getAllInRadius(center, radius); @@ -383,7 +383,7 @@ static int l_get_all(lua::State* L) { static int l_get_nearest(lua::State* L) { auto& level = require_level(); - auto position = lua::tovec3(L, 1); + auto position = lua::tovec<3, double>(L, 1); if (auto player = level.players->getNearest(position)) { lua::pushinteger(L, player->getId()); return 1; diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 690fcb32d..8a716faaa 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -100,8 +100,8 @@ namespace lua { return 1; } - template - inline int pushvec_stack(lua::State* L, const glm::vec& vec) { + template + inline int pushvec_stack(lua::State* L, const glm::vec& vec) { for (int i = 0; i < n; i++) { pushnumber(L, vec[i]); } diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 68b08cbb3..6a0392d18 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -43,7 +43,7 @@ Entity* Entities::get(entityid_t id) { entityid_t Entities::spawn( const EntityDef& def, - glm::vec3 position, + worldvec3 position, dv::value args, dv::value saved, entityid_t uid @@ -272,17 +272,22 @@ void Entities::updateSensors( sensor.nextEntered.clear(); switch (sensor.type) { - case SensorType::AABB: + case SensorType::AABB: { + glm::mat4 matrix = tsf.combined; + matrix[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); sensor.calculated.aabb = sensor.params.aabb; - sensor.calculated.aabb.transform(tsf.combined); + sensor.calculated.aabb.transform(matrix); + sensor.position = tsf.displayPos; break; + } case SensorType::RADIUS: sensor.calculated.radial = glm::vec4( - body.hitbox.position.x, - body.hitbox.position.y, - body.hitbox.position.z, + 0.0f, + 0.0f, + 0.0f, sensor.params.radial.w * sensor.params.radial.w ); + sensor.position = body.hitbox.position; break; } sensors.push_back(&sensor); @@ -409,10 +414,10 @@ void Entities::update(float delta) { } } -bool Entities::hasBlockingInside(AABB aabb) { +bool Entities::hasBlockingInside(const worldvec3& origin, AABB aabb) { constexpr float eps = 0.05f; for (const auto& [uid, entity] : entities) { - AABB bodyAABB(entity->getRigidbody().hitbox.getAABB()); + AABB bodyAABB(entity->getRigidbody().hitbox.getAABB(origin)); bodyAABB.scale(glm::vec3(1.0f - eps)); if (entity->getID().def.blocking && aabb.intersects(bodyAABB)) { return true; @@ -421,17 +426,24 @@ bool Entities::hasBlockingInside(AABB aabb) { return false; } -std::vector Entities::getAllInside(AABB aabb) { +std::vector Entities::getAllInside( + const worldvec3& a, const worldvec3& b +) { + const worldvec3 min = glm::min(a, b); + const worldvec3 max = glm::max(a, b); std::vector collected; for (const auto& [uid, entity] : entities) { - if (!entity->getID().destroyFlag && aabb.contains(entity->getTransform().pos)) { + const auto& pos = entity->getTransform().pos; + if (!entity->getID().destroyFlag && + glm::all(glm::greaterThanEqual(pos, min)) && + glm::all(glm::lessThan(pos, max))) { collected.push_back(entity.get()); } } return collected; } -std::vector Entities::getAllInRadius(glm::vec3 center, float radius) { +std::vector Entities::getAllInRadius(worldvec3 center, float radius) { std::vector collected; for (const auto& [uid, entity] : entities) { if (glm::distance2(entity->getTransform().pos, center) <= radius * radius) { diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index f6810c13d..24767ce9e 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -52,7 +52,7 @@ public: entityid_t spawn( const EntityDef& def, - glm::vec3 position, + worldvec3 position, dv::value args = nullptr, dv::value saved = nullptr, entityid_t uid = 0 @@ -94,9 +94,9 @@ public: void loadEntity(const dv::value& map); void loadEntity(const dv::value& map, Entity& entity); void onSave(const Entity& entity); - bool hasBlockingInside(AABB aabb); - std::vector getAllInside(AABB aabb); - std::vector getAllInRadius(glm::vec3 center, float radius); + bool hasBlockingInside(const worldvec3& origin, AABB aabb); + std::vector getAllInside(const worldvec3& a, const worldvec3& b); + std::vector getAllInRadius(worldvec3 center, float radius); void despawn(entityid_t id); void despawn(const std::vector& entities); dv::value serialize(const std::vector& entities); diff --git a/src/objects/Entity.cpp b/src/objects/Entity.cpp index a5aa4eeec..b4b6e5f66 100644 --- a/src/objects/Entity.cpp +++ b/src/objects/Entity.cpp @@ -26,13 +26,13 @@ Entity::Entity( Entity::~Entity() = default; -void Entity::setInterpolatedPosition(const glm::vec3& position) { +void Entity::setInterpolatedPosition(const worldvec3& position) { if (skeleton) { skeleton->interpolation.refresh(position); } } -glm::vec3 Entity::getInterpolatedPosition() const { +worldvec3 Entity::getInterpolatedPosition() const { if (skeleton) { const auto& interpolation = skeleton->interpolation; if (interpolation.isEnabled() && interpolation.isRefreshed()) { diff --git a/src/objects/Entity.hpp b/src/objects/Entity.hpp index f7c7c3b5d..102756be4 100644 --- a/src/objects/Entity.hpp +++ b/src/objects/Entity.hpp @@ -104,9 +104,9 @@ public: void setPlayer(int64_t player); - void setInterpolatedPosition(const glm::vec3& position); + void setInterpolatedPosition(const worldvec3& position); - glm::vec3 getInterpolatedPosition() const; + worldvec3 getInterpolatedPosition() const; void setInterpolatedRotation(const glm::quat& rotation); diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 760b14ef7..ee8ff4011 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -31,7 +31,7 @@ Player::Player( Level& level, int64_t id, const std::wstring& name, - glm::vec3 position, + worldvec3 position, float speed, std::shared_ptr inv, entityid_t eid @@ -121,7 +121,7 @@ void Player::postUpdate() { } } -void Player::teleport(glm::vec3 position) { +void Player::teleport(worldvec3 position) { this->position = position; if (auto entity = level.entities->get(eid)) { @@ -138,7 +138,7 @@ void Player::attemptToChooseSpawnpoint() { int minHeight = generatorDef.playerMinSpawnHeight; int maxHeight = generatorDef.playerMaxSpawnHeight; - glm::vec3 newpos {0.0f, random.randFloat() * (maxHeight - minHeight + 1) + minHeight, 0.0f}; + worldvec3 newpos {0.0f, random.randFloat() * (maxHeight - minHeight + 1) + minHeight, 0.0f}; double angle = random.randDouble() * glm::two_pi(); double radius = glm::sqrt(random.randDouble()); newpos.x += glm::cos(angle) * generatorDef.playerSpawnRadius * radius; @@ -154,7 +154,7 @@ void Player::attemptToChooseSpawnpoint() { headvox == nullptr || headvox->id != 0) { return; } - spawnpoint = newpos + glm::vec3(0.5f, 0.0f, 0.5f); + spawnpoint = newpos + worldvec3(0.5, 0.0, 0.5); teleport(spawnpoint); } @@ -259,11 +259,11 @@ const std::shared_ptr& Player::getInventory() const { return inventory; } -void Player::setSpawnPoint(glm::vec3 spawnpoint) { +void Player::setSpawnPoint(worldvec3 spawnpoint) { this->spawnpoint = spawnpoint; } -glm::vec3 Player::getSpawnPoint() const { +worldvec3 Player::getSpawnPoint() const { return spawnpoint; } @@ -324,7 +324,7 @@ void Player::deserialize(const dv::value& src) { dv::get_vec(rotarr, rotation); const auto& sparr = src["spawnpoint"]; - setSpawnPoint(glm::vec3( + setSpawnPoint(worldvec3( sparr[0].asNumber(), sparr[1].asNumber(), sparr[2].asNumber())); src.at("interaction-distance").get(interactionDistance); diff --git a/src/objects/Player.hpp b/src/objects/Player.hpp index 13eedb180..537e2816e 100644 --- a/src/objects/Player.hpp +++ b/src/objects/Player.hpp @@ -50,8 +50,8 @@ class Player : public Serializable { int chosenSlot; int hotbarSize; - glm::vec3 position; - glm::vec3 spawnpoint {std::nanf(""), std::nanf(""), std::nanf("")}; + worldvec3 position; + worldvec3 spawnpoint {std::nanf(""), std::nanf(""), std::nanf("")}; std::shared_ptr inventory; bool suspended = false; bool flight = false; @@ -81,14 +81,14 @@ public: Level& level, int64_t id, const std::wstring& name, - glm::vec3 position, + worldvec3 position, float speed, std::shared_ptr inv, entityid_t eid ); ~Player(); - void teleport(glm::vec3 position); + void teleport(worldvec3 position); void updateEntity(); void updateSelectedEntity(); void postUpdate(); @@ -134,7 +134,7 @@ public: const std::shared_ptr& getInventory() const; - const glm::vec3& getPosition() const { + const worldvec3& getPosition() const { return position; } @@ -142,8 +142,8 @@ public: Hitbox* getHitbox() const; - void setSpawnPoint(glm::vec3 point); - glm::vec3 getSpawnPoint() const; + void setSpawnPoint(worldvec3 point); + worldvec3 getSpawnPoint() const; glm::vec3 getRotation(bool interpolated=false) const; void setRotation(const glm::vec3& rotation); diff --git a/src/objects/Players.cpp b/src/objects/Players.cpp index 04c1aa2aa..47bdf1fd3 100644 --- a/src/objects/Players.cpp +++ b/src/objects/Players.cpp @@ -25,7 +25,7 @@ Player* Players::get(int64_t id) const { } std::vector Players::getAllInRadius( - const glm::vec3& center, float radius + const worldvec3& center, float radius ) const { std::vector foundPlayers; @@ -48,9 +48,9 @@ std::vector Players::getAll() const { return allPlayers; } -Player* Players::getNearest(const glm::vec3& position) const { +Player* Players::getNearest(const worldvec3& position) const { Player* nearest = nullptr; - float nearestDist2 = std::numeric_limits::max(); + double nearestDist2 = std::numeric_limits::max(); for (const auto& pair : players) { auto player = pair.second.get(); if (player->isSuspended()) { diff --git a/src/objects/Players.hpp b/src/objects/Players.hpp index 168033409..74c3d1240 100644 --- a/src/objects/Players.hpp +++ b/src/objects/Players.hpp @@ -36,9 +36,11 @@ public: void remove(int64_t id); - std::vector getAllInRadius(const glm::vec3& center, float radius) const; + std::vector getAllInRadius( + const worldvec3& center, float radius + ) const; std::vector getAll() const; - Player* getNearest(const glm::vec3& position) const; + Player* getNearest(const worldvec3& position) const; dv::value serialize() const override; diff --git a/src/objects/Transform.cpp b/src/objects/Transform.cpp index c398c48e7..5d5522825 100644 --- a/src/objects/Transform.cpp +++ b/src/objects/Transform.cpp @@ -11,7 +11,7 @@ static debug::Logger logger("entity-transform"); void Transform::refresh() { combined = glm::mat4(1.0f); - combined = glm::translate(combined, pos); + combined = glm::translate(combined, glm::vec3(pos)); combined = combined * glm::mat4(rot); combined = glm::scale(combined, size); displayPos = pos; @@ -37,7 +37,7 @@ void Transform::deserialize(const dv::value& root) { dv::get_mat(root, "rot", rot); } -bool Transform::checkValue(const glm::vec3& vector, std::string_view name) { +bool Transform::checkValue(const worldvec3& vector, std::string_view name) { if (util::is_nan_or_inf(vector)) { auto message = "invalid vector attempted set to " + std::string(name) + " : " + std::to_string(vector.x) + ", " + diff --git a/src/objects/Transform.hpp b/src/objects/Transform.hpp index 5cea5c83c..e27461f29 100644 --- a/src/objects/Transform.hpp +++ b/src/objects/Transform.hpp @@ -2,6 +2,8 @@ #define GLM_ENABLE_EXPERIMENTAL +#include "typedefs.hpp" + #include #include #include @@ -10,13 +12,13 @@ struct Transform { static inline constexpr float EPSILON = 1e-7f; - glm::vec3 pos; + worldvec3 pos; glm::vec3 size; glm::mat3 rot; glm::mat4 combined; bool dirty = true; - glm::vec3 displayPos {}; + worldvec3 displayPos {}; glm::vec3 displaySize {}; dv::value serialize() const; @@ -33,7 +35,7 @@ struct Transform { } inline void setSize(const glm::vec3& v) { - if (!checkValue(v, "size")) { + if (!checkValue(worldvec3(v), "size")) { return; } if (glm::distance2(displaySize, v) >= EPSILON) { @@ -42,7 +44,7 @@ struct Transform { size = v; } - inline void setPos(const glm::vec3& v) { + inline void setPos(const worldvec3& v) { if (!checkValue(v, "position")) { return; } @@ -52,6 +54,6 @@ struct Transform { pos = v; } - static bool checkValue(const glm::vec3& v, std::string_view name); + static bool checkValue(const worldvec3& v, std::string_view name); static bool checkValue(const glm::mat3& v, std::string_view name); }; diff --git a/src/physics/BroadPhase.hpp b/src/physics/BroadPhase.hpp index 7b9eabbc6..ec9c0a665 100644 --- a/src/physics/BroadPhase.hpp +++ b/src/physics/BroadPhase.hpp @@ -29,10 +29,10 @@ private: glm::vec3 gravity; SpatialGrid grid; std::vector> nearbySolids; - std::vector nearbyOrigins; + std::vector nearbyOrigins; std::vector nearbyBudgets; std::vector nearbyReaches; - std::vector solidOrigins; + std::vector solidOrigins; std::vector solidBudgets; std::vector candidates; std::vector solidStamps; diff --git a/src/physics/Hitbox.cpp b/src/physics/Hitbox.cpp index f49e0c2b8..cc206a399 100644 --- a/src/physics/Hitbox.cpp +++ b/src/physics/Hitbox.cpp @@ -1,7 +1,7 @@ #include "Hitbox.hpp" Hitbox::Hitbox( - entityid_t entity, BodyType type, glm::vec3 position, glm::vec3 halfsize + entityid_t entity, BodyType type, worldvec3 position, glm::vec3 halfsize ) : entity(entity), type(type), diff --git a/src/physics/Hitbox.hpp b/src/physics/Hitbox.hpp index 01e657236..e1a9a4904 100644 --- a/src/physics/Hitbox.hpp +++ b/src/physics/Hitbox.hpp @@ -19,7 +19,7 @@ enum class SensorType { union SensorParams { AABB aabb; - glm::vec4 radial; // x,y,z calculated entity coords + w - radius + glm::vec4 radial; // w - radius constexpr SensorParams() : aabb() { } @@ -38,6 +38,7 @@ struct Sensor { std::set nextEntered; sensorcallback enterCallback; sensorcallback exitCallback; + worldvec3 position {}; }; enum class BodyType { @@ -53,7 +54,7 @@ VC_ENUM_END struct Hitbox { entityid_t entity; BodyType type; - glm::vec3 position; + worldvec3 position; glm::vec3 halfsize; glm::vec3 velocity; glm::vec3 scale {1.0f, 1.0f, 1.0f}; @@ -70,7 +71,7 @@ struct Hitbox { std::string groundMaterial; glm::vec3 groundVelocity {}; - glm::vec3 prevPosition {}; + worldvec3 prevPosition {}; glm::vec3 prevVelocity {}; bool prevGrounded = false; float substepTime = 0.0f; @@ -80,18 +81,19 @@ struct Hitbox { static inline constexpr float TELEPORT_THRESOLD_SQR = 0.5f; Hitbox( - entityid_t entity, BodyType type, glm::vec3 position, glm::vec3 halfsize + entityid_t entity, BodyType type, worldvec3 position, glm::vec3 halfsize ); - AABB getAABB() const { - return AABB(position - halfsize, position + halfsize); + AABB getAABB(const worldvec3& origin) const { + glm::vec3 pos(position - origin); + return AABB(pos - halfsize, pos + halfsize); } glm::vec3 getHalfSize() const { return halfsize * scale; } - void setPos(const glm::vec3& vec) { + void setPos(const worldvec3& vec) { position = vec; if (glm::distance2(position, prevPosition) >= TELEPORT_THRESOLD_SQR) { prevPosition = vec; diff --git a/src/physics/SpatialGrid.cpp b/src/physics/SpatialGrid.cpp index ad686500f..23cf49035 100644 --- a/src/physics/SpatialGrid.cpp +++ b/src/physics/SpatialGrid.cpp @@ -2,9 +2,9 @@ #include -inline constexpr float GRID_CELL_SIZE = 4.0f; -inline constexpr float GRID_PADDING = 0.01f; -inline constexpr float GRID_MAX_COORD = 1e9f; +inline constexpr double GRID_CELL_SIZE = 4.0; +inline constexpr double GRID_PADDING = 0.01; +inline constexpr double GRID_MAX_COORD = 1e9; inline constexpr int GRID_MAX_CELLS = 64; void SpatialGrid::clear() { @@ -13,7 +13,7 @@ void SpatialGrid::clear() { } void SpatialGrid::insert( - uint id, const glm::vec3& center, const glm::vec3& extents + uint id, const worldvec3& center, const glm::vec3& extents ) { glm::ivec2 min, max; if (!calcRange(center, extents, min, max)) { @@ -37,20 +37,20 @@ uint64_t SpatialGrid::calcKey(int x, int z) { } bool SpatialGrid::calcRange( - const glm::vec3& center, + const worldvec3& center, const glm::vec3& extents, glm::ivec2& min, glm::ivec2& max ) { - glm::vec2 c(center.x, center.z); - glm::vec2 e = glm::vec2(extents.x, extents.z) + GRID_PADDING; - glm::vec2 a = glm::floor((c - e) / GRID_CELL_SIZE); - glm::vec2 b = glm::floor((c + e) / GRID_CELL_SIZE); + glm::dvec2 c(center.x, center.z); + glm::dvec2 e = glm::dvec2(extents.x, extents.z) + GRID_PADDING; + glm::dvec2 a = glm::floor((c - e) / GRID_CELL_SIZE); + glm::dvec2 b = glm::floor((c + e) / GRID_CELL_SIZE); if (!(glm::abs(a.x) < GRID_MAX_COORD && glm::abs(a.y) < GRID_MAX_COORD && glm::abs(b.x) < GRID_MAX_COORD && glm::abs(b.y) < GRID_MAX_COORD)) { return false; } - if ((b.x - a.x + 1.0f) * (b.y - a.y + 1.0f) > GRID_MAX_CELLS) { + if ((b.x - a.x + 1.0) * (b.y - a.y + 1.0) > GRID_MAX_CELLS) { return false; } min = glm::ivec2(a); diff --git a/src/physics/SpatialGrid.hpp b/src/physics/SpatialGrid.hpp index d35e94786..d48cc5c93 100644 --- a/src/physics/SpatialGrid.hpp +++ b/src/physics/SpatialGrid.hpp @@ -10,13 +10,13 @@ class SpatialGrid { public: void clear(); - void insert(uint id, const glm::vec3& center, const glm::vec3& extents); + void insert(uint id, const worldvec3& center, const glm::vec3& extents); void build(); template bool query( - const glm::vec3& center, const glm::vec3& extents, const Func& func + const worldvec3& center, const glm::vec3& extents, const Func& func ) const { glm::ivec2 min, max; if (!calcRange(center, extents, min, max)) { @@ -46,7 +46,7 @@ private: static uint64_t calcKey(int x, int z); static bool calcRange( - const glm::vec3& center, + const worldvec3& center, const glm::vec3& extents, glm::ivec2& min, glm::ivec2& max diff --git a/src/physics/collisions.cpp b/src/physics/collisions.cpp index bac65c9e5..84936e3d3 100644 --- a/src/physics/collisions.cpp +++ b/src/physics/collisions.cpp @@ -2,6 +2,7 @@ #include "scheduling.hpp" #include "maths/aabb.hpp" +#include "maths/util.hpp" #include "voxels/GlobalChunks.hpp" #define GLM_ENABLE_EXPERIMENTAL @@ -28,6 +29,7 @@ static glm::vec3 calc_collsion_velocity_result( static float calc_step_height( const GlobalChunks& chunks, const glm::vec3& pos, + const glm::ivec3& origin, const glm::vec3& half, float stepHeight ) { @@ -41,7 +43,9 @@ static float calc_step_height( float x = (pos.x - half.x) + ix; for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++) { float z = (pos.z - half.z) + iz; - if (chunks.isObstacleAt(x, pos.y + half.y + stepHeight, z, aabb)) { + if (chunks.isObstacleAt( + origin, x, pos.y + half.y + stepHeight, z, aabb + )) { return 0.0f; } } @@ -54,18 +58,20 @@ static float calc_step_height( template static void calc_collision( Hitbox& hitbox, + glm::vec3& pos, + const glm::ivec3& origin, const GlobalChunks& chunks, const std::vector& solidHitboxes, const glm::vec3& half, float stepHeight, uint wakeSubstep ) { - auto& pos = hitbox.position; auto& vel = hitbox.velocity; glm::vec3 offset(0.0f, stepHeight + E, 0.0f); for (auto box : solidHitboxes) { - if (glm::distance2(box->position, pos) < E) { + glm::vec3 boxpos = util::to_local(box->position, origin); + if (glm::distance2(boxpos, pos) < E) { continue; } auto boxhalf = box->getHalfSize(); @@ -77,12 +83,13 @@ static void calc_collision( aabb = aabb + offset; aabb.b.y -= stepHeight + E * 2; - if ((box->position[nx] - pos[nx]) * sign <= 0.0f || !box->getAABB().intersects(aabb)) { + if ((boxpos[nx] - pos[nx]) * sign <= 0.0f || + !box->getAABB(origin).intersects(aabb)) { continue; } - float newnegx = box->position[nx] - boxhalf[nx] - half[nx]; - float newposx = box->position[nx] + boxhalf[nx] + half[nx]; + float newnegx = boxpos[nx] - boxhalf[nx] - half[nx]; + float newposx = boxpos[nx] + boxhalf[nx] + half[nx]; float newx; if ((glm::abs(newnegx - pos[nx]) - glm::abs(newposx - pos[nx]))*sign < 0.0f) { newx = sign > 0 ? newnegx : newposx; @@ -116,7 +123,9 @@ static void calc_collision( aabb = aabb + offset; aabb.b.y -= stepHeight + E * 2; - if (const auto obstacle = chunks.isObstacleAt(coord.x, coord.y, coord.z, aabb)) { + if (const auto obstacle = chunks.isObstacleAt( + origin, coord.x, coord.y, coord.z, aabb + )) { float newx = std::floor(coord[nx]) - half[nx] * sign + (sign > 0 ? obstacle->min() : obstacle->max())[nx]; if ((pos[nx] - newx) * sign > 0.0f && glm::abs(pos[nx] - newx) < MAX_FIX) { @@ -130,15 +139,19 @@ static void calc_collision( } static bool calc_collision_neg_y( - Hitbox& hitbox, const glm::vec3& half, const CollisionContext& ctx + Hitbox& hitbox, + glm::vec3& pos, + const glm::ivec3& origin, + const glm::vec3& half, + const CollisionContext& ctx ) { const auto& chunks = ctx.chunks; const auto& solids = ctx.solids; - auto& pos = hitbox.position; auto& vel = hitbox.velocity; for (auto box : solids) { - if (glm::distance2(box->position, pos) < E) { + glm::vec3 boxpos = util::to_local(box->position, origin); + if (glm::distance2(boxpos, pos) < E) { continue; } auto aabb = AABB(pos - half, pos + half); @@ -148,8 +161,8 @@ static bool calc_collision_neg_y( aabb.scale(scale); auto boxhalf = box->getHalfSize(); - if (box->position.y < pos.y && box->getAABB().intersects(aabb)) { - float newy = box->position.y + boxhalf.y + half.y; + if (boxpos.y < pos.y && box->getAABB(origin).intersects(aabb)) { + float newy = boxpos.y + boxhalf.y + half.y; if (pos.y < newy && glm::abs(pos.y - newy) < boxhalf.y) { pos.y = newy; } @@ -158,7 +171,8 @@ static bool calc_collision_neg_y( auto velB = calc_collsion_velocity_result(*box, hitbox); float boxdt = box->substepTime > 0.0f ? box->substepTime : ctx.dt; - hitbox.groundVelocity = (box->position - box->prevPosition) / boxdt; + hitbox.groundVelocity = + glm::vec3(box->position - box->prevPosition) / boxdt; if (vel.y < hitbox.groundVelocity.y) { vel.y = velA.y; push_body(*box, 1, velB.y, ctx.wakeSubstep); @@ -185,8 +199,9 @@ static bool calc_collision_neg_y( scale.z = 1.0f - E * 4.0f; aabb.scale(scale); - if (const auto obstacle = - chunks.isObstacleAt(coord.x, coord.y, coord.z, aabb)) { + if (const auto obstacle = chunks.isObstacleAt( + origin, coord.x, coord.y, coord.z, aabb + )) { float newy = std::floor(coord.y) + half.y + obstacle->max().y; if (newy >= pos.y) { @@ -203,6 +218,8 @@ static bool calc_collision_neg_y( static void calc_collisions( Hitbox& hitbox, + glm::vec3& pos, + const glm::ivec3& origin, const glm::vec3& half, float stepHeight, const CollisionContext& ctx @@ -210,32 +227,31 @@ static void calc_collisions( const auto& chunks = ctx.chunks; const auto& solids = ctx.solids; uint wakeSubstep = ctx.wakeSubstep; - auto& pos = hitbox.position; auto& vel = hitbox.velocity; - stepHeight = calc_step_height(chunks, pos, half, stepHeight); + stepHeight = calc_step_height(chunks, pos, origin, half, stepHeight); auto prevPos = pos; calc_collision<0, 1, 2, -1>( - hitbox, chunks, solids, half, stepHeight, wakeSubstep + hitbox, pos, origin, chunks, solids, half, stepHeight, wakeSubstep ); calc_collision<0, 1, 2, 1>( - hitbox, chunks, solids, half, stepHeight, wakeSubstep + hitbox, pos, origin, chunks, solids, half, stepHeight, wakeSubstep ); float xpos = pos.x; pos.x = prevPos.x; calc_collision<2, 1, 0, -1>( - hitbox, chunks, solids, half, stepHeight, wakeSubstep + hitbox, pos, origin, chunks, solids, half, stepHeight, wakeSubstep ); calc_collision<2, 1, 0, 1>( - hitbox, chunks, solids, half, stepHeight, wakeSubstep + hitbox, pos, origin, chunks, solids, half, stepHeight, wakeSubstep ); pos.x = xpos; - if (calc_collision_neg_y(hitbox, half, ctx)) { + if (calc_collision_neg_y(hitbox, pos, origin, half, ctx)) { hitbox.grounded = true; } @@ -249,7 +265,7 @@ static void calc_collisions( for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++) { float z = (pos.z - half.z + E) + iz; float y = (pos.y + half.y + E) - E; - if (auto aabb = chunks.isObstacleAt(x, y, z, boxAABB)) { + if (auto aabb = chunks.isObstacleAt(origin, x, y, z, boxAABB)) { float bottom = std::floor(y) + aabb->min().y; float newy = bottom - half.y - E; if (pos.y >= newy && pos.y - half.y < bottom) { @@ -261,12 +277,13 @@ static void calc_collisions( } } for (auto box : solids) { - if (glm::distance2(box->position, pos) < E) { + glm::vec3 boxpos = util::to_local(box->position, origin); + if (glm::distance2(boxpos, pos) < E) { continue; } auto boxhalf = box->getHalfSize(); - if (box->position.y > pos.y && box->getAABB().intersects(boxAABB)) { - float newy = box->position.y - boxhalf.y - half.y; + if (boxpos.y > pos.y && box->getAABB(origin).intersects(boxAABB)) { + float newy = boxpos.y - boxhalf.y - half.y; if (pos.y > newy && glm::abs(pos.y - newy) < 0.5f) { pos.y = newy; } @@ -290,7 +307,7 @@ static void calc_collisions( for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++) { float z = (pos.z - half.z) + iz; float y = (pos.y - half.y + E) + E; - if (auto aabb = chunks.isObstacleAt(x, y, z, boxAABB)) { + if (auto aabb = chunks.isObstacleAt(origin, x, y, z, boxAABB)) { if (vel.y < 0.0f) { vel.y = 0.0f; } @@ -303,13 +320,14 @@ static void calc_collisions( } } for (auto box : solids) { - if (glm::distance2(box->position, pos) < E) { + glm::vec3 boxpos = util::to_local(box->position, origin); + if (glm::distance2(boxpos, pos) < E) { continue; } auto boxhalf = box->getHalfSize(); - if (box->getAABB().intersects(boxAABB)) { + if (box->getAABB(origin).intersects(boxAABB)) { vel.y = 0.0f; - float newy = box->position.y + boxhalf.y + half.y; + float newy = boxpos.y + boxhalf.y + half.y; if (std::abs(newy - pos.y) <= stepHeight + E * 4) { pos.y = newy; } @@ -318,12 +336,16 @@ static void calc_collisions( } } -void physics::calc_substep(Hitbox& hitbox, const CollisionContext& ctx) { +static void calc_local_substep( + Hitbox& hitbox, + glm::vec3& pos, + const glm::ivec3& origin, + const CollisionContext& ctx +) { const auto& chunks = ctx.chunks; const auto& solids = ctx.solids; const auto& gravity = ctx.gravity; float dt = ctx.dt; - auto& pos = hitbox.position; auto& vel = hitbox.velocity; auto initpos = pos; @@ -333,6 +355,8 @@ void physics::calc_substep(Hitbox& hitbox, const CollisionContext& ctx) { if (hitbox.type == BodyType::DYNAMIC) { calc_collisions( hitbox, + pos, + origin, half, (hitbox.prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight : 0.0f, @@ -361,17 +385,18 @@ void physics::calc_substep(Hitbox& hitbox, const CollisionContext& ctx) { float x = (pos.x - half.x + E) + ix; for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++){ float z = (pos.z - half.z + E) + iz; - if (chunks.isObstacleAt(x, y, z, boxAABB)){ + if (chunks.isObstacleAt(origin, x, y, z, boxAABB)) { hitbox.grounded = true; break; } } } for (auto box : solids) { - if (glm::distance2(box->position, pos) < E) { + glm::vec3 boxpos = util::to_local(box->position, origin); + if (glm::distance2(boxpos, pos) < E) { continue; } - if (box->position.y < pos.y && box->getAABB().intersects(boxAABB)) { + if (boxpos.y < pos.y && box->getAABB(origin).intersects(boxAABB)) { hitbox.grounded = true; break; } @@ -383,3 +408,10 @@ void physics::calc_substep(Hitbox& hitbox, const CollisionContext& ctx) { } hitbox.grounded = true; } + +void physics::calc_substep(Hitbox& hitbox, const CollisionContext& ctx) { + const glm::ivec3 origin = glm::floor(hitbox.position); + glm::vec3 pos = util::to_local(hitbox.position, origin); + calc_local_substep(hitbox, pos, origin, ctx); + hitbox.position = worldvec3(origin) + worldvec3(pos); +} diff --git a/src/physics/scheduling.hpp b/src/physics/scheduling.hpp index 1caf69ac5..e320feadd 100644 --- a/src/physics/scheduling.hpp +++ b/src/physics/scheduling.hpp @@ -13,8 +13,9 @@ namespace physics { ) { float n = static_cast(substeps); auto accel = gravity * box.gravityScale; - box.position += box.velocity * (dt * n) + - accel * (dt * dt * n * (n + 2.0f) * 0.5f); + box.position += worldvec3( + box.velocity * (dt * n) + accel * (dt * dt * n * (n + 2.0f) * 0.5f) + ); box.velocity += accel * (dt * n); } diff --git a/src/physics/sensors.cpp b/src/physics/sensors.cpp index 641d1c09b..c71880a84 100644 --- a/src/physics/sensors.cpp +++ b/src/physics/sensors.cpp @@ -3,8 +3,6 @@ void physics::update_sensors( Hitbox& hitbox, const std::vector& sensors ) { - auto aabb = hitbox.getAABB(); - for (size_t i = 0; i < sensors.size(); i++) { auto& sensor = *sensors[i]; if (sensor.entity == hitbox.entity) { @@ -13,12 +11,12 @@ void physics::update_sensors( bool triggered = false; switch (sensor.type) { case SensorType::AABB: - triggered = aabb.intersects(sensor.calculated.aabb); + triggered = hitbox.getAABB(sensor.position) + .intersects(sensor.calculated.aabb); break; case SensorType::RADIUS: - triggered = glm::distance2( - hitbox.position, glm::vec3(sensor.calculated.radial)) - < sensor.calculated.radial.w; + triggered = glm::distance2(hitbox.position, sensor.position) < + sensor.calculated.radial.w; break; } if (!triggered) { diff --git a/src/voxels/GlobalChunks.cpp b/src/voxels/GlobalChunks.cpp index c081a6a5b..d4bdd0100 100644 --- a/src/voxels/GlobalChunks.cpp +++ b/src/voxels/GlobalChunks.cpp @@ -191,7 +191,7 @@ void GlobalChunks::save(Chunk* chunk) { return; } AABB aabb = chunk->getAABB(); - auto entities = level.entities->getAllInside(aabb); + auto entities = level.entities->getAllInside(aabb.a, aabb.b); auto root = dv::object(); root["data"] = level.entities->serialize(entities); if (!entities.empty()) { @@ -214,6 +214,8 @@ void GlobalChunks::putChunk(std::shared_ptr chunk) { chunksMap[keyfrom(chunk->x, chunk->z)] = std::move(chunk); } -std::optional GlobalChunks::isObstacleAt(float x, float y, float z, const AABB& aabb) const { - return blocks_agent::is_obstacle_at(*this, x, y, z, aabb); +std::optional GlobalChunks::isObstacleAt( + const glm::ivec3& origin, float x, float y, float z, const AABB& aabb +) const { + return blocks_agent::is_obstacle_at(*this, origin, x, y, z, aabb); } diff --git a/src/voxels/GlobalChunks.hpp b/src/voxels/GlobalChunks.hpp index de62cbc58..81c7a3ab2 100644 --- a/src/voxels/GlobalChunks.hpp +++ b/src/voxels/GlobalChunks.hpp @@ -58,7 +58,9 @@ public: void putChunk(std::shared_ptr chunk); - std::optional isObstacleAt(float x, float y, float z, const AABB& aabb) const; + std::optional isObstacleAt( + const glm::ivec3& origin, float x, float y, float z, const AABB& aabb + ) const; inline Chunk* getChunk(int cx, int cz) const { const auto& found = chunksMap.find(keyfrom(cx, cz)); diff --git a/src/voxels/blocks_agent.hpp b/src/voxels/blocks_agent.hpp index a4e27a0b5..fe90d6c83 100644 --- a/src/voxels/blocks_agent.hpp +++ b/src/voxels/blocks_agent.hpp @@ -488,14 +488,20 @@ void get_voxels( template inline std::optional is_obstacle_at( - const Storage& chunks, float x, float y, float z, const AABB& aabb + const Storage& chunks, + const glm::ivec3& origin, + float x, + float y, + float z, + const AABB& aabb ) { int ix = std::floor(x); int iy = std::floor(y); int iz = std::floor(z); - voxel* v = get(chunks, ix, iy, iz); + glm::ivec3 point = origin + glm::ivec3(ix, iy, iz); + voxel* v = get(chunks, point.x, point.y, point.z); if (v == nullptr) { - if (iy >= CHUNK_H) { + if (point.y >= CHUNK_H) { return std::nullopt; } else { return AABB(); @@ -508,10 +514,9 @@ inline std::optional is_obstacle_at( glm::ivec3 offset {}; blockstate state = v->state; if (state.segment) { - glm::ivec3 point(ix, iy, iz); - auto origin = seek_origin(chunks, point, def, state); - offset = origin - point; - state = hitbox_state(chunks, origin, state); + auto blockOrigin = seek_origin(chunks, point, def, state); + offset = blockOrigin - point; + state = hitbox_state(chunks, blockOrigin, state); } const auto& boxes = def.getHitboxes(state); @@ -525,7 +530,9 @@ inline std::optional is_obstacle_at( template inline std::optional is_obstacle_at(const Storage& chunks, float x, float y, float z) { - return is_obstacle_at(chunks, x, y, z, AABB({x, y, z}, {x + 1, y + 1, z + 1})); + return is_obstacle_at( + chunks, {}, x, y, z, AABB({x, y, z}, {x + 1, y + 1, z + 1}) + ); } /// @brief Check block grounding diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 308604ca9..71a3eac0e 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -64,7 +64,7 @@ Level::Level( chunks->setOnUnload([this](Chunk& chunk) { events->trigger(LevelEventType::CHUNK_UNLOAD, &chunk); AABB aabb = chunk.getAABB(); - entities->despawn(entities->getAllInside(aabb)); + entities->despawn(entities->getAllInside(aabb.a, aabb.b)); }); inventories = std::make_unique(*this); }