mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-08 12:31:51 +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
|
|
@ -6,5 +6,6 @@ Hitbox::Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize)
|
|||
: type(type),
|
||||
position(position),
|
||||
halfsize(halfsize),
|
||||
velocity(0.0f,0.0f,0.0f)
|
||||
velocity(0.0f,0.0f,0.0f),
|
||||
prevPosition(position)
|
||||
{}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
|
||||
#include "maths/aabb.hpp"
|
||||
#include "typedefs.hpp"
|
||||
#include "util/EnumMetadata.hpp"
|
||||
|
|
@ -8,6 +10,7 @@
|
|||
#include <string>
|
||||
#include <functional>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtx/norm.hpp>
|
||||
|
||||
enum class SensorType {
|
||||
AABB,
|
||||
|
|
@ -62,6 +65,13 @@ struct Hitbox {
|
|||
bool crouching = false;
|
||||
float stepHeight = 0.5f;
|
||||
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);
|
||||
|
||||
|
|
@ -72,4 +82,11 @@ struct Hitbox {
|
|||
glm::vec3 getHalfSize() const {
|
||||
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) {
|
||||
pos.y = newy;
|
||||
}
|
||||
groundVelocty = box->velocity;
|
||||
groundVelocty = (box->position - box->prevPosition) / box->delta;
|
||||
if (vel.y < 0.0f) {
|
||||
vel.y = 0.0f;
|
||||
return true;
|
||||
|
|
@ -362,8 +362,14 @@ void PhysicsSolver::calcSubstep(
|
|||
glm::vec3& vel,
|
||||
glm::vec3& pos,
|
||||
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 half = hitbox.getHalfSize();
|
||||
float gravityScale = hitbox.gravityScale;
|
||||
|
|
@ -434,6 +440,10 @@ void PhysicsSolver::step(
|
|||
uint substeps,
|
||||
entityid_t entity
|
||||
) {
|
||||
hitbox.prevPosition = hitbox.position;
|
||||
hitbox.substeps = substeps;
|
||||
hitbox.delta = delta;
|
||||
|
||||
float dt = delta / static_cast<float>(substeps);
|
||||
float linearDamping = hitbox.linearDamping * hitbox.friction;
|
||||
|
||||
|
|
@ -443,12 +453,7 @@ void PhysicsSolver::step(
|
|||
bool prevGrounded = hitbox.grounded;
|
||||
hitbox.grounded = false;
|
||||
for (uint i = 0; i < substeps; i++) {
|
||||
calcSubstep(chunks, hitbox, vel, pos, prevGrounded, dt);
|
||||
}
|
||||
|
||||
if (prevGrounded && !hitbox.grounded) {
|
||||
vel += hitbox.groundVelocity;
|
||||
hitbox.groundVelocity = {};
|
||||
calcSubstep(chunks, hitbox, vel, pos, prevGrounded, dt, substeps);
|
||||
}
|
||||
|
||||
vel.x /= 1.0f + delta * linearDamping;
|
||||
|
|
@ -456,6 +461,11 @@ void PhysicsSolver::step(
|
|||
if (hitbox.verticalDamping > 0.0f) {
|
||||
vel.y /= 1.0f + delta * linearDamping * hitbox.verticalDamping;
|
||||
}
|
||||
if (prevGrounded && !hitbox.grounded) {
|
||||
auto appliedVelocity = hitbox.groundVelocity;
|
||||
vel += appliedVelocity;
|
||||
hitbox.groundVelocity = {};
|
||||
}
|
||||
|
||||
AABB aabb;
|
||||
aabb.a = pos - hitbox.getHalfSize();
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ private:
|
|||
glm::vec3& vel,
|
||||
glm::vec3& pos,
|
||||
bool prevGrounded,
|
||||
float dt
|
||||
float dt,
|
||||
int substeps
|
||||
);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue