4 * Created on: Apr 9, 2012
15 using geometry::Shape;
16 using std::numeric_limits;
21 Entity::Entity(Shape *shape)
26 , maxVelocitySquared(numeric_limits<Scalar>::infinity())
28 , updateVelocity(false) {
32 Entity::Entity(const Vector &position, Shape *shape)
37 , maxVelocitySquared(numeric_limits<Scalar>::infinity())
39 , updateVelocity(false) {
43 Entity::~Entity(void) {
48 void Entity::Move(const Vector &delta) {
50 bounds->SetPosition(position);
53 void Entity::SetPosition(const Vector &p) {
55 bounds->SetPosition(position);
59 void Entity::SetVelocity(const Vector &v) {
68 Entity::Vector Entity::Center(void) const {
69 return bounds->Center();
73 void Entity::Accelerate(Scalar factor) {
75 Vector scaled(nextVelocity * factor);
76 float speedSquared(scaled.LengthSquared());
77 if (speedSquared <= maxVelocitySquared) {
78 nextVelocity = scaled;
81 Vector scaled(velocity * factor);
82 float speedSquared(scaled.LengthSquared());
83 if (speedSquared <= maxVelocitySquared) {
89 void Entity::SetMaxVelocity(Scalar max) {
90 maxVelocitySquared = max * max;
94 void Entity::Update(float deltaT) {
95 if (velocity.X() == Limits::quiet_NaN() || velocity.X() == -Limits::quiet_NaN()) {
96 cout << "NaN! " << velocity << endl;
98 position += velocity * deltaT;
99 bounds->SetPosition(position);
102 void Entity::Revert(float deltaT) {
103 position -= velocity * deltaT;
104 bounds->SetPosition(position);
107 void Entity::Collide(Entity &other, const Vector &normal) {
108 nextVelocity = velocity;
109 nextVelocity.Reflect(normal);
110 updateVelocity = true;
113 void Entity::PostCollide(void) {
114 if (updateVelocity) {
115 velocity = nextVelocity;
116 updateVelocity = false;