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 | 19 | |
5e0713e5 PG |
20 | #include <bear/debug.h> |
21 | #include <bear/Vector2.h> | |
22 | using namespace bear; | |
89ca62b2 | 23 | |
3d1f0813 | 24 | #include "effectManager.h" |
617dcc71 PG |
25 | |
26 | ||
27 | /// ***** Constructors/Destructors ***** | |
ad9f1fb6 | 28 | |
5f1f55d1 | 29 | PhysicsEntity::PhysicsEntity(const Vector2& pos) |
fd1a93a7 | 30 | : Entity(pos), m_force(0,0), m_mass(1), m_CoR(0.8) |
ad9f1fb6 | 31 | { |
617dcc71 | 32 | |
ad9f1fb6 PG |
33 | } |
34 | PhysicsEntity::~PhysicsEntity() | |
35 | { | |
617dcc71 | 36 | |
ad9f1fb6 PG |
37 | } |
38 | ||
617dcc71 PG |
39 | /// ***** Public Class Methods ***** |
40 | ||
ad9f1fb6 PG |
41 | void PhysicsEntity::update(float time_step) |
42 | { | |
3ae4b047 PG |
43 | m_position = positionAt(time_step); |
44 | m_velocity = velocityAt(time_step); | |
ad9f1fb6 | 45 | |
fd1a93a7 | 46 | m_force *= 0; |
ad9f1fb6 PG |
47 | } |
48 | ||
49 | Vector2 PhysicsEntity::positionAt(float time_step) const | |
50 | { | |
3ae4b047 PG |
51 | Vector2 newPosition = m_position; |
52 | Vector2 newVelocity = m_velocity; | |
fd1a93a7 | 53 | Vector2 newForce = m_force; |
6aad402a | 54 | |
f206d19d PG |
55 | newPosition += effect::positionDelta(this, time_step); |
56 | newVelocity += effect::velocityDelta(this, time_step); | |
57 | newForce += effect::forceDelta(this, time_step); | |
6aad402a | 58 | |
fd1a93a7 | 59 | return newForce/m_mass / 2 * time_step * time_step + newVelocity * time_step + newPosition; |
ad9f1fb6 PG |
60 | } |
61 | ||
62 | Vector2 PhysicsEntity::velocityAt(float time_step) const | |
63 | { | |
fd1a93a7 | 64 | Vector2 newForce = m_force; |
3ae4b047 | 65 | Vector2 newVelocity = m_velocity; |
6aad402a | 66 | |
f206d19d PG |
67 | newForce += effect::forceDelta(this, time_step); |
68 | newVelocity += effect::velocityDelta(this, time_step); | |
6aad402a | 69 | |
fd1a93a7 | 70 | return newForce/m_mass / 2 * time_step + newVelocity; |
ad9f1fb6 PG |
71 | } |
72 | ||
73 | void PhysicsEntity::applyForce(const Vector2& force) | |
74 | { | |
3ae4b047 | 75 | applyForce(force, m_position); |
54fe85c5 | 76 | } |
88085362 | 77 | void PhysicsEntity::applyForce(const Vector2& force, const Vector2& /*at*/) |
54fe85c5 | 78 | { |
fd1a93a7 | 79 | this->m_force += force; |
ad9f1fb6 PG |
80 | } |
81 | ||
54fe85c5 PG |
82 | void PhysicsEntity::applyImpulse(const Vector2& impulse) |
83 | { | |
3ae4b047 | 84 | applyImpulse(impulse, m_position); |
54fe85c5 | 85 | } |
88085362 | 86 | void PhysicsEntity::applyImpulse(const Vector2& impulse, const Vector2& /*at*/) |
ad9f1fb6 | 87 | { |
3ae4b047 | 88 | m_velocity += impulse; |
ad9f1fb6 | 89 | } |
19c2da50 PG |
90 | |
91 | void PhysicsEntity::applyNudge(const Vector2& vecPush) | |
92 | { | |
3ae4b047 | 93 | m_position += vecPush; |
19c2da50 | 94 | } |