Added Sprite::getRadius().

This commit is contained in:
Felix Ableitner 2013-03-19 20:51:56 +01:00
parent 1dfd878a20
commit ff65f6ceb6
3 changed files with 17 additions and 6 deletions

View file

@ -109,12 +109,10 @@ World::testCollision(std::shared_ptr<Sprite> spriteA,
} }
axis = thor::unitVector(axis); axis = thor::unitVector(axis);
float centerA = thor::dotProduct(axis, spriteA->getPosition()); float centerA = thor::dotProduct(axis, spriteA->getPosition());
float radiusA = std::static_pointer_cast<sf::CircleShape>( float radiusA = spriteA->getRadius();
spriteA->mShape.shape)->getRadius();
float movementA = thor::dotProduct(axis, spriteA->getSpeed() * (elapsed / 1000.0f)); float movementA = thor::dotProduct(axis, spriteA->getSpeed() * (elapsed / 1000.0f));
float centerB = thor::dotProduct(axis, spriteB->getPosition()); float centerB = thor::dotProduct(axis, spriteB->getPosition());
float radiusB = std::static_pointer_cast<sf::CircleShape>( float radiusB = spriteB->getRadius();
spriteB->mShape.shape)->getRadius();
float movementB = thor::dotProduct(axis, spriteB->getSpeed() * (elapsed / 1000.0f)); float movementB = thor::dotProduct(axis, spriteB->getSpeed() * (elapsed / 1000.0f));
// Allow movement if sprites are moving apart. // Allow movement if sprites are moving apart.
@ -132,8 +130,7 @@ World::testCollision(std::shared_ptr<Sprite> spriteA,
if (circle->mShape.type != Sprite::Shape::Type::CIRCLE) { if (circle->mShape.type != Sprite::Shape::Type::CIRCLE) {
std::swap(circle, rect); std::swap(circle, rect);
} }
float radius = float radius = circle->getRadius();
std::static_pointer_cast<sf::CircleShape>(circle->mShape.shape)->getRadius();
sf::Vector2f halfsize = rect->getSize() / 2.0f; sf::Vector2f halfsize = rect->getSize() / 2.0f;
sf::Vector2f circlePos = circle->getPosition(); sf::Vector2f circlePos = circle->getPosition();
sf::Vector2f rectPos = rect->getPosition(); sf::Vector2f rectPos = rect->getPosition();

View file

@ -188,3 +188,16 @@ void
Sprite::setPosition(const sf::Vector2f& position) { Sprite::setPosition(const sf::Vector2f& position) {
mShape.shape->setPosition(position); mShape.shape->setPosition(position);
} }
/**
* Returns the radius of this sprite. Will fail if this is not a circle.
*
* @return The radius of this sprite.
*/
float
Sprite::getRadius() const {
std::shared_ptr<sf::CircleShape> circleShape =
std::dynamic_pointer_cast<sf::CircleShape>(mShape.shape);
assert(circleShape);
return circleShape->getRadius();
}

View file

@ -81,6 +81,7 @@ protected:
void setSpeed(sf::Vector2f direction, float speed); void setSpeed(sf::Vector2f direction, float speed);
void setAngle(float angle); void setAngle(float angle);
void setPosition(const sf::Vector2f& position); void setPosition(const sf::Vector2f& position);
float getRadius() const;
// Private types. // Private types.
private: private: