Commit | Line | Data |
---|---|---|
ad9f1fb6 | 1 | #include "PhysicsEntity.h" |
89ca62b2 PG |
2 | #include "../debug.h" |
3 | ||
ad9f1fb6 | 4 | #include "../Vector2.h" |
6aad402a PG |
5 | #include "../Effects/Effect.h" |
6 | #include "../Effects/Gravity.h" | |
ad9f1fb6 PG |
7 | |
8 | /// ***** Public Class Methods ***** | |
5f1f55d1 | 9 | PhysicsEntity::PhysicsEntity(const Vector2& pos) |
c58ffbb1 | 10 | : Entity(pos), force(0,0), mass(1), CoR(1) |
ad9f1fb6 | 11 | { |
6aad402a | 12 | g = new Gravity(); |
ad9f1fb6 PG |
13 | } |
14 | PhysicsEntity::~PhysicsEntity() | |
15 | { | |
6aad402a | 16 | delete g; |
ad9f1fb6 PG |
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 | { | |
6aad402a PG |
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; | |
ad9f1fb6 PG |
38 | } |
39 | ||
40 | Vector2 PhysicsEntity::velocityAt(float time_step) const | |
41 | { | |
6aad402a PG |
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; | |
ad9f1fb6 PG |
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 | } |