Commit | Line | Data |
---|---|---|
ad9f1fb6 | 1 | #include "PhysicsEntity.h" |
89ca62b2 PG |
2 | #include "../debug.h" |
3 | ||
ad9f1fb6 | 4 | #include "../Vector2.h" |
f206d19d | 5 | #include "../effectManager.h" |
ad9f1fb6 PG |
6 | |
7 | /// ***** Public Class Methods ***** | |
5f1f55d1 | 8 | PhysicsEntity::PhysicsEntity(const Vector2& pos) |
c58ffbb1 | 9 | : Entity(pos), force(0,0), mass(1), CoR(1) |
ad9f1fb6 | 10 | { |
ad9f1fb6 PG |
11 | } |
12 | PhysicsEntity::~PhysicsEntity() | |
13 | { | |
ad9f1fb6 PG |
14 | } |
15 | ||
16 | void PhysicsEntity::update(float time_step) | |
17 | { | |
18 | position = positionAt(time_step); | |
19 | velocity = velocityAt(time_step); | |
20 | ||
21 | force *= 0; | |
22 | } | |
23 | ||
24 | Vector2 PhysicsEntity::positionAt(float time_step) const | |
25 | { | |
6aad402a PG |
26 | Vector2 newPosition = position; |
27 | Vector2 newVelocity = velocity; | |
28 | Vector2 newForce = force; | |
29 | ||
f206d19d PG |
30 | newPosition += effect::positionDelta(this, time_step); |
31 | newVelocity += effect::velocityDelta(this, time_step); | |
32 | newForce += effect::forceDelta(this, time_step); | |
6aad402a PG |
33 | |
34 | return newForce/mass / 2 * time_step * time_step + newVelocity * time_step + newPosition; | |
ad9f1fb6 PG |
35 | } |
36 | ||
37 | Vector2 PhysicsEntity::velocityAt(float time_step) const | |
38 | { | |
6aad402a PG |
39 | Vector2 newForce = force; |
40 | Vector2 newVelocity = velocity; | |
41 | ||
f206d19d PG |
42 | newForce += effect::forceDelta(this, time_step); |
43 | newVelocity += effect::velocityDelta(this, time_step); | |
6aad402a PG |
44 | |
45 | return newForce/mass / 2 * time_step + newVelocity; | |
ad9f1fb6 PG |
46 | } |
47 | ||
48 | void PhysicsEntity::applyForce(const Vector2& force) | |
49 | { | |
50 | this->force += force; | |
51 | } | |
52 | ||
53 | void PhysicsEntity::applyImpulse(const Vector2& impluse) | |
54 | { | |
55 | velocity += impluse; | |
56 | } |