add triggers

This commit is contained in:
MihailRis
2024-06-30 16:25:08 +03:00
parent 50c714692a
commit 5769be8ec8
15 changed files with 239 additions and 64 deletions
+16
View File
@@ -1,8 +1,24 @@
#ifndef PHYSICS_HITBOX_HPP_
#define PHYSICS_HITBOX_HPP_
#include "../maths/aabb.hpp"
#include "../typedefs.hpp"
#include <set>
#include <functional>
#include <glm/glm.hpp>
struct Trigger {
bool enabled = true;
entityid_t entity;
AABB aabb;
AABB calculated;
std::set<entityid_t> prevEntered;
std::set<entityid_t> nextEntered;
std::function<void(entityid_t, size_t, entityid_t)> enterCallback;
std::function<void(entityid_t, size_t, entityid_t)> exitCallback;
};
struct Hitbox {
glm::vec3 position;
glm::vec3 halfsize;
+20 -2
View File
@@ -6,12 +6,14 @@
#include "../voxels/Chunks.hpp"
#include "../voxels/voxel.hpp"
#include <iostream>
const float E = 0.03f;
const float MAX_FIX = 0.1f;
PhysicsSolver::PhysicsSolver(glm::vec3 gravity) : gravity(gravity) {
}
#include "../util/timeutil.hpp"
void PhysicsSolver::step(
Chunks* chunks,
Hitbox* hitbox,
@@ -19,7 +21,8 @@ void PhysicsSolver::step(
uint substeps,
bool shifting,
float gravityScale,
bool collisions
bool collisions,
entityid_t entity
) {
float dt = delta / static_cast<float>(substeps);
float linearDamping = hitbox->linearDamping;
@@ -78,6 +81,21 @@ void PhysicsSolver::step(
hitbox->grounded = true;
}
}
AABB aabb;
aabb.a = hitbox->position - hitbox->halfsize;
aabb.b = hitbox->position + hitbox->halfsize;
for (size_t i = 0; i < triggers.size(); i++) {
auto& trigger = triggers[i];
if (trigger->entity == entity) {
continue;
}
if (aabb.intersect(trigger->calculated)) {
if (trigger->prevEntered.find(entity) == trigger->prevEntered.end()) {
trigger->enterCallback(trigger->entity, i, entity);
}
trigger->nextEntered.insert(entity);
}
}
}
void PhysicsSolver::colisionCalc(
+15 -6
View File
@@ -1,17 +1,21 @@
#ifndef PHYSICS_PHYSICSSOLVER_HPP_
#define PHYSICS_PHYSICSSOLVER_HPP_
#include "Hitbox.hpp"
#include "../typedefs.hpp"
#include "../voxels/voxel.hpp"
#include <vector>
#include <glm/glm.hpp>
class Block;
class Chunks;
struct Hitbox;
struct Trigger;
class PhysicsSolver {
glm::vec3 gravity;
std::vector<Trigger*> triggers;
public:
PhysicsSolver(glm::vec3 gravity);
void step(
@@ -21,18 +25,23 @@ public:
uint substeps,
bool shifting,
float gravityScale,
bool collisions
bool collisions,
entityid_t entity
);
void colisionCalc(
Chunks* chunks,
Hitbox* hitbox,
glm::vec3& vel,
glm::vec3& pos,
Chunks* chunks,
Hitbox* hitbox,
glm::vec3& vel,
glm::vec3& pos,
const glm::vec3 half,
float stepHeight
);
bool isBlockInside(int x, int y, int z, Hitbox* hitbox);
bool isBlockInside(int x, int y, int z, Block* def, blockstate state, Hitbox* hitbox);
void setTriggers(std::vector<Trigger*> triggers) {
this->triggers = std::move(triggers);
}
};
#endif // PHYSICS_PHYSICSSOLVER_HPP_