mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
Transform::pos, Hitbox::position and Player::position were floats. At 65k blocks the float step is 8 mm, at 1M it is 6-12 cm, at 30M it is 2 m. The physics moves a body by 3 cm or less per substep and pushes it out of a wall by 10 cm or less: steps got rounded to the grid (the body changed its speed, then stopped), the AABB of a body was off by a float step and gave false collisions with the blocks around. Positions of bodies, entity transforms, players, spawn points and the skeleton position interpolation are worldvec3 now. Collisions are still calculated in floats, but in the coordinates relative to the block the body is in: the solver takes the origin, block lookups and hitboxes of other bodies are shifted by it, the result is added back in doubles. So the precision of the physics is the same anywhere in the world. A calculated sensor is stored relative to the position of its entity. Entities::getAllInside takes the corners in doubles, Entities::hasBlockingInside takes the origin of the AABB. The save format does not change: positions are written as numbers (float64) as before, saves are compatible in both directions. Lua functions keep their signatures, they only stop rounding to float.
209 lines
6.4 KiB
C++
209 lines
6.4 KiB
C++
#include "EntitiesRenderer.hpp"
|
|
|
|
#include "animation/rigging.hpp"
|
|
#include "assets/Assets.hpp"
|
|
#include "graphics/core/DrawContext.hpp"
|
|
#include "graphics/core/LineBatch.hpp"
|
|
#include "maths/FrustumCulling.hpp"
|
|
#include "maths/util.hpp"
|
|
#include "objects/Entities.hpp"
|
|
#include "objects/Entity.hpp"
|
|
#include "objects/EntityDef.hpp"
|
|
#include "ModelBatch.hpp"
|
|
|
|
static void debug_render_skeleton(
|
|
LineBatch& batch,
|
|
const rigging::Bone* bone,
|
|
const rigging::Skeleton& skeleton
|
|
) {
|
|
size_t pindex = bone->getIndex();
|
|
for (auto& sub : bone->getBones()) {
|
|
size_t sindex = sub->getIndex();
|
|
const auto& matrices = skeleton.calculated.matrices;
|
|
batch.line(
|
|
glm::vec3(matrices[pindex] * glm::vec4(0, 0, 0, 1)),
|
|
glm::vec3(matrices[sindex] * glm::vec4(0, 0, 0, 1)),
|
|
glm::vec4(0, 0.5f, 0, 1)
|
|
);
|
|
debug_render_skeleton(batch, sub.get(), skeleton);
|
|
}
|
|
}
|
|
|
|
static glm::vec3 calc_extent(
|
|
const Transform& transform,
|
|
const Rigidbody& rigidbody,
|
|
const rigging::Skeleton& skeleton
|
|
) {
|
|
const auto& size = transform.size;
|
|
float scale = glm::max(
|
|
glm::abs(size.x), glm::max(glm::abs(size.y), glm::abs(size.z))
|
|
);
|
|
return glm::max(
|
|
rigidbody.hitbox.getHalfSize(), glm::vec3(skeleton.boundsRadius * scale)
|
|
);
|
|
}
|
|
|
|
static bool check_frustum(
|
|
const Frustum& frustum,
|
|
const Transform& transform,
|
|
const Rigidbody& rigidbody,
|
|
const rigging::Skeleton& skeleton,
|
|
const glm::ivec3& origin
|
|
) {
|
|
glm::vec3 pos = util::to_local(transform.pos, origin);
|
|
glm::vec3 ext = calc_extent(transform, rigidbody, skeleton);
|
|
return frustum.isBoxVisible(pos - ext, pos + ext);
|
|
}
|
|
|
|
EntitiesRenderer::EntitiesRenderer(const Assets& assets, Entities& entities)
|
|
: assets(assets), entities(entities) {
|
|
}
|
|
|
|
void EntitiesRenderer::render(
|
|
ModelBatch& batch,
|
|
const glm::ivec3& origin,
|
|
const Frustum* frustum,
|
|
entityid_t fpsEntity
|
|
) {
|
|
batch.setLightsOffset(origin);
|
|
for (const auto& [uid, entity] : entities.getEntities()) {
|
|
if (!entity->getSkeleton() || uid == fpsEntity) {
|
|
continue;
|
|
}
|
|
const auto& transform = entity->getTransform();
|
|
const auto& rigidbody = entity->getRigidbody();
|
|
auto& skeleton = *entity->getSkeleton();
|
|
const auto& def = entity->getID().def;
|
|
const auto& pos = transform.pos;
|
|
const auto& size = transform.size;
|
|
const auto& rigConfig = skeleton.config;
|
|
if (rigConfig == nullptr) {
|
|
continue;
|
|
}
|
|
if (frustum &&
|
|
!check_frustum(*frustum, transform, rigidbody, skeleton, origin)) {
|
|
if (!skeleton.boundsDirty || !skeleton.visible) {
|
|
continue;
|
|
}
|
|
rigConfig->updateBounds(
|
|
assets, skeleton, transform.rot, pos, size, origin
|
|
);
|
|
if (!check_frustum(
|
|
*frustum, transform, rigidbody, skeleton, origin
|
|
)) {
|
|
continue;
|
|
}
|
|
}
|
|
rigConfig->render(
|
|
assets,
|
|
batch,
|
|
skeleton,
|
|
def.lightingMode,
|
|
transform.rot,
|
|
pos,
|
|
size,
|
|
origin,
|
|
def.translucent
|
|
);
|
|
}
|
|
}
|
|
|
|
void EntitiesRenderer::renderDebug(
|
|
LineBatch& batch,
|
|
const glm::ivec3& origin,
|
|
const Frustum* frustum,
|
|
const DrawContext& pctx,
|
|
entityid_t fpsEntity
|
|
) {
|
|
const auto& all = entities.getEntities();
|
|
{
|
|
auto ctx = pctx.sub(&batch);
|
|
ctx.setLineWidth(1);
|
|
for (const auto& [uid, entity] : all) {
|
|
if (!entity->getSkeleton()) {
|
|
continue;
|
|
}
|
|
const auto& transform = entity->getTransform();
|
|
const auto& rigidbody = entity->getRigidbody();
|
|
const auto& skeleton = *entity->getSkeleton();
|
|
const auto& hitbox = rigidbody.hitbox;
|
|
if (frustum &&
|
|
!check_frustum(
|
|
*frustum, transform, rigidbody, skeleton, origin
|
|
)) {
|
|
continue;
|
|
}
|
|
batch.box(
|
|
util::to_local(hitbox.position, origin),
|
|
hitbox.getHalfSize() * 2.0f,
|
|
glm::vec4(1.0f)
|
|
);
|
|
glm::vec3 ext = calc_extent(transform, rigidbody, skeleton);
|
|
batch.box(
|
|
util::to_local(transform.pos, origin),
|
|
ext * 2.0f,
|
|
glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)
|
|
);
|
|
for (auto& sensor : rigidbody.sensors) {
|
|
if (sensor.type != SensorType::AABB) continue;
|
|
batch.box(
|
|
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)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
{
|
|
auto ctx = pctx.sub(&batch);
|
|
ctx.setDepthTest(false);
|
|
ctx.setDepthMask(false);
|
|
ctx.setLineWidth(2);
|
|
for (const auto& [uid, entity] : all) {
|
|
if (!entity->getSkeleton() || uid == fpsEntity) {
|
|
continue;
|
|
}
|
|
const auto& transform = entity->getTransform();
|
|
const auto& rigidbody = entity->getRigidbody();
|
|
const auto& skeleton = *entity->getSkeleton();
|
|
auto config = skeleton.config;
|
|
if (config == nullptr) {
|
|
continue;
|
|
}
|
|
if (frustum &&
|
|
!check_frustum(
|
|
*frustum, transform, rigidbody, skeleton, origin
|
|
)) {
|
|
continue;
|
|
}
|
|
auto bone = config->getRoot();
|
|
debug_render_skeleton(batch, bone, skeleton);
|
|
}
|
|
}
|
|
}
|
|
|
|
void EntitiesRenderer::updateCulling(
|
|
const glm::ivec3& origin, const Frustum* frustum, entityid_t fpsEntity
|
|
) {
|
|
for (const auto& [uid, entity] : entities.getEntities()) {
|
|
if (!entity->getSkeleton()) {
|
|
continue;
|
|
}
|
|
auto& skeleton = *entity->getSkeleton();
|
|
if (frustum == nullptr || uid == fpsEntity) {
|
|
skeleton.culled = false;
|
|
continue;
|
|
}
|
|
skeleton.culled = !check_frustum(
|
|
*frustum,
|
|
entity->getTransform(),
|
|
entity->getRigidbody(),
|
|
skeleton,
|
|
origin
|
|
);
|
|
}
|
|
}
|