This commit is contained in:
MihailRis
2024-05-19 08:40:43 +03:00
parent c5f663b7cb
commit 47c6e35a0b
6 changed files with 54 additions and 48 deletions
+1 -1
View File
@@ -66,7 +66,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
if (def.rotatable) { if (def.rotatable) {
for (uint i = 0; i < BlockRotProfile::MAX_COUNT; i++) { for (uint i = 0; i < BlockRotProfile::MAX_COUNT; i++) {
def.rt.hitboxes[i].reserve(def.hitboxes.size()); def.rt.hitboxes[i].reserve(def.hitboxes.size());
for (AABB& aabb : def.hitboxes) { for (AABB aabb : def.hitboxes) {
def.rotations.variants[i].transform(aabb); def.rotations.variants[i].transform(aabb);
def.rt.hitboxes[i].push_back(aabb); def.rt.hitboxes[i].push_back(aabb);
} }
+3 -1
View File
@@ -353,7 +353,9 @@ void ContentLoader::load(ContentBuilder& builder) {
std::string full = colon == std::string::npos ? pack->id + ":" + name : name; std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
if (colon != std::string::npos) name[colon] = '/'; if (colon != std::string::npos) name[colon] = '/';
auto& def = builder.createBlock(full); auto& def = builder.createBlock(full);
if (colon != std::string::npos) def.scriptName = name.substr(0, colon) + '/' + def.scriptName; if (colon != std::string::npos) {
def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
}
loadBlock(def, full, name); loadBlock(def, full, name);
stats.totalBlocks++; stats.totalBlocks++;
if (!def.hidden) { if (!def.hidden) {
+5 -5
View File
@@ -1,8 +1,8 @@
#include "Hitbox.hpp" #include "Hitbox.hpp"
Hitbox::Hitbox(glm::vec3 position, glm::vec3 halfsize) Hitbox::Hitbox(glm::vec3 position, glm::vec3 halfsize)
: position(position), : position(position),
halfsize(halfsize), halfsize(halfsize),
velocity(0.0f,0.0f,0.0f), velocity(0.0f,0.0f,0.0f),
linear_damping(0.1f) { linear_damping(0.1f)
} {}
+1 -1
View File
@@ -14,4 +14,4 @@ public:
Hitbox(glm::vec3 position, glm::vec3 halfsize); Hitbox(glm::vec3 position, glm::vec3 halfsize);
}; };
#endif /* PHYSICS_HITBOX_HPP_ */ #endif // PHYSICS_HITBOX_HPP_
+24 -24
View File
@@ -6,21 +6,21 @@
#include "../voxels/Chunks.hpp" #include "../voxels/Chunks.hpp"
#include "../voxels/voxel.hpp" #include "../voxels/voxel.hpp"
const double E = 0.03; const float E = 0.03f;
const double MAX_FIX = 0.1; const float MAX_FIX = 0.1f;
PhysicsSolver::PhysicsSolver(glm::vec3 gravity) : gravity(gravity) { PhysicsSolver::PhysicsSolver(glm::vec3 gravity) : gravity(gravity) {
} }
void PhysicsSolver::step( void PhysicsSolver::step(
Chunks* chunks, Chunks* chunks,
Hitbox* hitbox, Hitbox* hitbox,
float delta, float delta,
uint substeps, uint substeps,
bool shifting, bool shifting,
float gravityScale, float gravityScale,
bool collisions) bool collisions
{ ) {
float dt = delta / float(substeps); float dt = delta / float(substeps);
float linear_damping = hitbox->linear_damping; float linear_damping = hitbox->linear_damping;
float s = 2.0f/BLOCK_AABB_GRID; float s = 2.0f/BLOCK_AABB_GRID;
@@ -103,7 +103,7 @@ void PhysicsSolver::colisionCalc(
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
float x = (pos.x-half.x-E); float x = (pos.x-half.x-E);
if ((aabb = chunks->isObstacleAt(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.x *= 0.0f; vel.x = 0.0f;
float newx = floor(x) + aabb->max().x + half.x + E; float newx = floor(x) + aabb->max().x + half.x + E;
if (glm::abs(newx-pos.x) <= MAX_FIX) { if (glm::abs(newx-pos.x) <= MAX_FIX) {
pos.x = newx; pos.x = newx;
@@ -118,7 +118,7 @@ void PhysicsSolver::colisionCalc(
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
float x = (pos.x+half.x+E); float x = (pos.x+half.x+E);
if ((aabb = chunks->isObstacleAt(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.x *= 0.0f; vel.x = 0.0f;
float newx = floor(x) - half.x + aabb->min().x - E; float newx = floor(x) - half.x + aabb->min().x - E;
if (glm::abs(newx-pos.x) <= MAX_FIX) { if (glm::abs(newx-pos.x) <= MAX_FIX) {
pos.x = newx; pos.x = newx;
@@ -134,7 +134,7 @@ void PhysicsSolver::colisionCalc(
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){ for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
float z = (pos.z-half.z-E); float z = (pos.z-half.z-E);
if ((aabb = chunks->isObstacleAt(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.z *= 0.0f; vel.z = 0.0f;
float newz = floor(z) + aabb->max().z + half.z + E; float newz = floor(z) + aabb->max().z + half.z + E;
if (glm::abs(newz-pos.z) <= MAX_FIX) { if (glm::abs(newz-pos.z) <= MAX_FIX) {
pos.z = newz; pos.z = newz;
@@ -150,7 +150,7 @@ void PhysicsSolver::colisionCalc(
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){ for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
float z = (pos.z+half.z+E); float z = (pos.z+half.z+E);
if ((aabb = chunks->isObstacleAt(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.z *= 0.0f; vel.z = 0.0f;
float newz = floor(z) - half.z + aabb->min().z - E; float newz = floor(z) - half.z + aabb->min().z - E;
if (glm::abs(newz-pos.z) <= MAX_FIX) { if (glm::abs(newz-pos.z) <= MAX_FIX) {
pos.z = newz; pos.z = newz;
@@ -166,7 +166,7 @@ void PhysicsSolver::colisionCalc(
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
float y = (pos.y-half.y-E); float y = (pos.y-half.y-E);
if ((aabb = chunks->isObstacleAt(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.y *= 0.0f; vel.y = 0.0f;
float newy = floor(y) + aabb->max().y + half.y; float newy = floor(y) + aabb->max().y + half.y;
if (glm::abs(newy-pos.y) <= MAX_FIX) { if (glm::abs(newy-pos.y) <= MAX_FIX) {
pos.y = newy; pos.y = newy;
@@ -182,7 +182,7 @@ void PhysicsSolver::colisionCalc(
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
float y = (pos.y-half.y+E); float y = (pos.y-half.y+E);
if ((aabb = chunks->isObstacleAt(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.y *= 0.0f; vel.y = 0.0f;
float newy = floor(y) + aabb->max().y + half.y; float newy = floor(y) + aabb->max().y + half.y;
if (glm::abs(newy-pos.y) <= MAX_FIX+stepHeight) { if (glm::abs(newy-pos.y) <= MAX_FIX+stepHeight) {
pos.y = newy; pos.y = newy;
@@ -197,7 +197,7 @@ void PhysicsSolver::colisionCalc(
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
float y = (pos.y+half.y+E); float y = (pos.y+half.y+E);
if ((aabb = chunks->isObstacleAt(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.y *= 0.0f; vel.y = 0.0f;
float newy = floor(y) - half.y + aabb->min().y - E; float newy = floor(y) - half.y + aabb->min().y - E;
if (glm::abs(newy-pos.y) <= MAX_FIX) { if (glm::abs(newy-pos.y) <= MAX_FIX) {
pos.y = newy; pos.y = newy;
@@ -213,8 +213,8 @@ bool PhysicsSolver::isBlockInside(int x, int y, int z, Hitbox* hitbox) {
const glm::vec3& pos = hitbox->position; const glm::vec3& pos = hitbox->position;
const glm::vec3& half = hitbox->halfsize; const glm::vec3& half = hitbox->halfsize;
return x >= floor(pos.x-half.x) && x <= floor(pos.x+half.x) && return x >= floor(pos.x-half.x) && x <= floor(pos.x+half.x) &&
z >= floor(pos.z-half.z) && z <= floor(pos.z+half.z) && z >= floor(pos.z-half.z) && z <= floor(pos.z+half.z) &&
y >= floor(pos.y-half.y) && y <= floor(pos.y+half.y); y >= floor(pos.y-half.y) && y <= floor(pos.y+half.y);
} }
bool PhysicsSolver::isBlockInside(int x, int y, int z, Block* def, blockstate_t states, Hitbox* hitbox) { bool PhysicsSolver::isBlockInside(int x, int y, int z, Block* def, blockstate_t states, Hitbox* hitbox) {
@@ -223,15 +223,15 @@ bool PhysicsSolver::isBlockInside(int x, int y, int z, Block* def, blockstate_t
voxel v {}; voxel v {};
v.states = states; v.states = states;
const auto& boxes = def->rotatable const auto& boxes = def->rotatable
? def->rt.hitboxes[v.rotation()] ? def->rt.hitboxes[v.rotation()]
: def->hitboxes; : def->hitboxes;
for (const auto& block_hitbox : boxes) { for (const auto& block_hitbox : boxes) {
glm::vec3 min = block_hitbox.min(); glm::vec3 min = block_hitbox.min();
glm::vec3 max = block_hitbox.max(); glm::vec3 max = block_hitbox.max();
// 0.00001 - inaccuracy // 0.00001 - inaccuracy
if (min.x < pos.x+half.x-x-0.00001 && max.x > pos.x-half.x-x+0.00001 && if (min.x < pos.x+half.x-x-0.00001f && max.x > pos.x-half.x-x+0.00001f &&
min.z < pos.z+half.z-z-0.00001 && max.z > pos.z-half.z-z+0.00001 && min.z < pos.z+half.z-z-0.00001f && max.z > pos.z-half.z-z+0.00001f &&
min.y < pos.y+half.y-y-0.00001 && max.y > pos.y-half.y-y+0.00001) min.y < pos.y+half.y-y-0.00001f && max.y > pos.y-half.y-y+0.00001f)
return true; return true;
} }
return false; return false;
+20 -16
View File
@@ -1,10 +1,11 @@
#ifndef PHYSICS_PHYSICSSOLVER_HPP_ #ifndef PHYSICS_PHYSICSSOLVER_HPP_
#define PHYSICS_PHYSICSSOLVER_HPP_ #define PHYSICS_PHYSICSSOLVER_HPP_
#include <glm/glm.hpp>
#include "../typedefs.hpp" #include "../typedefs.hpp"
#include "../voxels/Block.hpp"
#include <glm/glm.hpp>
class Block;
class Hitbox; class Hitbox;
class Chunks; class Chunks;
@@ -12,22 +13,25 @@ class PhysicsSolver {
glm::vec3 gravity; glm::vec3 gravity;
public: public:
PhysicsSolver(glm::vec3 gravity); PhysicsSolver(glm::vec3 gravity);
void step(Chunks* chunks, void step(
Hitbox* hitbox, Chunks* chunks,
float delta, Hitbox* hitbox,
uint substeps, float delta,
bool shifting, uint substeps,
float gravityScale, bool shifting,
bool collisions); float gravityScale,
bool collisions
);
void colisionCalc( void colisionCalc(
Chunks* chunks, Chunks* chunks,
Hitbox* hitbox, Hitbox* hitbox,
glm::vec3& vel, glm::vec3& vel,
glm::vec3& pos, glm::vec3& pos,
const glm::vec3 half, const glm::vec3 half,
float stepHeight); float stepHeight
);
bool isBlockInside(int x, int y, int z, Hitbox* hitbox); bool isBlockInside(int x, int y, int z, Hitbox* hitbox);
bool isBlockInside(int x, int y, int z, Block* def, blockstate_t states, Hitbox* hitbox); bool isBlockInside(int x, int y, int z, Block* def, blockstate_t states, Hitbox* hitbox);
}; };
#endif /* PHYSICS_PHYSICSSOLVER_HPP_ */ #endif // PHYSICS_PHYSICSSOLVER_HPP_