Commit | Line | Data |
---|---|---|
e68f847b PG |
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 | ||
ad9f1fb6 | 18 | #include "PhysicsEntity.h" |
f32a9b7c PG |
19 | |
20 | #include <pg/debug.h> | |
21 | #include <pg/Vector2.h> | |
89ca62b2 | 22 | |
3d1f0813 | 23 | #include "effectManager.h" |
617dcc71 PG |
24 | |
25 | ||
26 | /// ***** Constructors/Destructors ***** | |
ad9f1fb6 | 27 | |
5f1f55d1 | 28 | PhysicsEntity::PhysicsEntity(const Vector2& pos) |
d69c6a21 | 29 | : Entity(pos), force(0,0), mass(1), CoR(0.8) |
ad9f1fb6 | 30 | { |
617dcc71 | 31 | |
ad9f1fb6 PG |
32 | } |
33 | PhysicsEntity::~PhysicsEntity() | |
34 | { | |
617dcc71 | 35 | |
ad9f1fb6 PG |
36 | } |
37 | ||
617dcc71 PG |
38 | /// ***** Public Class Methods ***** |
39 | ||
ad9f1fb6 PG |
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 | { | |
6aad402a PG |
50 | Vector2 newPosition = position; |
51 | Vector2 newVelocity = velocity; | |
52 | Vector2 newForce = force; | |
53 | ||
f206d19d PG |
54 | newPosition += effect::positionDelta(this, time_step); |
55 | newVelocity += effect::velocityDelta(this, time_step); | |
56 | newForce += effect::forceDelta(this, time_step); | |
6aad402a PG |
57 | |
58 | return newForce/mass / 2 * time_step * time_step + newVelocity * time_step + newPosition; | |
ad9f1fb6 PG |
59 | } |
60 | ||
61 | Vector2 PhysicsEntity::velocityAt(float time_step) const | |
62 | { | |
6aad402a PG |
63 | Vector2 newForce = force; |
64 | Vector2 newVelocity = velocity; | |
65 | ||
f206d19d PG |
66 | newForce += effect::forceDelta(this, time_step); |
67 | newVelocity += effect::velocityDelta(this, time_step); | |
6aad402a PG |
68 | |
69 | return newForce/mass / 2 * time_step + newVelocity; | |
ad9f1fb6 PG |
70 | } |
71 | ||
72 | void PhysicsEntity::applyForce(const Vector2& force) | |
73 | { | |
54fe85c5 PG |
74 | applyForce(force, position); |
75 | } | |
76 | void PhysicsEntity::applyForce(const Vector2& force, const Vector2& at) | |
77 | { | |
ad9f1fb6 PG |
78 | this->force += force; |
79 | } | |
80 | ||
54fe85c5 PG |
81 | void PhysicsEntity::applyImpulse(const Vector2& impulse) |
82 | { | |
83 | applyImpulse(impulse, position); | |
84 | } | |
85 | void PhysicsEntity::applyImpulse(const Vector2& impulse, const Vector2& at) | |
ad9f1fb6 | 86 | { |
54fe85c5 | 87 | velocity += impulse; |
ad9f1fb6 | 88 | } |
19c2da50 PG |
89 | |
90 | void PhysicsEntity::applyNudge(const Vector2& vecPush) | |
91 | { | |
92 | position += vecPush; | |
93 | } |