added ball to ball collisions
[physics.git] / src / Entities / PhysicsEntity.cpp
CommitLineData
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"
89ca62b2
PG
19#include "../debug.h"
20
f206d19d 21#include "../effectManager.h"
617dcc71
PG
22#include "../Vector2.h"
23
24
25/// ***** Constructors/Destructors *****
ad9f1fb6 26
5f1f55d1 27PhysicsEntity::PhysicsEntity(const Vector2& pos)
c58ffbb1 28 : Entity(pos), force(0,0), mass(1), CoR(1)
ad9f1fb6 29{
617dcc71 30
ad9f1fb6
PG
31}
32PhysicsEntity::~PhysicsEntity()
33{
617dcc71 34
ad9f1fb6
PG
35}
36
617dcc71
PG
37/// ***** Public Class Methods *****
38
ad9f1fb6
PG
39void PhysicsEntity::update(float time_step)
40{
41 position = positionAt(time_step);
42 velocity = velocityAt(time_step);
43
44 force *= 0;
45}
46
47Vector2 PhysicsEntity::positionAt(float time_step) const
48{
6aad402a
PG
49 Vector2 newPosition = position;
50 Vector2 newVelocity = velocity;
51 Vector2 newForce = force;
52
f206d19d
PG
53 newPosition += effect::positionDelta(this, time_step);
54 newVelocity += effect::velocityDelta(this, time_step);
55 newForce += effect::forceDelta(this, time_step);
6aad402a
PG
56
57 return newForce/mass / 2 * time_step * time_step + newVelocity * time_step + newPosition;
ad9f1fb6
PG
58}
59
60Vector2 PhysicsEntity::velocityAt(float time_step) const
61{
6aad402a
PG
62 Vector2 newForce = force;
63 Vector2 newVelocity = velocity;
64
f206d19d
PG
65 newForce += effect::forceDelta(this, time_step);
66 newVelocity += effect::velocityDelta(this, time_step);
6aad402a
PG
67
68 return newForce/mass / 2 * time_step + newVelocity;
ad9f1fb6
PG
69}
70
71void PhysicsEntity::applyForce(const Vector2& force)
72{
54fe85c5
PG
73 applyForce(force, position);
74}
75void PhysicsEntity::applyForce(const Vector2& force, const Vector2& at)
76{
ad9f1fb6
PG
77 this->force += force;
78}
79
54fe85c5
PG
80void PhysicsEntity::applyImpulse(const Vector2& impulse)
81{
82 applyImpulse(impulse, position);
83}
84void PhysicsEntity::applyImpulse(const Vector2& impulse, const Vector2& at)
ad9f1fb6 85{
54fe85c5 86 velocity += impulse;
ad9f1fb6 87}