canvas.Fill();
canvas.SetColor(outlineColor);
- canvas.Grid(cam.ToScreen(Vector<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector<float>(1, 1)));
+ canvas.Grid(cam.ToScreen(Vector<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(world.TileSize()));
}
void Application::RenderWorld() {
canvas.SetColor(vboxColor);
canvas.Line(
cam.ToScreen(Vector<float>(e.vbox.Left(), e.vbox.Top())),
- cam.ToScreen(Vector<float>(e.vbox.Right(), e.vbox.Top()))
+ cam.ToScreen(Vector<float>(e.vbox.Right(), e.vbox.Top())) - Vector<int>(1, 0)
);
canvas.Line(
- cam.ToScreen(Vector<float>(e.vbox.Left(), e.vbox.Bottom())),
- cam.ToScreen(Vector<float>(e.vbox.Right(), e.vbox.Bottom()))
+ cam.ToScreen(Vector<float>(e.vbox.Left(), e.vbox.Bottom())) - Vector<int>(0, 1),
+ cam.ToScreen(Vector<float>(e.vbox.Right(), e.vbox.Bottom())) - Vector<int>(1, 1)
);
canvas.SetColor(hboxColor);
canvas.Line(
cam.ToScreen(Vector<float>(e.hbox.Left(), e.hbox.Top())),
- cam.ToScreen(Vector<float>(e.hbox.Left(), e.hbox.Bottom()))
+ cam.ToScreen(Vector<float>(e.hbox.Left(), e.hbox.Bottom())) - Vector<int>(0, 1)
);
canvas.Line(
- cam.ToScreen(Vector<float>(e.hbox.Right(), e.hbox.Top())),
- cam.ToScreen(Vector<float>(e.hbox.Right(), e.hbox.Bottom()))
+ cam.ToScreen(Vector<float>(e.hbox.Right(), e.hbox.Top())) - Vector<int>(1, 0),
+ cam.ToScreen(Vector<float>(e.hbox.Right(), e.hbox.Bottom())) - Vector<int>(1, 1)
);
}
}
/// transform v from world coords to screen coords
Vector<int> ToScreen(Vector<float> v) const {
- return ToScale(v - *target) + offset;
+ return Vector<int>(round(ToScale(v - *target))) + offset;
}
/// scale v from world to screen
- Vector<int> ToScale(Vector<float> v) const {
- return Vector<int>(v * scale);
+ Vector<float> ToScale(Vector<float> v) const {
+ return Vector<float>(v * scale);
}
/// transform v from screen coords to world coords
return Vector<Scalar>(std::abs(v.x), std::abs(v.y));
}
template<class Scalar>
+inline Vector<Scalar> round(Vector<Scalar> v) {
+ return Vector<Scalar>(std::round(v.x), std::round(v.y));
+}
+template<>
+inline Vector<float> round(Vector<float> v) {
+ return Vector<float>(std::roundf(v.x), std::roundf(v.y));
+}
+template<class Scalar>
constexpr Vector<Scalar> min(Vector<Scalar> lhs, Vector<Scalar> rhs) {
return Vector<Scalar>(std::min(lhs.x, rhs.x), std::min(lhs.y, rhs.y));
}
Tileset tiles(
canv.LoadTexture(sdl.GetBasePath() + "../../data/test-tile.png"),
- Vector<int>(32, 32)
+ Vector<int>(16, 16)
);
World world(Vector<int>(10, 10));
public:
constexpr AABB() { }
constexpr AABB(Vector<float> pos, Vector<float> size)
- : lt(pos), rb(pos + size) { }
+ : size(size), lt(pos), rb(pos + size) { }
constexpr AABB(float x, float y, float w, float h)
- : lt(x, y), rb(x + w, y + h) { }
+ : size(w, h), lt(x, y), rb(x + w, y + h) { }
public:
float Left() const { return lt.x; }
Vector<float> Position() const { return lt; }
Vector<float> Center() const { return lt + HalfSize(); }
Vector<float> HalfSize() const { return Size() / 2.0f; }
- Vector<float> Size() const { return rb - lt; }
+ Vector<float> Size() const { return size; }
public:
- void Move(Vector<float> delta) { lt += delta; rb += delta; }
+ void Move(Vector<float> delta) { lt += delta; rb = lt + size; }
void Resize(Vector<float> size) { *this = AABB(lt, size); }
bool Intersects(const AABB &other) const;
bool Intersects(const AABB &other, Collision &) const;
private:
+ Vector<float> size;
Vector<float> lt;
Vector<float> rb;
}
void Entity::Move(Vector<float> delta) {
+ pos += delta;
bounds.Move(delta);
vbox.Move(delta);
hbox.Move(delta);
void Move(Vector<float> delta);
public:
+ Vector<float> pos;
+ Vector<float> vel;
+ Vector<float> acc;
+
AABB bounds;
AABB vbox;
AABB hbox;
- Vector<float> vel;
- Vector<float> acc;
float mass = 1.0f;
float elast = 0.75f;
void World::Update(float dt) {
for (Entity &e : entities) {
- e.Update(dt, gravity, terminal);
- e.onGround = false;
+ if (e.onGround) {
+ e.Update(dt, Vector<float>(), terminal);
+ e.onGround = false;
+ } else {
+ e.Update(dt, gravity, terminal);
+ }
BoundsCollision(e, dt);
TileCollision(e, dt);
}
-const AABB &World::TileShapeAt(Vector<int> pos) const {
- tileShape = AABB(pos, Vector<float>(1, 1));
- return tileShape;
-}
-
-
Entity &World::AddEntity(const Entity &e) {
entities.emplace_back(e);
return entities.back();
public:
Vector<int> Size() const { return size; }
+ Vector<int> TileSize() const { return Vector<int>(1, 1); }
public:
void Update(float dt);
Tile &TileAt(Vector<int> pos) { return tiles[Index(pos)]; }
const Tile &TileAt(Vector<int> pos) const { return tiles[Index(pos)]; }
void SetTile(Vector<int> pos, const Tile &t) { tiles[Index(pos)] = t; }
- const AABB &TileShapeAt(Vector<int> pos) const;
const std::list<Entity> &Entities() const { return entities; }
Entity &AddEntity(const Entity &);