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