effectManager created
[physics.git] / src / Entities / PhysicsEntity.cpp
1 #include "PhysicsEntity.h"
2 #include "../debug.h"
3
4 #include "../Vector2.h"
5 #include "../effectManager.h"
6
7 /// ***** Public Class Methods *****
8 PhysicsEntity::PhysicsEntity(const Vector2& pos)
9     : Entity(pos), force(0,0), mass(1), CoR(1)
10 {
11 }
12 PhysicsEntity::~PhysicsEntity()
13 {
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 {
26     Vector2 newPosition = position;
27     Vector2 newVelocity = velocity;
28     Vector2 newForce = force;
29
30     newPosition += effect::positionDelta(this, time_step);
31     newVelocity += effect::velocityDelta(this, time_step);
32     newForce += effect::forceDelta(this, time_step);
33
34     return newForce/mass / 2 * time_step * time_step + newVelocity * time_step + newPosition;
35 }
36
37 Vector2 PhysicsEntity::velocityAt(float time_step) const
38 {
39     Vector2 newForce = force;
40     Vector2 newVelocity = velocity;
41
42     newForce += effect::forceDelta(this, time_step);
43     newVelocity += effect::velocityDelta(this, time_step);
44
45     return newForce/mass / 2 * time_step + newVelocity;
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 }