Sfoglia il codice sorgente

better collision

opengl-renderer-broken
Martin Dørum 4 anni fa
parent
commit
3e07ceb91f

+ 1
- 1
core.mod/src/entities/EntPlayer.h Vedi File

@@ -20,7 +20,7 @@ public:

private:
static constexpr float FORCE = 3000;
static constexpr float JUMP_FORCE = 7;
static constexpr float JUMP_FORCE = 9;
static constexpr float MASS = 80;
static constexpr Swan::Vec2 FRICTION = Swan::Vec2(400, 0);
static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.6, 1.9);

+ 1
- 1
libswan/include/swan/Body.h Vedi File

@@ -13,7 +13,7 @@ public:
pos_(pos), size_(size), mass_(mass) {};

void friction(Vec2 coef);
void gravity(Vec2 g = Vec2(0, 9.81));
void gravity(Vec2 g = Vec2(0, 20));
void collide(WorldPlane &plane);

void outline(Win &win);

+ 3
- 0
libswan/include/swan/WorldPlane.h Vedi File

@@ -35,6 +35,8 @@ public:
void update(float dt);
void tick();

void debugBox(TilePos pos);

ID id_;
World *world_;
std::shared_ptr<WorldGen> gen_;
@@ -42,6 +44,7 @@ public:
private:
std::map<std::pair<int, int>, std::unique_ptr<Chunk>> chunks_;
std::vector<std::unique_ptr<Entity>> entities_;
std::vector<TilePos> debug_boxes_;
};

}

+ 20
- 18
libswan/src/Body.cc Vedi File

@@ -13,13 +13,27 @@ void Body::gravity(Vec2 g) {
}

void Body::collide(WorldPlane &plane) {
int px = (int)pos_.x_;
if (pos_.x_ < 0) px -= 1;
int startx = px;
int endx = (int)ceil(px + size_.x_);
int startx, endx, y;

startx = (int)floor(pos_.x_);
endx = (int)floor(pos_.x_ + size_.x_);
y = (int)ceil(pos_.y_ + size_.y_ - 1.3);
for (int x = startx; x <= endx; ++x) {
Tile &wall = plane.getTile(TilePos(x, y));
if (x == startx && vel_.x_ < 0 && wall.is_solid_) {
vel_.x_ = 0;
pos_.x_ = startx + 1.01;
startx = (int)floor(pos_.x_);
} else if (x == endx && vel_.x_ > 0 && wall.is_solid_) {
vel_.x_ = 0;
pos_.x_ = endx - size_.x_ - 0.01;
endx = (int)floor(pos_.x_ + size_.x_);
}
plane.debugBox(TilePos(x, y));
}

on_ground_ = false;
int y = (int)ceil(pos_.y_ + size_.y_ - 1);
y = (int)ceil(pos_.y_ + size_.y_ - 1);
for (int x = startx; x <= endx; ++x) {
Tile &ground = plane.getTile(TilePos(x, y));
if (ground.is_solid_ && vel_.y_ > 0) {
@@ -27,19 +41,7 @@ void Body::collide(WorldPlane &plane) {
vel_.y_ = 0;
on_ground_ = true;
}

Tile &wall = plane.getTile(TilePos(x, y - 1));
if (x == startx && vel_.x_ < 0) {
if (wall.is_solid_) {
vel_.x_ = 0;
pos_.x_ = startx + 1;
}
} else if (x == endx && vel_.x_ > 0) {
if (wall.is_solid_) {
vel_.x_ = 0;
pos_.x_ = endx - size_.x_;
}
}
plane.debugBox(TilePos(x, y));
}
}


+ 16
- 0
libswan/src/WorldPlane.cc Vedi File

@@ -67,9 +67,21 @@ void WorldPlane::draw(Win &win) {
ch.second->draw(win);
for (auto &ent: entities_)
ent->draw(win);

if (debug_boxes_.size() > 0) {
sf::RectangleShape rect(Vec2(TILE_SIZE, TILE_SIZE));
rect.setFillColor(sf::Color(60, 70, 200, 100));
rect.setOutlineThickness(1);
rect.setOutlineColor(sf::Color(50, 65, 170, 200));
for (auto &pos: debug_boxes_) {
win.setPos(pos);
win.draw(rect);
}
}
}

void WorldPlane::update(float dt) {
debug_boxes_.clear();
for (auto &ent: entities_)
ent->update(*this, dt);
}
@@ -79,4 +91,8 @@ void WorldPlane::tick() {
ent->tick();
}

void WorldPlane::debugBox(TilePos pos) {
debug_boxes_.push_back(pos);
}

}

Loading…
Annulla
Salva