changed src so the libpg headers are now used
[physics.git] / src / Entities / PhysicsEntity.cpp
1 /*
2  *  Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "PhysicsEntity.h"
19
20 #include <pg/debug.h>
21 #include <pg/Vector2.h>
22
23 #include "effectManager.h"
24
25
26 /// ***** Constructors/Destructors *****
27
28 PhysicsEntity::PhysicsEntity(const Vector2& pos)
29     : Entity(pos), force(0,0), mass(1), CoR(0.8)
30 {
31
32 }
33 PhysicsEntity::~PhysicsEntity()
34 {
35
36 }
37
38 /// ***** Public Class Methods *****
39
40 void PhysicsEntity::update(float time_step)
41 {
42     position = positionAt(time_step);
43     velocity = velocityAt(time_step);
44
45     force *= 0;
46 }
47
48 Vector2 PhysicsEntity::positionAt(float time_step) const
49 {
50     Vector2 newPosition = position;
51     Vector2 newVelocity = velocity;
52     Vector2 newForce = force;
53
54     newPosition += effect::positionDelta(this, time_step);
55     newVelocity += effect::velocityDelta(this, time_step);
56     newForce += effect::forceDelta(this, time_step);
57
58     return newForce/mass / 2 * time_step * time_step + newVelocity * time_step + newPosition;
59 }
60
61 Vector2 PhysicsEntity::velocityAt(float time_step) const
62 {
63     Vector2 newForce = force;
64     Vector2 newVelocity = velocity;
65
66     newForce += effect::forceDelta(this, time_step);
67     newVelocity += effect::velocityDelta(this, time_step);
68
69     return newForce/mass / 2 * time_step + newVelocity;
70 }
71
72 void PhysicsEntity::applyForce(const Vector2& force)
73 {
74     applyForce(force, position);
75 }
76 void PhysicsEntity::applyForce(const Vector2& force, const Vector2& at)
77 {
78     this->force += force;
79 }
80
81 void PhysicsEntity::applyImpulse(const Vector2& impulse)
82 {
83     applyImpulse(impulse, position);
84 }
85 void PhysicsEntity::applyImpulse(const Vector2& impulse, const Vector2& at)
86 {
87     velocity += impulse;
88 }
89
90 void PhysicsEntity::applyNudge(const Vector2& vecPush)
91 {
92     position += vecPush;
93 }