mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-13 15:01:49 +00:00
applying velocity of ground the worst way possible
This commit is contained in:
parent
7f09c47ffb
commit
38354c42f9
8 changed files with 72 additions and 37 deletions
|
|
@ -229,8 +229,8 @@ PlayerController::PlayerController(
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::onFootstep(const Hitbox& hitbox) {
|
void PlayerController::onFootstep(const Hitbox& hitbox) {
|
||||||
auto pos = hitbox.position;
|
const auto& pos = hitbox.position;
|
||||||
auto half = hitbox.halfsize;
|
const auto& half = hitbox.getHalfSize();
|
||||||
|
|
||||||
for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
|
for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
|
||||||
for (int offsetX = -1; offsetX <= 1; offsetX++) {
|
for (int offsetX = -1; offsetX <= 1; offsetX++) {
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ static int l_set_pos(lua::State* L) {
|
||||||
auto vec = lua::tovec3(L, 2);
|
auto vec = lua::tovec3(L, 2);
|
||||||
check_valid(vec);
|
check_valid(vec);
|
||||||
entity->getTransform().setPos(vec);
|
entity->getTransform().setPos(vec);
|
||||||
entity->getRigidbody().hitbox.position = vec;
|
entity->getRigidbody().hitbox.setPos(vec);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -316,30 +316,36 @@ void Entities::updatePhysics(float delta) {
|
||||||
|
|
||||||
auto view = registry->view<EntityId, Transform, Rigidbody>();
|
auto view = registry->view<EntityId, Transform, Rigidbody>();
|
||||||
auto physics = level.physics.get();
|
auto physics = level.physics.get();
|
||||||
for (auto [entity, eid, transform, rigidbody] : view.each()) {
|
|
||||||
if (!rigidbody.enabled || rigidbody.hitbox.type == BodyType::STATIC) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
auto& hitbox = rigidbody.hitbox;
|
|
||||||
auto prevVel = hitbox.velocity;
|
|
||||||
bool grounded = hitbox.grounded;
|
|
||||||
|
|
||||||
float vel = glm::length(prevVel);
|
for (int solid = false; solid <= true; solid++) {
|
||||||
int substeps = static_cast<int>(delta * vel * 20);
|
for (auto [entity, eid, transform, rigidbody] : view.each()) {
|
||||||
substeps = std::min(100, std::max(2, substeps));
|
if (!rigidbody.enabled || rigidbody.hitbox.type == BodyType::STATIC) {
|
||||||
physics->step(*level.chunks, hitbox, delta, substeps, eid.uid);
|
continue;
|
||||||
hitbox.friction = glm::abs(hitbox.gravityScale <= 1e-7f)
|
}
|
||||||
? 8.0f
|
if (eid.def.solid == solid) {
|
||||||
: (!grounded ? 2.0f : 10.0f);
|
continue;
|
||||||
hitbox.scale = transform.size;
|
}
|
||||||
transform.setPos(hitbox.position);
|
auto& hitbox = rigidbody.hitbox;
|
||||||
if (hitbox.grounded && !grounded) {
|
auto prevVel = hitbox.velocity;
|
||||||
scripting::on_entity_grounded(
|
bool grounded = hitbox.grounded;
|
||||||
*get(eid.uid), glm::length(prevVel - hitbox.velocity)
|
|
||||||
);
|
float vel = glm::length(prevVel);
|
||||||
}
|
int substeps = static_cast<int>(delta * vel * 20);
|
||||||
if (!hitbox.grounded && grounded) {
|
substeps = std::min(100, std::max(2, substeps));
|
||||||
scripting::on_entity_fall(*get(eid.uid));
|
physics->step(*level.chunks, hitbox, delta, substeps, eid.uid);
|
||||||
|
hitbox.friction = glm::abs(hitbox.gravityScale <= 1e-7f)
|
||||||
|
? 8.0f
|
||||||
|
: (!grounded ? 2.0f : 10.0f);
|
||||||
|
hitbox.scale = transform.size;
|
||||||
|
transform.setPos(hitbox.position);
|
||||||
|
if (hitbox.grounded && !grounded) {
|
||||||
|
scripting::on_entity_grounded(
|
||||||
|
*get(eid.uid), glm::length(prevVel - hitbox.velocity)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!hitbox.grounded && grounded) {
|
||||||
|
scripting::on_entity_fall(*get(eid.uid));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,7 @@ void Player::teleport(glm::vec3 position) {
|
||||||
this->position = position;
|
this->position = position;
|
||||||
|
|
||||||
if (auto entity = level.entities->get(eid)) {
|
if (auto entity = level.entities->get(eid)) {
|
||||||
entity->getRigidbody().hitbox.position = position;
|
entity->getRigidbody().hitbox.setPos(position);
|
||||||
entity->getTransform().setPos(position);
|
entity->getTransform().setPos(position);
|
||||||
entity->setInterpolatedPosition(position);
|
entity->setInterpolatedPosition(position);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,6 @@ Hitbox::Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize)
|
||||||
: type(type),
|
: type(type),
|
||||||
position(position),
|
position(position),
|
||||||
halfsize(halfsize),
|
halfsize(halfsize),
|
||||||
velocity(0.0f,0.0f,0.0f)
|
velocity(0.0f,0.0f,0.0f),
|
||||||
|
prevPosition(position)
|
||||||
{}
|
{}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
|
|
||||||
#include "maths/aabb.hpp"
|
#include "maths/aabb.hpp"
|
||||||
#include "typedefs.hpp"
|
#include "typedefs.hpp"
|
||||||
#include "util/EnumMetadata.hpp"
|
#include "util/EnumMetadata.hpp"
|
||||||
|
|
@ -8,6 +10,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtx/norm.hpp>
|
||||||
|
|
||||||
enum class SensorType {
|
enum class SensorType {
|
||||||
AABB,
|
AABB,
|
||||||
|
|
@ -63,6 +66,13 @@ struct Hitbox {
|
||||||
float stepHeight = 0.5f;
|
float stepHeight = 0.5f;
|
||||||
glm::vec3 groundVelocity {};
|
glm::vec3 groundVelocity {};
|
||||||
|
|
||||||
|
// garbage tbh
|
||||||
|
glm::vec3 prevPosition {};
|
||||||
|
int substeps = 1;
|
||||||
|
float delta = 0.0f;
|
||||||
|
|
||||||
|
static inline constexpr float TELEPORT_THRESOLD_SQR = 0.5f;
|
||||||
|
|
||||||
Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize);
|
Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize);
|
||||||
|
|
||||||
AABB getAABB() const {
|
AABB getAABB() const {
|
||||||
|
|
@ -72,4 +82,11 @@ struct Hitbox {
|
||||||
glm::vec3 getHalfSize() const {
|
glm::vec3 getHalfSize() const {
|
||||||
return halfsize * scale;
|
return halfsize * scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setPos(const glm::vec3& vec) {
|
||||||
|
position = vec;
|
||||||
|
if (glm::distance2(position, prevPosition) >= TELEPORT_THRESOLD_SQR) {
|
||||||
|
prevPosition = vec;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -208,7 +208,7 @@ static bool calc_collision_neg_y(
|
||||||
if (pos.y < newy && glm::abs(pos.y - newy) < 0.5f) {
|
if (pos.y < newy && glm::abs(pos.y - newy) < 0.5f) {
|
||||||
pos.y = newy;
|
pos.y = newy;
|
||||||
}
|
}
|
||||||
groundVelocty = box->velocity;
|
groundVelocty = (box->position - box->prevPosition) / box->delta;
|
||||||
if (vel.y < 0.0f) {
|
if (vel.y < 0.0f) {
|
||||||
vel.y = 0.0f;
|
vel.y = 0.0f;
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -362,8 +362,14 @@ void PhysicsSolver::calcSubstep(
|
||||||
glm::vec3& vel,
|
glm::vec3& vel,
|
||||||
glm::vec3& pos,
|
glm::vec3& pos,
|
||||||
bool prevGrounded,
|
bool prevGrounded,
|
||||||
float dt
|
float dt,
|
||||||
|
int substeps
|
||||||
) {
|
) {
|
||||||
|
if (hitbox.grounded) {
|
||||||
|
pos.x += hitbox.groundVelocity.x * dt * substeps;
|
||||||
|
pos.z += hitbox.groundVelocity.z * dt * substeps;
|
||||||
|
}
|
||||||
|
|
||||||
auto initpos = pos;
|
auto initpos = pos;
|
||||||
auto half = hitbox.getHalfSize();
|
auto half = hitbox.getHalfSize();
|
||||||
float gravityScale = hitbox.gravityScale;
|
float gravityScale = hitbox.gravityScale;
|
||||||
|
|
@ -434,6 +440,10 @@ void PhysicsSolver::step(
|
||||||
uint substeps,
|
uint substeps,
|
||||||
entityid_t entity
|
entityid_t entity
|
||||||
) {
|
) {
|
||||||
|
hitbox.prevPosition = hitbox.position;
|
||||||
|
hitbox.substeps = substeps;
|
||||||
|
hitbox.delta = delta;
|
||||||
|
|
||||||
float dt = delta / static_cast<float>(substeps);
|
float dt = delta / static_cast<float>(substeps);
|
||||||
float linearDamping = hitbox.linearDamping * hitbox.friction;
|
float linearDamping = hitbox.linearDamping * hitbox.friction;
|
||||||
|
|
||||||
|
|
@ -443,12 +453,7 @@ void PhysicsSolver::step(
|
||||||
bool prevGrounded = hitbox.grounded;
|
bool prevGrounded = hitbox.grounded;
|
||||||
hitbox.grounded = false;
|
hitbox.grounded = false;
|
||||||
for (uint i = 0; i < substeps; i++) {
|
for (uint i = 0; i < substeps; i++) {
|
||||||
calcSubstep(chunks, hitbox, vel, pos, prevGrounded, dt);
|
calcSubstep(chunks, hitbox, vel, pos, prevGrounded, dt, substeps);
|
||||||
}
|
|
||||||
|
|
||||||
if (prevGrounded && !hitbox.grounded) {
|
|
||||||
vel += hitbox.groundVelocity;
|
|
||||||
hitbox.groundVelocity = {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vel.x /= 1.0f + delta * linearDamping;
|
vel.x /= 1.0f + delta * linearDamping;
|
||||||
|
|
@ -456,6 +461,11 @@ void PhysicsSolver::step(
|
||||||
if (hitbox.verticalDamping > 0.0f) {
|
if (hitbox.verticalDamping > 0.0f) {
|
||||||
vel.y /= 1.0f + delta * linearDamping * hitbox.verticalDamping;
|
vel.y /= 1.0f + delta * linearDamping * hitbox.verticalDamping;
|
||||||
}
|
}
|
||||||
|
if (prevGrounded && !hitbox.grounded) {
|
||||||
|
auto appliedVelocity = hitbox.groundVelocity;
|
||||||
|
vel += appliedVelocity;
|
||||||
|
hitbox.groundVelocity = {};
|
||||||
|
}
|
||||||
|
|
||||||
AABB aabb;
|
AABB aabb;
|
||||||
aabb.a = pos - hitbox.getHalfSize();
|
aabb.a = pos - hitbox.getHalfSize();
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@ private:
|
||||||
glm::vec3& vel,
|
glm::vec3& vel,
|
||||||
glm::vec3& pos,
|
glm::vec3& pos,
|
||||||
bool prevGrounded,
|
bool prevGrounded,
|
||||||
float dt
|
float dt,
|
||||||
|
int substeps
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue