commit 4d79cd91
Collision with rectangles
Changed files
| M | src/saland/Game.cpp before |
| M | src/saland/model/World.cpp before |
| M | src/saland/model/World.hpp before |
| M | src/saland/model/placeables.hpp before |
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp
index b873e30..d95474e 100644
--- a/src/saland/Game.cpp
+++ b/src/saland/Game.cpp
@@ -55,7 +55,6 @@ struct Game::GameImpl {
std::vector<std::shared_ptr<Placeable> > placeables;
float time = 0.0;
std::shared_ptr<Human> human;
- //b2Body* humanBody = nullptr;
std::shared_ptr<b2World> physicsBox;
float center_x = 0;
float center_y = 0;
@@ -71,11 +70,12 @@ struct Game::GameImpl {
Game::Game() {
data.reset(new Game::GameImpl());
- data->world.init();
- data->lastUpdate = SDL_GetTicks();
- data->human.reset(new Human());
b2Vec2 gravity(0.0f, 0.0f);
data->physicsBox.reset(new b2World(gravity));
+ data->world.init(data->physicsBox);
+ data->lastUpdate = SDL_GetTicks();
+ data->human.reset(new Human());
+
b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody; //this will be a dynamic body
diff --git a/src/saland/model/World.cpp b/src/saland/model/World.cpp
index 892f6ab..5a89e84 100644
--- a/src/saland/model/World.cpp
+++ b/src/saland/model/World.cpp
@@ -1,13 +1,35 @@
#include "World.hpp"
+#include "placeables.hpp"
World::World() {
}
-void World::init() {
+void World::init(std::shared_ptr<b2World>& world) {
+ this->physicsWorld = world;
std::string tsx_file = sago::GetFileContent("maps/terrain.tsx");
std::string tmx_file = sago::GetFileContent("maps/sample1.tmx");
ts = sago::tiled::string2tileset(tsx_file);
tm = sago::tiled::string2tilemap(tmx_file);
tm.tileset.alternativeSource = &ts;
+ const std::vector<sago::tiled::TileObjectGroup>& object_groups = tm.object_groups;
+ for (const auto& group : object_groups) {
+ for (const auto& item : group.objects) {
+ if (item.isEcplise) {
+ continue;
+ }
+ if (item.x > 0 && item.y > 0 && item.width > 0 && item.height > 0) {
+ b2BodyDef myBodyDef;
+ myBodyDef.type = b2_staticBody;
+ myBodyDef.position.Set(item.x/pixel2unit+item.width/pixel2unit/2.0, item.y/pixel2unit + item.height/pixel2unit/2.0 );
+ myBodyDef.linearDamping = 1.0f;
+ b2Body* body = physicsWorld->CreateBody(&myBodyDef);
+ b2PolygonShape polygonShape;
+ polygonShape.SetAsBox(item.width/pixel2unit/2.0, item.height/pixel2unit/2.0);
+ b2FixtureDef myFixtureDef;
+ myFixtureDef.shape = &polygonShape;
+ body->CreateFixture(&myFixtureDef);
+ }
+ }
+ }
}
diff --git a/src/saland/model/World.hpp b/src/saland/model/World.hpp
index c6438c5..fb406d1 100644
--- a/src/saland/model/World.hpp
+++ b/src/saland/model/World.hpp
@@ -3,14 +3,16 @@
#include "../../sagotmx/tmx_struct.h"
#include "../../sago/SagoMisc.hpp"
+#include <Box2D/Box2D.h>
class World {
public:
World();
- void init();
+ void init(std::shared_ptr<b2World>& world);
//private:
sago::tiled::TileSet ts;
sago::tiled::TileMap tm;
+ std::shared_ptr<b2World> physicsWorld;
};
#endif /* WORLD_HPP */
diff --git a/src/saland/model/placeables.hpp b/src/saland/model/placeables.hpp
index 942bd24..74e9f2d 100644
--- a/src/saland/model/placeables.hpp
+++ b/src/saland/model/placeables.hpp
@@ -4,7 +4,7 @@
#include <string>
#include <Box2D/Box2D.h>
-const float pixel2unit = 16.0f;
+const float pixel2unit = 32.0f;
class Placeable {
public: