Line data Source code
1 : #include "PhysicalObject.hpp" 2 : #include "ExceptionWithLocation.hpp" 3 : 4 : #include <cmath> 5 : 6 : namespace cell 7 : { 8 : 9 333 : void PhysicalObject::setVelocity(const sf::Vector2d& velocity) 10 : { 11 : #ifdef DEBUG 12 333 : if (isNanOrInf(velocity)) 13 0 : throw ExceptionWithLocation("Trying to assign an invalid value to velocity"); 14 : #endif 15 333 : velocity_ = velocity; 16 333 : } 17 : 18 2 : void PhysicalObject::scaleVelocity(double factor) 19 : { 20 2 : setVelocity(velocity_ * factor); 21 2 : } 22 : 23 7 : void PhysicalObject::accelerate(const sf::Vector2d& acceleration) 24 : { 25 7 : setVelocity(velocity_ + acceleration); 26 7 : } 27 : 28 356 : void PhysicalObject::setPosition(const sf::Vector2d& position) 29 : { 30 : #ifdef DEBUG 31 356 : if (isNanOrInf(position)) 32 0 : throw ExceptionWithLocation("Trying to assign an invalid value to position"); 33 : #endif 34 356 : position_ = position; 35 356 : } 36 : 37 17 : void PhysicalObject::move(const sf::Vector2d& distance) 38 : { 39 17 : setPosition(position_ + distance); 40 17 : } 41 : 42 32 : const sf::Vector2d& PhysicalObject::getVelocity() const 43 : { 44 32 : return velocity_; 45 : } 46 : 47 362878 : const sf::Vector2d& PhysicalObject::getPosition() const 48 : { 49 362878 : return position_; 50 : } 51 : 52 0 : double PhysicalObject::getAbsoluteMomentum(double mass) const 53 : { 54 0 : return mass * std::hypot(velocity_.x, velocity_.y); 55 : } 56 : 57 0 : sf::Vector2d PhysicalObject::getMomentum(double mass) const 58 : { 59 0 : return mass * velocity_; 60 : } 61 : 62 0 : double PhysicalObject::getKineticEnergy(double mass) const 63 : { 64 0 : return 0.5 * mass * (velocity_.x * velocity_.x + velocity_.y * velocity_.y); 65 : } 66 : 67 689 : bool PhysicalObject::isNanOrInf(const sf::Vector2d& vec) const 68 : { 69 689 : return std::isnan(vec.x) || std::isnan(vec.y) || std::isinf(vec.x) || std::isinf(vec.y); 70 : } 71 : 72 : } // namespace cell