mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 14:01:49 +00:00
improve floor collision physics (add mass)
This commit is contained in:
parent
fc57e98a96
commit
3bcb7f7e70
2 changed files with 14 additions and 2 deletions
|
|
@ -64,6 +64,8 @@ struct Hitbox {
|
||||||
float gravityScale = 1.0f;
|
float gravityScale = 1.0f;
|
||||||
bool crouching = false;
|
bool crouching = false;
|
||||||
float stepHeight = 0.5f;
|
float stepHeight = 0.5f;
|
||||||
|
float mass = 1.0f;
|
||||||
|
float elasticity = 0.0f;
|
||||||
std::string material;
|
std::string material;
|
||||||
std::string groundMaterial;
|
std::string groundMaterial;
|
||||||
glm::vec3 groundVelocity {};
|
glm::vec3 groundVelocity {};
|
||||||
|
|
|
||||||
|
|
@ -141,9 +141,18 @@ bool PhysicsSolver::calcCollisionNegY(
|
||||||
if (pos.y < newy && glm::abs(pos.y - newy) < boxhalf.y) {
|
if (pos.y < newy && glm::abs(pos.y - newy) < boxhalf.y) {
|
||||||
pos.y = newy;
|
pos.y = newy;
|
||||||
}
|
}
|
||||||
|
const auto& vA = hitbox.velocity;
|
||||||
|
const auto& vB = box->velocity;
|
||||||
|
const auto& mA = hitbox.mass;
|
||||||
|
const auto& mB = box->mass;
|
||||||
|
|
||||||
|
auto velA = (mA * vA + mB * vB + hitbox.elasticity * mB * (vB - vA)) / (mA + mB);
|
||||||
|
auto velB = (mA * vA + mB * vB + box->elasticity * mA * (vA - vB)) / (mA + mB);
|
||||||
|
|
||||||
hitbox.groundVelocity = box->position - box->prevPosition;
|
hitbox.groundVelocity = box->position - box->prevPosition;
|
||||||
if (vel.y < hitbox.groundVelocity.y / dt) {
|
if (vel.y < hitbox.groundVelocity.y / dt) {
|
||||||
vel.y = hitbox.groundVelocity.y / dt * (1.0f - E);
|
vel.y = velA.y;
|
||||||
|
box->velocity.y = velB.y;
|
||||||
if (hitbox.groundMaterial.empty() && !box->material.empty()) {
|
if (hitbox.groundMaterial.empty() && !box->material.empty()) {
|
||||||
hitbox.groundMaterial = box->material;
|
hitbox.groundMaterial = box->material;
|
||||||
}
|
}
|
||||||
|
|
@ -171,8 +180,9 @@ bool PhysicsSolver::calcCollisionNegY(
|
||||||
if (const auto obstacle =
|
if (const auto obstacle =
|
||||||
chunks.isObstacleAt(coord.x, coord.y, coord.z, aabb)) {
|
chunks.isObstacleAt(coord.x, coord.y, coord.z, aabb)) {
|
||||||
float newy = std::floor(coord.y) + half.y + obstacle->max().y;
|
float newy = std::floor(coord.y) + half.y + obstacle->max().y;
|
||||||
|
|
||||||
if (newy >= pos.y) {
|
if (newy >= pos.y) {
|
||||||
vel.y = 0.0f;
|
vel.y = -hitbox.elasticity * vel.y;
|
||||||
pos.y = newy;
|
pos.y = newy;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue