mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-10 21:41:50 +00:00
cleanup
This commit is contained in:
parent
4b4f2439c0
commit
38b478bc8d
3 changed files with 34 additions and 31 deletions
|
|
@ -1,7 +1,5 @@
|
||||||
#include "Hitbox.hpp"
|
#include "Hitbox.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
Hitbox::Hitbox(
|
Hitbox::Hitbox(
|
||||||
entityid_t entity, BodyType type, glm::vec3 position, glm::vec3 halfsize
|
entityid_t entity, BodyType type, glm::vec3 position, glm::vec3 halfsize
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -392,9 +392,8 @@ void PhysicsSolver::step(
|
||||||
|
|
||||||
for (auto hitbox : hitboxes) {
|
for (auto hitbox : hitboxes) {
|
||||||
float linearDamping = hitbox->linearDamping * hitbox->friction;
|
float linearDamping = hitbox->linearDamping * hitbox->friction;
|
||||||
glm::vec3& pos = hitbox->position;
|
|
||||||
glm::vec3& vel = hitbox->velocity;
|
|
||||||
|
|
||||||
|
glm::vec3& vel = hitbox->velocity;
|
||||||
vel.x /= 1.0f + delta * linearDamping;
|
vel.x /= 1.0f + delta * linearDamping;
|
||||||
vel.z /= 1.0f + delta * linearDamping;
|
vel.z /= 1.0f + delta * linearDamping;
|
||||||
if (hitbox->verticalDamping > 0.0f) {
|
if (hitbox->verticalDamping > 0.0f) {
|
||||||
|
|
@ -406,39 +405,43 @@ void PhysicsSolver::step(
|
||||||
hitbox->groundVelocity = {};
|
hitbox->groundVelocity = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
AABB aabb;
|
updateSensors(*hitbox);
|
||||||
aabb.a = pos - hitbox->getHalfSize();
|
|
||||||
aabb.b = pos + hitbox->getHalfSize();
|
|
||||||
for (size_t i = 0; i < sensors.size(); i++) {
|
|
||||||
auto& sensor = *sensors[i];
|
|
||||||
if (sensor.entity == hitbox->entity) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
bool triggered = false;
|
|
||||||
switch (sensor.type) {
|
|
||||||
case SensorType::AABB:
|
|
||||||
triggered = aabb.intersects(sensor.calculated.aabb);
|
|
||||||
break;
|
|
||||||
case SensorType::RADIUS:
|
|
||||||
triggered = glm::distance2(
|
|
||||||
pos, glm::vec3(sensor.calculated.radial))
|
|
||||||
< sensor.calculated.radial.w;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!triggered) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (sensor.prevEntered.find(hitbox->entity) == sensor.prevEntered.end()) {
|
|
||||||
sensor.enterCallback(sensor.entity, sensor.index, hitbox->entity);
|
|
||||||
}
|
|
||||||
sensor.nextEntered.insert(hitbox->entity);
|
|
||||||
}
|
|
||||||
hitbox->friction = glm::abs(hitbox->gravityScale <= 1e-7f)
|
hitbox->friction = glm::abs(hitbox->gravityScale <= 1e-7f)
|
||||||
? 8.0f
|
? 8.0f
|
||||||
: (!hitbox->prevGrounded ? 2.0f : 10.0f);
|
: (!hitbox->prevGrounded ? 2.0f : 10.0f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PhysicsSolver::updateSensors(Hitbox& hitbox) {
|
||||||
|
auto aabb = hitbox.getAABB();
|
||||||
|
|
||||||
|
for (size_t i = 0; i < sensors.size(); i++) {
|
||||||
|
auto& sensor = *sensors[i];
|
||||||
|
if (sensor.entity == hitbox.entity) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
bool triggered = false;
|
||||||
|
switch (sensor.type) {
|
||||||
|
case SensorType::AABB:
|
||||||
|
triggered = aabb.intersects(sensor.calculated.aabb);
|
||||||
|
break;
|
||||||
|
case SensorType::RADIUS:
|
||||||
|
triggered = glm::distance2(
|
||||||
|
hitbox.position, glm::vec3(sensor.calculated.radial))
|
||||||
|
< sensor.calculated.radial.w;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!triggered) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (sensor.prevEntered.find(hitbox.entity) == sensor.prevEntered.end()) {
|
||||||
|
sensor.enterCallback(sensor.entity, sensor.index, hitbox.entity);
|
||||||
|
}
|
||||||
|
sensor.nextEntered.insert(hitbox.entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void PhysicsSolver::removeSensor(Sensor* sensor) {
|
void PhysicsSolver::removeSensor(Sensor* sensor) {
|
||||||
sensors.erase(
|
sensors.erase(
|
||||||
std::remove(sensors.begin(), sensors.end(), sensor), sensors.end()
|
std::remove(sensors.begin(), sensors.end(), sensor), sensors.end()
|
||||||
|
|
|
||||||
|
|
@ -58,4 +58,6 @@ private:
|
||||||
float dt,
|
float dt,
|
||||||
int substeps
|
int substeps
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void updateSensors(Hitbox& hitbox);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue