commit b38e2d16
Now uses the same body for all blocking tiles
Changed files
| M | src/saland/model/World.cpp before |
diff --git a/src/saland/model/World.cpp b/src/saland/model/World.cpp
index 2002918..6f056e1 100644
--- a/src/saland/model/World.cpp
+++ b/src/saland/model/World.cpp
@@ -27,10 +27,19 @@ https://github.com/sago007/saland
World::World() {
}
-
+/**
+ * Adds a rext based on pixels to a body.
+ * @param body The body to add to
+ * @param x X coordiante for the top left corner in pixels
+ * @param y Y coordinate for the top left corner in pixels
+ * @param width Width of the rectangle in pixels
+ * @param hight Hight of the rectangle in pixels
+ */
static void AddRectToBody(b2Body* body, float x, float y, float width, float hight) {
+ b2Vec2 center;
+ center.Set(x/pixel2unit+width/pixel2unit/2.0, y/pixel2unit + hight/pixel2unit/2.0);
b2PolygonShape polygonShape;
- polygonShape.SetAsBox(width/pixel2unit/2.0, hight/pixel2unit/2.0);
+ polygonShape.SetAsBox(width/pixel2unit/2.0, hight/pixel2unit/2.0, center, 0.0f);
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &polygonShape;
body->CreateFixture(&myFixtureDef);
@@ -40,7 +49,7 @@ static void AddRectToBody(b2Body* body, float x, float y, float width, float hig
* Adds a static body to the world. Returns a pointer to the body
* @param world The world to use for the body
* @param x X coordiante for the top left corner in pixels
- * @param y Y coordinate for the top left corner in pixels
+ * @param y Y coordinate for the top left corner in pixels
* @param width Width of the rectangle in pixels
* @param hight Hight of the rectangle in pixels
* @return A pointer to the body. Valid until the world is destroyed or destroyBody is called.
@@ -48,21 +57,31 @@ static void AddRectToBody(b2Body* body, float x, float y, float width, float hig
static b2Body* AddStaticRect(b2World* world, float x, float y, float width, float hight) {
b2BodyDef myBodyDef;
myBodyDef.type = b2_staticBody;
- myBodyDef.position.Set(x/pixel2unit+width/pixel2unit/2.0, y/pixel2unit + hight/pixel2unit/2.0 );
+ myBodyDef.position.Set(0,0);
myBodyDef.linearDamping = 1.0f;
b2Body* body = world->CreateBody(&myBodyDef);
AddRectToBody(body, x, y, width, hight);
return body;
}
+static b2Body* AddStaticBody(b2World* world) {
+ b2BodyDef myBodyDef;
+ myBodyDef.type = b2_staticBody;
+ myBodyDef.position.Set(0,0);
+ myBodyDef.linearDamping = 1.0f;
+ b2Body* body = world->CreateBody(&myBodyDef);
+ return body;
+}
+
static void AddStaticTilesToWorld(b2World* world, const sago::tiled::TileMap& tm, const sago::tiled::TileLayer& layer) {
+ b2Body* body = AddStaticBody(world);
for (int i = 0; i < tm.height; ++i) {
for (int j = 0; j < tm.width; ++j) {
uint32_t gid = sago::tiled::getTileFromLayer(tm, layer, i, j);
if (gid == 0) {
continue;
}
- AddStaticRect(world, i*32.0f, j*32.0f, 32.0f, 32.0f);
+ AddRectToBody(body, i*32.0f, j*32.0f, 32.0f, 32.0f);
}
}
}