Added option to use circle as collision model.

This commit is contained in:
Felix Ableitner 2012-09-13 14:34:36 +02:00
parent 188fa2907b
commit 2f8e91c3b7
2 changed files with 36 additions and 13 deletions

View file

@ -33,11 +33,21 @@ Physical::Physical(const PhysicalData& data) :
mBody = data.world.CreateBody(&bodyDef); mBody = data.world.CreateBody(&bodyDef);
b2PolygonShape boxShape; b2Shape* shape;
boxShape.SetAsBox(pixelToMeter(data.size.x) / 2, pixelToMeter(data.size.y) / 2); if (data.circle) {
assert(data.size.x == data.size.y);
shape = new b2CircleShape;
shape->m_radius = pixelToMeter(data.size.x) / 2;
}
else {
b2PolygonShape* box = new b2PolygonShape;
box->SetAsBox(pixelToMeter(data.size.x) / 2,
pixelToMeter(data.size.y) / 2);
shape = dynamic_cast<b2Shape*>(box);
}
b2FixtureDef fixtureDef; b2FixtureDef fixtureDef;
fixtureDef.shape = &boxShape; fixtureDef.shape = shape;
fixtureDef.density = 1.0f; fixtureDef.density = 1.0f;
fixtureDef.filter.categoryBits = data.category; fixtureDef.filter.categoryBits = data.category;
fixtureDef.filter.maskBits = ~data.maskExclude; fixtureDef.filter.maskBits = ~data.maskExclude;
@ -45,6 +55,8 @@ Physical::Physical(const PhysicalData& data) :
fixtureDef.density = (data.bullet) ? 0 : 10000; fixtureDef.density = (data.bullet) ? 0 : 10000;
mBody->CreateFixture(&fixtureDef); mBody->CreateFixture(&fixtureDef);
delete shape;
} }
/** /**
@ -60,14 +72,15 @@ Physical::~Physical() {
* @link Physical::PhysicalData * @link Physical::PhysicalData
*/ */
Physical::PhysicalData::PhysicalData( const Vector2f& position, const Vector2i& size, Physical::PhysicalData::PhysicalData( const Vector2f& position, const Vector2i& size,
b2World& world, uint16 category, uint16 maskExclude, bool moving, bool bullet) : b2World& world, uint16 category, uint16 maskExclude, bool moving, bool bullet, bool circle) :
position(position), position(position),
size(size), size(size),
world(world), world(world),
category(category), category(category),
maskExclude(maskExclude), maskExclude(maskExclude),
moving(moving), moving(moving),
bullet(bullet) { bullet(bullet),
circle(circle) {
} }
/** /**

View file

@ -27,14 +27,24 @@ public:
public: public:
PhysicalData() = default; PhysicalData() = default;
PhysicalData(const Vector2f& position, const Vector2i& size, b2World& world, PhysicalData(const Vector2f& position, const Vector2i& size, b2World& world,
uint16 category, uint16 maskExclude, bool moving, bool bullet = false); uint16 category, uint16 maskExclude, bool moving, bool bullet = false,
const Vector2f& position; //< World position of the body in pixel coordinates. bool circle = false);
const Vector2i& size; //< Pixel size of the body. /// World position of the body in pixel coordinates.
b2World& world; //< Box2D world object. const Vector2f& position;
uint16 category; //< The category for collision filtering. Only one may be set. @link Physical::Category /// Pixel size of the body if it is a box.
uint16 maskExclude; //< All categories set here will have collisions disabled with this object. Vector2i size;
bool moving; //< True if the body may move on its own (player, monster). /// Box2D world object.
bool bullet; //< True if the object is a bullet. b2World& world;
/// The category for collision filtering. Only one may be set. @link Physical::Category
uint16 category;
/// All categories set here will have collisions disabled with this object.
uint16 maskExclude;
/// True if the body may move on its own (player, monster).
bool moving;
/// True if the object is a bullet.
bool bullet;
/// True if the body collides as a circle. Radius is side length / 2, both sides must be equal.
bool circle;
}; };
/** /**