2012-09-09 20:50:15 +00:00
|
|
|
/*
|
2012-12-20 09:31:32 +00:00
|
|
|
* Body.cpp
|
2012-09-09 20:50:15 +00:00
|
|
|
*
|
|
|
|
* Created on: 11.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
2012-12-20 09:31:32 +00:00
|
|
|
#include "Body.h"
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
2012-12-20 09:31:32 +00:00
|
|
|
const String Body::KEY_SIZE = "size";
|
2012-12-20 13:59:05 +00:00
|
|
|
const Vector2i Body::DEFAULT_SIZE = Vector2i(50, 50);
|
2012-10-13 10:22:18 +00:00
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
/**
|
|
|
|
* Initializes Box2D body.
|
|
|
|
*
|
|
|
|
* @param data Data needed for construction.
|
|
|
|
*/
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) :
|
2012-09-09 20:50:15 +00:00
|
|
|
mDelete(false) {
|
2012-10-13 10:54:09 +00:00
|
|
|
Vector2i size = (pSize == Vector2i())
|
2012-12-20 13:59:05 +00:00
|
|
|
? config.get(KEY_SIZE, DEFAULT_SIZE)
|
2012-10-13 10:54:09 +00:00
|
|
|
: pSize;
|
2012-10-13 10:22:18 +00:00
|
|
|
assert(size != Vector2i());
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-09-12 12:21:57 +00:00
|
|
|
b2BodyDef bodyDef;
|
2012-09-28 16:43:28 +00:00
|
|
|
bodyDef.type = (data.moving)
|
2012-09-12 12:21:57 +00:00
|
|
|
? b2_dynamicBody
|
2012-09-09 20:50:15 +00:00
|
|
|
: b2_staticBody;
|
|
|
|
bodyDef.position = vector(data.position);
|
|
|
|
bodyDef.allowSleep = true;
|
|
|
|
bodyDef.fixedRotation = true;
|
|
|
|
bodyDef.bullet = data.bullet;
|
|
|
|
bodyDef.userData = this;
|
|
|
|
|
|
|
|
mBody = data.world.CreateBody(&bodyDef);
|
|
|
|
|
2012-09-13 12:34:36 +00:00
|
|
|
b2Shape* shape;
|
|
|
|
if (data.circle) {
|
2012-10-13 10:22:18 +00:00
|
|
|
assert(size.x == size.y);
|
2012-09-13 12:34:36 +00:00
|
|
|
shape = new b2CircleShape;
|
2012-10-13 10:22:18 +00:00
|
|
|
shape->m_radius = pixelToMeter(size.x) / 2;
|
2012-09-13 12:34:36 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
b2PolygonShape* box = new b2PolygonShape;
|
2012-10-13 10:22:18 +00:00
|
|
|
box->SetAsBox(pixelToMeter(size.x) / 2,
|
|
|
|
pixelToMeter(size.y) / 2);
|
2012-09-13 12:34:36 +00:00
|
|
|
shape = dynamic_cast<b2Shape*>(box);
|
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
b2FixtureDef fixtureDef;
|
2012-09-13 12:34:36 +00:00
|
|
|
fixtureDef.shape = shape;
|
2012-09-09 20:50:15 +00:00
|
|
|
fixtureDef.density = 1.0f;
|
|
|
|
fixtureDef.filter.categoryBits = data.category;
|
|
|
|
fixtureDef.filter.maskBits = ~data.maskExclude;
|
2012-09-12 12:21:57 +00:00
|
|
|
fixtureDef.restitution = 0;
|
|
|
|
fixtureDef.density = (data.bullet) ? 0 : 10000;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
mBody->CreateFixture(&fixtureDef);
|
2012-09-13 12:34:36 +00:00
|
|
|
|
|
|
|
delete shape;
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes body from world.
|
|
|
|
*/
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::~Body() {
|
2012-09-09 20:50:15 +00:00
|
|
|
mBody->GetWorld()->DestroyBody(mBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes container.
|
|
|
|
*
|
|
|
|
* @link Physical::PhysicalData
|
|
|
|
*/
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::PhysicalData::PhysicalData( const Vector2f& position, b2World& world, uint16 category,
|
2012-10-13 10:22:18 +00:00
|
|
|
uint16 maskExclude, bool moving, bool bullet, bool circle) :
|
|
|
|
position(position),
|
|
|
|
world(world),
|
|
|
|
category(category),
|
|
|
|
maskExclude(maskExclude),
|
|
|
|
moving(moving),
|
|
|
|
bullet(bullet),
|
|
|
|
circle(circle) {
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the position of the sprite (center).
|
|
|
|
*/
|
|
|
|
Vector2f
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::getPosition() const {
|
2012-09-09 20:50:15 +00:00
|
|
|
return vector(mBody->GetPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the movement speed of the body.
|
|
|
|
*/
|
|
|
|
Vector2f
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::getSpeed() const {
|
2012-09-09 20:50:15 +00:00
|
|
|
return vector(mBody->GetLinearVelocity());
|
|
|
|
}
|
|
|
|
|
2012-09-15 13:28:15 +00:00
|
|
|
/**
|
|
|
|
* Returns the rotation of the body (converted to an SFML angle).
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
|
|
|
float
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::getAngle() const {
|
2012-09-13 11:35:16 +00:00
|
|
|
return thor::toDegree(mBody->GetAngle());
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this object should be deleted.
|
|
|
|
*/
|
|
|
|
bool
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::getDelete() const {
|
2012-09-09 20:50:15 +00:00
|
|
|
return mDelete;
|
|
|
|
}
|
2012-09-12 12:21:57 +00:00
|
|
|
|
2012-09-11 17:37:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the Physical::Category of this object.
|
2012-09-12 12:21:57 +00:00
|
|
|
*/
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::Category
|
|
|
|
Body::getCategory() const {
|
2012-09-16 18:33:00 +00:00
|
|
|
return (Category) mBody->GetFixtureList()->GetFilterData().categoryBits;
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 16:43:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the size of the body as a vector.
|
|
|
|
*/
|
|
|
|
Vector2f
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::getSize() const {
|
2012-09-28 16:43:28 +00:00
|
|
|
b2AABB aabb(mBody->GetFixtureList()->GetAABB(0));
|
|
|
|
return vector(aabb.upperBound - aabb.lowerBound);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if collisions are enabled for the body.
|
|
|
|
*/
|
|
|
|
bool
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::isSolid() const {
|
2012-09-28 16:43:28 +00:00
|
|
|
return mBody->GetFixtureList()->GetFilterData().maskBits != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the body is able to move.
|
|
|
|
*/
|
|
|
|
bool
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::isMovable() const {
|
2012-09-28 16:43:28 +00:00
|
|
|
return mBody->GetType() != b2_staticBody;
|
|
|
|
}
|
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
/**
|
|
|
|
* This method filters collisions with other physicals. Implement it if you want to
|
|
|
|
* limit collisions to/with certain objects. Default implementation always returns true.
|
|
|
|
*
|
|
|
|
* @param other The Physical this object is about to collide with.
|
|
|
|
* @return True if the objects should collide.
|
|
|
|
*/
|
|
|
|
bool
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::doesCollide(Body& other) {
|
2012-09-09 20:50:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a collision with another body occured. Override this method
|
|
|
|
* to manage collision events.
|
|
|
|
*
|
|
|
|
* @param other Reference to the other Physical in the collision.
|
|
|
|
* @param category The Category of the other object (as passed in constructor).
|
|
|
|
*/
|
|
|
|
void
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::onCollide(Body& other, uint16 type) {
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set to true to mark this object for deletion from the world.
|
|
|
|
*/
|
|
|
|
void
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::setDelete(bool value) {
|
2012-09-09 20:50:15 +00:00
|
|
|
mDelete = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets movement speed and direction of the body. Set either value to zero to stop movement.
|
|
|
|
*
|
|
|
|
* @param direction The direction the body moves in, does not have to be normalized.
|
|
|
|
* @param speed The value of the movement speed to be used.
|
|
|
|
*/
|
|
|
|
void
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::setSpeed(Vector2f direction, float speed) {
|
2012-09-09 20:50:15 +00:00
|
|
|
if (direction != Vector2f()) {
|
|
|
|
direction = thor::unitVector<float>(direction);
|
|
|
|
}
|
|
|
|
direction *= speed;
|
|
|
|
mBody->SetLinearVelocity(vector(direction));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the angle of the body based on the direction of a vector.
|
|
|
|
*/
|
|
|
|
void
|
2012-12-20 09:31:32 +00:00
|
|
|
Body::setAngle(float angle) {
|
2012-09-13 11:35:16 +00:00
|
|
|
mBody->SetTransform(mBody->GetPosition(), thor::toRadian(angle));
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|