Browse Source

better collision

feature/replace-renderer
Martin Dørum 3 years ago
parent
commit
8b9076547b
1 changed files with 5 additions and 5 deletions
  1. 5
    5
      libswan/src/traits/PhysicsTrait.cc

+ 5
- 5
libswan/src/traits/PhysicsTrait.cc View File



namespace Swan { namespace Swan {


static float epsilon = 0.0001;
static float epsilon = 0.05;


static void collideX( static void collideX(
PhysicsTrait::Physics &phys, BodyTrait::Body &body, PhysicsTrait::Physics &phys, BodyTrait::Body &body,
bool collided = false; bool collided = false;


for (int y = (int)floor(body.top() + epsilon); y <= (int)floor(body.bottom() - epsilon); ++y) { for (int y = (int)floor(body.top() + epsilon); y <= (int)floor(body.bottom() - epsilon); ++y) {
int lx = (int)floor(body.left() + epsilon);
int lx = (int)floor(body.left());
Tile &left = plane.getTile({lx, y}); Tile &left = plane.getTile({lx, y});
if (left.isSolid) { if (left.isSolid) {
body.pos.x = (float)lx + 1.0; body.pos.x = (float)lx + 1.0;
break; break;
} }


int rx = (int)floor(body.right() - epsilon);
int rx = (int)floor(body.right());
Tile &right = plane.getTile({rx, y}); Tile &right = plane.getTile({rx, y});
if (right.isSolid) { if (right.isSolid) {
body.pos.x = (float)rx - body.size.x; body.pos.x = (float)rx - body.size.x;
phys.onGround = false; phys.onGround = false;


for (int x = (int)floor(body.left() + epsilon); x <= (int)floor(body.right() - epsilon); ++x) { for (int x = (int)floor(body.left() + epsilon); x <= (int)floor(body.right() - epsilon); ++x) {
int ty = (int)floor(body.top() + epsilon);
int ty = (int)floor(body.top());
Tile &top = plane.getTile({x, ty}); Tile &top = plane.getTile({x, ty});
if (top.isSolid) { if (top.isSolid) {
body.pos.y = (float)ty + 1.0; body.pos.y = (float)ty + 1.0;
break; break;
} }


int by = (int)floor(body.bottom() - epsilon);
int by = (int)floor(body.bottom());
Tile &bottom = plane.getTile({x, by}); Tile &bottom = plane.getTile({x, by});
if (bottom.isSolid) { if (bottom.isSolid) {
body.pos.y = (float)by - body.size.y; body.pos.y = (float)by - body.size.y;

Loading…
Cancel
Save