Fixes for Physics_Move

This commit is contained in:
Edgaru089 2024-03-01 13:29:29 +08:00
parent e6bf0bfa61
commit 058865f29e

View File

@ -16,24 +16,26 @@ static inline double dabs(double x) {
void _physics_MoveX(System_Physics *sys, Entity *e, Duration deltaTime) { void _physics_MoveX(System_Physics *sys, Entity *e, Duration deltaTime) {
double delta = e->position->velocity.x * duration_Seconds(deltaTime); double delta = e->position->velocity.x * duration_Seconds(deltaTime);
Box2 mybox = physics_HitboxAbsolute(e->hitbox);
if (dabs(delta) < EPS) if (dabs(delta) < EPS)
return; return;
for (tree_Node *i = tree_FirstNode(sys->hit); for (tree_Node *i = tree_FirstNode(sys->hit);
i != NULL; i != NULL;
i = tree_Node_Next(i)) { i = tree_Node_Next(i)) {
Component_Hitbox *tohit = *((Component_Hitbox **)i->data); Component_Hitbox *tohit_comp = *((Component_Hitbox **)i->data);
if (!tohit->fixed) Box2 tohit = physics_HitboxAbsolute(tohit_comp);
if (!tohit_comp->fixed)
continue; continue;
if (box2_Intersects(tohit->box, box2_OffsetX(e->hitbox->box, delta), NULL)) { if (box2_Intersects(tohit, box2_OffsetX(mybox, delta), NULL)) {
if (delta > 0) { if (delta > 0) {
// Moves right, hits left edge // Moves right, hits left edge
double maxdelta = tohit->box.lefttop.x - e->hitbox->box.lefttop.x - e->hitbox->box.size.x; double maxdelta = tohit.lefttop.x - mybox.lefttop.x - mybox.size.x;
delta = maxdelta - EPS; delta = maxdelta - EPS;
} else { } else {
// Moves left, hits right edge // Moves left, hits right edge
double maxdelta = tohit->box.lefttop.x - e->hitbox->box.lefttop.x + tohit->box.size.x; double maxdelta = tohit.lefttop.x - mybox.lefttop.x + tohit.size.x;
delta = maxdelta + EPS; delta = maxdelta + EPS;
} }
} }
@ -48,24 +50,26 @@ void _physics_MoveX(System_Physics *sys, Entity *e, Duration deltaTime) {
void _physics_MoveY(System_Physics *sys, Entity *e, Duration deltaTime) { void _physics_MoveY(System_Physics *sys, Entity *e, Duration deltaTime) {
double delta = e->position->velocity.y * duration_Seconds(deltaTime); double delta = e->position->velocity.y * duration_Seconds(deltaTime);
Box2 mybox = physics_HitboxAbsolute(e->hitbox);
if (dabs(delta) < EPS) if (dabs(delta) < EPS)
return; return;
for (tree_Node *i = tree_FirstNode(sys->hit); for (tree_Node *i = tree_FirstNode(sys->hit);
i != NULL; i != NULL;
i = tree_Node_Next(i)) { i = tree_Node_Next(i)) {
Component_Hitbox *tohit = *((Component_Hitbox **)i->data); Component_Hitbox *tohit_comp = *((Component_Hitbox **)i->data);
if (!tohit->fixed) Box2 tohit = physics_HitboxAbsolute(tohit_comp);
if (!tohit_comp->fixed)
continue; continue;
if (box2_Intersects(tohit->box, box2_OffsetY(e->hitbox->box, delta), NULL)) { if (box2_Intersects(tohit, box2_OffsetY(mybox, delta), NULL)) {
if (delta > 0) { if (delta > 0) {
// Moves down, hits top edge // Moves down, hits top edge
double maxdelta = tohit->box.lefttop.y - e->hitbox->box.lefttop.y - e->hitbox->box.size.y; double maxdelta = tohit.lefttop.y - mybox.lefttop.y - mybox.size.y;
delta = maxdelta - EPS; delta = maxdelta - EPS;
} else { } else {
// Moves up, hits bottom edge // Moves up, hits bottom edge
double maxdelta = tohit->box.lefttop.y - e->hitbox->box.lefttop.y + tohit->box.size.y; double maxdelta = tohit.lefttop.y - mybox.lefttop.y + tohit.size.y;
delta = maxdelta + EPS; delta = maxdelta + EPS;
} }
} }