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

View File

@ -66,7 +66,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
if (def.rotatable) {
for (uint i = 0; i < BlockRotProfile::MAX_COUNT; i++) {
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.rt.hitboxes[i].push_back(aabb);
}

View File

@ -353,7 +353,9 @@ void ContentLoader::load(ContentBuilder& builder) {
std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
if (colon != std::string::npos) name[colon] = '/';
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);
stats.totalBlocks++;
if (!def.hidden) {

View File

@ -1,8 +1,8 @@
#include "Hitbox.hpp"
Hitbox::Hitbox(glm::vec3 position, glm::vec3 halfsize)
: position(position),
halfsize(halfsize),
velocity(0.0f,0.0f,0.0f),
linear_damping(0.1f) {
}
: position(position),
halfsize(halfsize),
velocity(0.0f,0.0f,0.0f),
linear_damping(0.1f)
{}

View File

@ -14,4 +14,4 @@ public:
Hitbox(glm::vec3 position, glm::vec3 halfsize);
};
#endif /* PHYSICS_HITBOX_HPP_ */
#endif // PHYSICS_HITBOX_HPP_

View File

@ -6,21 +6,21 @@
#include "../voxels/Chunks.hpp"
#include "../voxels/voxel.hpp"
const double E = 0.03;
const double MAX_FIX = 0.1;
const float E = 0.03f;
const float MAX_FIX = 0.1f;
PhysicsSolver::PhysicsSolver(glm::vec3 gravity) : gravity(gravity) {
}
void PhysicsSolver::step(
Chunks* chunks,
Hitbox* hitbox,
float delta,
uint substeps,
bool shifting,
float gravityScale,
bool collisions)
{
Chunks* chunks,
Hitbox* hitbox,
float delta,
uint substeps,
bool shifting,
float gravityScale,
bool collisions
) {
float dt = delta / float(substeps);
float linear_damping = hitbox->linear_damping;
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){
float x = (pos.x-half.x-E);
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;
if (glm::abs(newx-pos.x) <= MAX_FIX) {
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){
float x = (pos.x+half.x+E);
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;
if (glm::abs(newx-pos.x) <= MAX_FIX) {
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){
float z = (pos.z-half.z-E);
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;
if (glm::abs(newz-pos.z) <= MAX_FIX) {
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){
float z = (pos.z+half.z+E);
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;
if (glm::abs(newz-pos.z) <= MAX_FIX) {
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){
float y = (pos.y-half.y-E);
if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.y *= 0.0f;
vel.y = 0.0f;
float newy = floor(y) + aabb->max().y + half.y;
if (glm::abs(newy-pos.y) <= MAX_FIX) {
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){
float y = (pos.y-half.y+E);
if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.y *= 0.0f;
vel.y = 0.0f;
float newy = floor(y) + aabb->max().y + half.y;
if (glm::abs(newy-pos.y) <= MAX_FIX+stepHeight) {
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){
float y = (pos.y+half.y+E);
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;
if (glm::abs(newy-pos.y) <= MAX_FIX) {
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& half = hitbox->halfsize;
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) &&
y >= floor(pos.y-half.y) && y <= floor(pos.y+half.y);
z >= floor(pos.z-half.z) && z <= floor(pos.z+half.z) &&
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) {
@ -223,15 +223,15 @@ bool PhysicsSolver::isBlockInside(int x, int y, int z, Block* def, blockstate_t
voxel v {};
v.states = states;
const auto& boxes = def->rotatable
? def->rt.hitboxes[v.rotation()]
: def->hitboxes;
? def->rt.hitboxes[v.rotation()]
: def->hitboxes;
for (const auto& block_hitbox : boxes) {
glm::vec3 min = block_hitbox.min();
glm::vec3 max = block_hitbox.max();
// 0.00001 - inaccuracy
if (min.x < pos.x+half.x-x-0.00001 && max.x > pos.x-half.x-x+0.00001 &&
min.z < pos.z+half.z-z-0.00001 && max.z > pos.z-half.z-z+0.00001 &&
min.y < pos.y+half.y-y-0.00001 && max.y > pos.y-half.y-y+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.00001f && max.z > pos.z-half.z-z+0.00001f &&
min.y < pos.y+half.y-y-0.00001f && max.y > pos.y-half.y-y+0.00001f)
return true;
}
return false;

View File

@ -1,10 +1,11 @@
#ifndef PHYSICS_PHYSICSSOLVER_HPP_
#define PHYSICS_PHYSICSSOLVER_HPP_
#include <glm/glm.hpp>
#include "../typedefs.hpp"
#include "../voxels/Block.hpp"
#include <glm/glm.hpp>
class Block;
class Hitbox;
class Chunks;
@ -12,22 +13,25 @@ class PhysicsSolver {
glm::vec3 gravity;
public:
PhysicsSolver(glm::vec3 gravity);
void step(Chunks* chunks,
Hitbox* hitbox,
float delta,
uint substeps,
bool shifting,
float gravityScale,
bool collisions);
void step(
Chunks* chunks,
Hitbox* hitbox,
float delta,
uint substeps,
bool shifting,
float gravityScale,
bool collisions
);
void colisionCalc(
Chunks* chunks,
Hitbox* hitbox,
glm::vec3& vel,
glm::vec3& pos,
const glm::vec3 half,
float stepHeight);
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_t states, Hitbox* hitbox);
};
#endif /* PHYSICS_PHYSICSSOLVER_HPP_ */
#endif // PHYSICS_PHYSICSSOLVER_HPP_