add BodyType, fix crouching

This commit is contained in:
MihailRis
2024-07-09 06:39:05 +03:00
parent 7e80642ec3
commit 0a14d6220a
13 changed files with 131 additions and 34 deletions
+22 -2
View File
@@ -1,7 +1,27 @@
#include "Hitbox.hpp"
Hitbox::Hitbox(glm::vec3 position, glm::vec3 halfsize)
: position(position),
#include <stdexcept>
std::optional<BodyType> BodyType_from(std::string_view str) {
if (str == "kinematic") {
return BodyType::KINEMATIC;
} else if (str == "dynamic") {
return BodyType::DYNAMIC;
}
return std::nullopt;
}
std::string to_string(BodyType type) {
switch (type) {
case BodyType::KINEMATIC: return "kinematic";
case BodyType::DYNAMIC: return "dynamic";
default: return "unknown";
}
}
Hitbox::Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize)
: type(type),
position(position),
halfsize(halfsize),
velocity(0.0f,0.0f,0.0f),
linearDamping(0.1f)
+12 -1
View File
@@ -5,6 +5,8 @@
#include "../typedefs.hpp"
#include <set>
#include <string>
#include <optional>
#include <functional>
#include <glm/glm.hpp>
@@ -36,7 +38,15 @@ struct Trigger {
triggercallback exitCallback;
};
enum class BodyType {
KINEMATIC, DYNAMIC
};
std::optional<BodyType> BodyType_from(std::string_view str);
std::string to_string(BodyType type);
struct Hitbox {
BodyType type;
glm::vec3 position;
glm::vec3 halfsize;
glm::vec3 velocity;
@@ -44,8 +54,9 @@ struct Hitbox {
bool verticalDamping = false;
bool grounded = false;
float gravityScale = 1.0f;
bool crouching = false;
Hitbox(glm::vec3 position, glm::vec3 halfsize);
Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize);
};
#endif // PHYSICS_HITBOX_HPP_
+2 -4
View File
@@ -21,8 +21,6 @@ void PhysicsSolver::step(
Hitbox* hitbox,
float delta,
uint substeps,
bool shifting,
bool collisions,
entityid_t entity
) {
float dt = delta / static_cast<float>(substeps);
@@ -42,7 +40,7 @@ void PhysicsSolver::step(
float pz = pos.z;
vel += gravity * dt * gravityScale;
if (collisions) {
if (hitbox->type == BodyType::DYNAMIC) {
colisionCalc(chunks, hitbox, vel, pos, half,
(prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f);
}
@@ -57,7 +55,7 @@ void PhysicsSolver::step(
pos.y = py;
}
if (shifting && hitbox->grounded){
if (hitbox->crouching && hitbox->grounded){
float y = (pos.y-half.y-E);
hitbox->grounded = false;
for (float x = (px-half.x+E); x <= (px+half.x-E); x+=s){
-2
View File
@@ -23,8 +23,6 @@ public:
Hitbox* hitbox,
float delta,
uint substeps,
bool shifting,
bool collisions,
entityid_t entity
);
void colisionCalc(