mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-23 02:49:46 +00:00
move to namespace
This commit is contained in:
parent
67ed7ac582
commit
8798b34423
8 changed files with 63 additions and 45 deletions
|
|
@ -24,8 +24,8 @@ static glm::vec3 calc_nearby_reach(
|
||||||
|
|
||||||
glm::vec3 reach(
|
glm::vec3 reach(
|
||||||
0.5f,
|
0.5f,
|
||||||
hitbox.stepHeight * 2.0f + E * 8.0f + ext.y * 0.5f +
|
hitbox.stepHeight * 2.0f + physics::E * 8.0f + ext.y * 0.5f +
|
||||||
maxSolidHalfY + 0.5f + MAX_FIX * 2.0f,
|
maxSolidHalfY + 0.5f + physics::MAX_FIX * 2.0f,
|
||||||
0.5f
|
0.5f
|
||||||
);
|
);
|
||||||
reach += ext + budget;
|
reach += ext + budget;
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ void PhysicsSolver::step(
|
||||||
float dt = delta / static_cast<float>(substeps);
|
float dt = delta / static_cast<float>(substeps);
|
||||||
broadPhase.build(delta);
|
broadPhase.build(delta);
|
||||||
for (auto hitbox : hitboxes) {
|
for (auto hitbox : hitboxes) {
|
||||||
schedule_substep(*hitbox, gravity, 0, substeps, dt);
|
physics::schedule_substep(*hitbox, gravity, 0, substeps, dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint i = 0; i < substeps; i++) {
|
for (uint i = 0; i < substeps; i++) {
|
||||||
|
|
@ -44,12 +44,14 @@ void PhysicsSolver::step(
|
||||||
dt * static_cast<float>(i + 1 - hitbox->doneSubsteps);
|
dt * static_cast<float>(i + 1 - hitbox->doneSubsteps);
|
||||||
while (hitbox->doneSubsteps <= i) {
|
while (hitbox->doneSubsteps <= i) {
|
||||||
uint left = i + 1 - hitbox->doneSubsteps;
|
uint left = i + 1 - hitbox->doneSubsteps;
|
||||||
uint skip = calc_substep_stride(*hitbox, gravity, dt, left) - 1;
|
uint skip =
|
||||||
|
physics::calc_substep_stride(*hitbox, gravity, dt, left) -
|
||||||
|
1;
|
||||||
if (skip > 0) {
|
if (skip > 0) {
|
||||||
integrate(*hitbox, gravity, dt, skip);
|
physics::integrate(*hitbox, gravity, dt, skip);
|
||||||
hitbox->doneSubsteps += skip;
|
hitbox->doneSubsteps += skip;
|
||||||
}
|
}
|
||||||
calc_substep(
|
physics::calc_substep(
|
||||||
*hitbox,
|
*hitbox,
|
||||||
{this->chunks,
|
{this->chunks,
|
||||||
broadPhase.getNearbySolids(j),
|
broadPhase.getNearbySolids(j),
|
||||||
|
|
@ -61,7 +63,7 @@ void PhysicsSolver::step(
|
||||||
|
|
||||||
broadPhase.update(j, dt * static_cast<float>(substeps - i));
|
broadPhase.update(j, dt * static_cast<float>(substeps - i));
|
||||||
}
|
}
|
||||||
schedule_substep(*hitbox, gravity, i + 1, substeps, dt);
|
physics::schedule_substep(*hitbox, gravity, i + 1, substeps, dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,7 +86,7 @@ void PhysicsSolver::step(
|
||||||
hitbox->groundVelocity *= 1.0f - delta;
|
hitbox->groundVelocity *= 1.0f - delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
update_sensors(*hitbox, sensors);
|
physics::update_sensors(*hitbox, sensors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@
|
||||||
#define GLM_ENABLE_EXPERIMENTAL
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
#include <glm/gtx/norm.hpp>
|
#include <glm/gtx/norm.hpp>
|
||||||
|
|
||||||
|
using namespace physics;
|
||||||
|
|
||||||
static glm::vec3 calc_collsion_velocity_result(
|
static glm::vec3 calc_collsion_velocity_result(
|
||||||
const Hitbox& a, const Hitbox& b
|
const Hitbox& a, const Hitbox& b
|
||||||
) {
|
) {
|
||||||
|
|
@ -316,7 +318,7 @@ static void calc_collisions(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void calc_substep(Hitbox& hitbox, const CollisionContext& ctx) {
|
void physics::calc_substep(Hitbox& hitbox, const CollisionContext& ctx) {
|
||||||
const auto& chunks = ctx.chunks;
|
const auto& chunks = ctx.chunks;
|
||||||
const auto& solids = ctx.solids;
|
const auto& solids = ctx.solids;
|
||||||
const auto& gravity = ctx.gravity;
|
const auto& gravity = ctx.gravity;
|
||||||
|
|
|
||||||
|
|
@ -9,15 +9,17 @@
|
||||||
|
|
||||||
class GlobalChunks;
|
class GlobalChunks;
|
||||||
|
|
||||||
inline constexpr float E = 0.03f;
|
namespace physics {
|
||||||
inline constexpr float MAX_FIX = 0.1f;
|
inline constexpr float E = 0.03f;
|
||||||
|
inline constexpr float MAX_FIX = 0.1f;
|
||||||
|
|
||||||
struct CollisionContext {
|
struct CollisionContext {
|
||||||
const GlobalChunks& chunks;
|
const GlobalChunks& chunks;
|
||||||
const std::vector<Hitbox*>& solids;
|
const std::vector<Hitbox*>& solids;
|
||||||
glm::vec3 gravity;
|
glm::vec3 gravity;
|
||||||
float dt;
|
float dt;
|
||||||
uint wakeSubstep;
|
uint wakeSubstep;
|
||||||
};
|
};
|
||||||
|
|
||||||
void calc_substep(Hitbox& hitbox, const CollisionContext& ctx);
|
void calc_substep(Hitbox& hitbox, const CollisionContext& ctx);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
inline constexpr float MAX_SUBSTEP_MOVE = 0.03f;
|
inline constexpr float MAX_SUBSTEP_MOVE = 0.03f;
|
||||||
inline constexpr float MAX_SUBSTEP_SINK = 0.015f;
|
inline constexpr float MAX_SUBSTEP_SINK = 0.015f;
|
||||||
|
|
||||||
uint calc_substep_stride(
|
uint physics::calc_substep_stride(
|
||||||
const Hitbox& hitbox, const glm::vec3& gravity, float dt, uint left
|
const Hitbox& hitbox, const glm::vec3& gravity, float dt, uint left
|
||||||
) {
|
) {
|
||||||
if (hitbox.crouching) {
|
if (hitbox.crouching) {
|
||||||
|
|
@ -28,7 +28,7 @@ uint calc_substep_stride(
|
||||||
return glm::max(1u, static_cast<uint>(stride));
|
return glm::max(1u, static_cast<uint>(stride));
|
||||||
}
|
}
|
||||||
|
|
||||||
void schedule_substep(
|
void physics::schedule_substep(
|
||||||
Hitbox& hitbox, const glm::vec3& gravity, uint done, uint substeps, float dt
|
Hitbox& hitbox, const glm::vec3& gravity, uint done, uint substeps, float dt
|
||||||
) {
|
) {
|
||||||
hitbox.doneSubsteps = done;
|
hitbox.doneSubsteps = done;
|
||||||
|
|
|
||||||
|
|
@ -7,27 +7,35 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
inline void integrate(
|
namespace physics {
|
||||||
Hitbox& box, const glm::vec3& gravity, float dt, uint substeps
|
inline void integrate(
|
||||||
) {
|
Hitbox& box, const glm::vec3& gravity, float dt, uint substeps
|
||||||
float n = static_cast<float>(substeps);
|
) {
|
||||||
auto accel = gravity * box.gravityScale;
|
float n = static_cast<float>(substeps);
|
||||||
box.position += box.velocity * (dt * n) +
|
auto accel = gravity * box.gravityScale;
|
||||||
accel * (dt * dt * n * (n + 2.0f) * 0.5f);
|
box.position += box.velocity * (dt * n) +
|
||||||
box.velocity += accel * (dt * n);
|
accel * (dt * dt * n * (n + 2.0f) * 0.5f);
|
||||||
}
|
box.velocity += accel * (dt * n);
|
||||||
|
|
||||||
inline void push_body(Hitbox& box, int axis, float value, uint wakeSubstep) {
|
|
||||||
if (box.velocity[axis] != value) {
|
|
||||||
box.velocity[axis] = value;
|
|
||||||
box.nextSubstep = std::min(box.nextSubstep, wakeSubstep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void push_body(
|
||||||
|
Hitbox& box, int axis, float value, uint wakeSubstep
|
||||||
|
) {
|
||||||
|
if (box.velocity[axis] != value) {
|
||||||
|
box.velocity[axis] = value;
|
||||||
|
box.nextSubstep = std::min(box.nextSubstep, wakeSubstep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint calc_substep_stride(
|
||||||
|
const Hitbox& hitbox, const glm::vec3& gravity, float dt, uint left
|
||||||
|
);
|
||||||
|
|
||||||
|
void schedule_substep(
|
||||||
|
Hitbox& hitbox,
|
||||||
|
const glm::vec3& gravity,
|
||||||
|
uint done,
|
||||||
|
uint substeps,
|
||||||
|
float dt
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint calc_substep_stride(
|
|
||||||
const Hitbox& hitbox, const glm::vec3& gravity, float dt, uint left
|
|
||||||
);
|
|
||||||
|
|
||||||
void schedule_substep(
|
|
||||||
Hitbox& hitbox, const glm::vec3& gravity, uint done, uint substeps, float dt
|
|
||||||
);
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#include "sensors.hpp"
|
#include "sensors.hpp"
|
||||||
|
|
||||||
void update_sensors(Hitbox& hitbox, const std::vector<Sensor*>& sensors) {
|
void physics::update_sensors(
|
||||||
|
Hitbox& hitbox, const std::vector<Sensor*>& sensors
|
||||||
|
) {
|
||||||
auto aabb = hitbox.getAABB();
|
auto aabb = hitbox.getAABB();
|
||||||
|
|
||||||
for (size_t i = 0; i < sensors.size(); i++) {
|
for (size_t i = 0; i < sensors.size(); i++) {
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,6 @@
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
void update_sensors(Hitbox& hitbox, const std::vector<Sensor*>& sensors);
|
namespace physics {
|
||||||
|
void update_sensors(Hitbox& hitbox, const std::vector<Sensor*>& sensors);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue