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);
b2PolygonShape boxShape;
boxShape.SetAsBox(pixelToMeter(data.size.x) / 2, pixelToMeter(data.size.y) / 2);
b2Shape* shape;
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;
fixtureDef.shape = &boxShape;
fixtureDef.shape = shape;
fixtureDef.density = 1.0f;
fixtureDef.filter.categoryBits = data.category;
fixtureDef.filter.maskBits = ~data.maskExclude;
@ -45,6 +55,8 @@ Physical::Physical(const PhysicalData& data) :
fixtureDef.density = (data.bullet) ? 0 : 10000;
mBody->CreateFixture(&fixtureDef);
delete shape;
}
/**
@ -60,14 +72,15 @@ Physical::~Physical() {
* @link Physical::PhysicalData
*/
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),
size(size),
world(world),
category(category),
maskExclude(maskExclude),
moving(moving),
bullet(bullet) {
bullet(bullet),
circle(circle) {
}
/**

View File

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