mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 05:51:51 +00:00
Merge pull request #788 from MihailRis/physics-related
physics-related works
This commit is contained in:
commit
a5ef4710bc
5 changed files with 16 additions and 9 deletions
|
|
@ -88,4 +88,5 @@ template<> void ContentUnitLoader<EntityDef>::loadUnit(
|
||||||
|
|
||||||
root.at("skeleton-name").get(def.skeletonName);
|
root.at("skeleton-name").get(def.skeletonName);
|
||||||
root.at("blocking").get(def.blocking);
|
root.at("blocking").get(def.blocking);
|
||||||
|
root.at("step-height").get(def.stepHeight);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,9 @@ struct EntityDef {
|
||||||
/// @brief Does entity prevent blocks setup
|
/// @brief Does entity prevent blocks setup
|
||||||
bool blocking = true;
|
bool blocking = true;
|
||||||
|
|
||||||
|
/// @brief Max obstacle height that does not require jumping
|
||||||
|
float stepHeight = 0.5f;
|
||||||
|
|
||||||
/// @brief save-** flags
|
/// @brief save-** flags
|
||||||
struct {
|
struct {
|
||||||
bool enabled = true;
|
bool enabled = true;
|
||||||
|
|
|
||||||
|
|
@ -80,4 +80,5 @@ void Rigidbody::initialize(
|
||||||
create_sensor_callback<scripting::on_sensor_enter>(entities),
|
create_sensor_callback<scripting::on_sensor_enter>(entities),
|
||||||
create_sensor_callback<scripting::on_sensor_exit>(entities)};
|
create_sensor_callback<scripting::on_sensor_exit>(entities)};
|
||||||
}
|
}
|
||||||
|
hitbox.stepHeight = def.stepHeight;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,7 @@ struct Hitbox {
|
||||||
bool grounded = false;
|
bool grounded = false;
|
||||||
float gravityScale = 1.0f;
|
float gravityScale = 1.0f;
|
||||||
bool crouching = false;
|
bool crouching = false;
|
||||||
|
float stepHeight = 0.5f;
|
||||||
|
|
||||||
Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize);
|
Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ void PhysicsSolver::step(
|
||||||
vel += gravity * dt * gravityScale;
|
vel += gravity * dt * gravityScale;
|
||||||
if (hitbox.type == BodyType::DYNAMIC) {
|
if (hitbox.type == BodyType::DYNAMIC) {
|
||||||
colisionCalc(chunks, hitbox, vel, pos, half,
|
colisionCalc(chunks, hitbox, vel, pos, half,
|
||||||
(prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f);
|
(prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight : 0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f;
|
pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f;
|
||||||
|
|
@ -65,6 +65,7 @@ void PhysicsSolver::step(
|
||||||
}
|
}
|
||||||
if (!hitbox.grounded) {
|
if (!hitbox.grounded) {
|
||||||
pos.z = pz;
|
pos.z = pz;
|
||||||
|
vel.z = 0.0f;
|
||||||
}
|
}
|
||||||
hitbox.grounded = false;
|
hitbox.grounded = false;
|
||||||
for (int ix = 0; ix <= (half.x-E)*2/s; ix++) {
|
for (int ix = 0; ix <= (half.x-E)*2/s; ix++) {
|
||||||
|
|
@ -79,6 +80,7 @@ void PhysicsSolver::step(
|
||||||
}
|
}
|
||||||
if (!hitbox.grounded) {
|
if (!hitbox.grounded) {
|
||||||
pos.x = px;
|
pos.x = px;
|
||||||
|
vel.x = 0.0f;
|
||||||
}
|
}
|
||||||
hitbox.grounded = true;
|
hitbox.grounded = true;
|
||||||
}
|
}
|
||||||
|
|
@ -152,17 +154,16 @@ static bool calc_collision_neg(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
glm::vec3 offset(0.0f, stepHeight, 0.0f);
|
glm::vec3 offset(0.0f, stepHeight, 0.0f);
|
||||||
for (int iy = 0; iy <= ((half-offset*0.5f)[ny]-E)*2/s; iy++) {
|
for (int iy = 0; iy <= std::max<int>(1, ((half-offset*0.5f)[ny]-E)*2/s); iy++) {
|
||||||
glm::vec3 coord;
|
glm::vec3 coord;
|
||||||
coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy * s;
|
coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy * s;
|
||||||
for (int iz = 0; iz <= (half[nz]-E)*2/s; iz++){
|
for (int iz = 0; iz <= (half[nz]-E)*2/s; iz++){
|
||||||
coord[nz] = (pos[nz]-half[nz]+E) + iz * s;
|
coord[nz] = (pos[nz]-half[nz]+E) + iz * s;
|
||||||
coord[nx] = (pos[nx]-half[nx]-E);
|
coord[nx] = (pos[nx]-half[nx]-E);
|
||||||
|
|
||||||
if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z)) {
|
if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z)) {
|
||||||
vel[nx] = 0.0f;
|
vel[nx] = 0.0f;
|
||||||
float newx = std::floor(coord[nx]) + aabb->max()[nx] + half[nx] + E;
|
float newx = std::floor(coord[nx]) + half[nx] + aabb->max()[nx] + E;
|
||||||
if (std::abs(newx-pos[nx]) <= MAX_FIX) {
|
if (newx - pos[nx] <= E) {
|
||||||
pos[nx] = newx;
|
pos[nx] = newx;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -185,7 +186,7 @@ static void calc_collision_pos(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
glm::vec3 offset(0.0f, stepHeight, 0.0f);
|
glm::vec3 offset(0.0f, stepHeight, 0.0f);
|
||||||
for (int iy = 0; iy <= ((half-offset*0.5f)[ny]-E)*2/s; iy++) {
|
for (int iy = 0; iy <= std::max<int>(1, ((half-offset*0.5f)[ny]-E)*2/s); iy++) {
|
||||||
glm::vec3 coord;
|
glm::vec3 coord;
|
||||||
coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy * s;
|
coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy * s;
|
||||||
for (int iz = 0; iz <= (half[nz]-E)*2/s; iz++) {
|
for (int iz = 0; iz <= (half[nz]-E)*2/s; iz++) {
|
||||||
|
|
@ -194,7 +195,7 @@ static void calc_collision_pos(
|
||||||
if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z)) {
|
if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z)) {
|
||||||
vel[nx] = 0.0f;
|
vel[nx] = 0.0f;
|
||||||
float newx = std::floor(coord[nx]) - half[nx] + aabb->min()[nx] - E;
|
float newx = std::floor(coord[nx]) - half[nx] + aabb->min()[nx] - E;
|
||||||
if (std::abs(newx-pos[nx]) <= MAX_FIX) {
|
if (newx - pos[nx] <= E) {
|
||||||
pos[nx] = newx;
|
pos[nx] = newx;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
@ -211,7 +212,7 @@ void PhysicsSolver::colisionCalc(
|
||||||
const glm::vec3 half,
|
const glm::vec3 half,
|
||||||
float stepHeight
|
float stepHeight
|
||||||
) {
|
) {
|
||||||
// step size (smaller - more accurate, but slower)
|
// step size (smaller - more accurate, but slower) // TODO: GET RID OF THIS
|
||||||
float s = 2.0f/BLOCK_AABB_GRID;
|
float s = 2.0f/BLOCK_AABB_GRID;
|
||||||
|
|
||||||
stepHeight = calc_step_height(chunks, pos, half, stepHeight, s);
|
stepHeight = calc_step_height(chunks, pos, half, stepHeight, s);
|
||||||
|
|
@ -224,7 +225,7 @@ void PhysicsSolver::colisionCalc(
|
||||||
calc_collision_neg<2, 1, 0>(chunks, pos, vel, half, stepHeight, s);
|
calc_collision_neg<2, 1, 0>(chunks, pos, vel, half, stepHeight, s);
|
||||||
calc_collision_pos<2, 1, 0>(chunks, pos, vel, half, stepHeight, s);
|
calc_collision_pos<2, 1, 0>(chunks, pos, vel, half, stepHeight, s);
|
||||||
|
|
||||||
if (calc_collision_neg<1, 0, 2>(chunks, pos, vel, half, stepHeight, s)) {
|
if (calc_collision_neg<1, 0, 2>(chunks, pos, vel, half, 0.0f, s)) {
|
||||||
hitbox.grounded = true;
|
hitbox.grounded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue