basic gravity working through effects
[physics.git] / src / Entities / PhysicsEntity.cpp
1 #include "PhysicsEntity.h"
2 #include "../debug.h"
3
4 #include "../Vector2.h"
5 #include "../Effects/Effect.h"
6 #include "../Effects/Gravity.h"
7
8 /// ***** Public Class Methods *****
9 PhysicsEntity::PhysicsEntity(const Vector2& pos)
10     : Entity(pos), force(0,0), mass(1), CoR(1)
11 {
12     g = new Gravity();
13 }
14 PhysicsEntity::~PhysicsEntity()
15 {
16     delete g;
17 }
18
19 void PhysicsEntity::update(float time_step)
20 {
21     position = positionAt(time_step);
22     velocity = velocityAt(time_step);
23
24     force *= 0;
25 }
26
27 Vector2 PhysicsEntity::positionAt(float time_step) const
28 {
29     Vector2 newPosition = position;
30     Vector2 newVelocity = velocity;
31     Vector2 newForce = force;
32
33     newPosition += g->positionDelta(this, time_step);
34     newVelocity += g->velocityDelta(this, time_step);
35     newForce += g->forceDelta(this, time_step);
36
37     return newForce/mass / 2 * time_step * time_step + newVelocity * time_step + newPosition;
38 }
39
40 Vector2 PhysicsEntity::velocityAt(float time_step) const
41 {
42     Vector2 newForce = force;
43     Vector2 newVelocity = velocity;
44
45     newForce += g->forceDelta(this, time_step);
46     newVelocity += g->velocityDelta(this, time_step);
47
48     return newForce/mass / 2 * time_step + newVelocity;
49 }
50
51 void PhysicsEntity::applyForce(const Vector2& force)
52 {
53     this->force += force;
54 }
55
56 void PhysicsEntity::applyImpulse(const Vector2& impluse)
57 {
58     velocity += impluse;
59 }