-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPengine.h
157 lines (123 loc) · 3.78 KB
/
Pengine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
#############################################################################
# Tux in Space - space exploration game #
# Copyright (C) 2016-2017 [email protected] #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, version 3 or compatibles. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc. #
#############################################################################
*
* phisic engine implementation
* see information file for more info
*/
#ifndef PengineH
#define PengineH
#include "generic.h"
#include "math.h"
#include "time.h"
// The physic entity
// TODO: add orientation, torque and company
class PEntity {
private:
tposition pos;
tvelocity vel;
tforce acc_force; // accumuled in the simulation slice
tforce_scalar force_single_tolerance; // if a force applyed is littler than this is ignored
tforce_scalar force_sum_tolerance; // if acc_force is littler than this will not be applyed to the object
tmass mass;
tlength radius;
protected:
inline void SetMass(tmass m) {
mass = m;
}
inline void SetRadius(tlength r) {
radius = r;
}
inline void SetPos(const tposition& _pos){
pos = _pos;
}
inline void SetVel(const tvelocity& _vel){
vel = _vel;
}
inline void SetFrcSingleTolerance (const tforce_scalar& frc){
force_single_tolerance = frc;
}
inline void SetFrcSumTolerance (const tforce_scalar& frc){
force_sum_tolerance = frc;
}
public:
inline tforce_scalar FrcSumTolerance() const {
return force_sum_tolerance;
}
inline tforce_scalar FrcSingleTolerance() const {
return force_single_tolerance;
}
inline tmass Mass() const {
return mass;
}
inline tlength Radius() const {
return radius;
}
inline tposition Pos() const {
return pos;
}
inline tvelocity Vel() const {
return vel;
}
// add a force to the entity
inline void AddForce(const tforce& force) {
if(force > force_single_tolerance)
acc_force += force;
}
// add an impulse to the entity
inline void AddImpulse(const tmomentum& impulse, const time_raw& t){
AddForce(impulse / t);
}
// Move all the stats about position and kinematic elements to a dest PEntity
inline void MoveKinematic(PEntity& destination){
destination.pos = pos;
destination.vel = vel;
destination.acc_force = acc_force;
}
inline tposition Distance(const PEntity& e) {
return tposition(pos - e.pos);
}
// Does the two object touch?
bool touch(const PEntity& e) {
return Distance(e).scalar() <= (e.radius + radius);
}
// Do the simulation for the time slice given
void Simulation(const time_raw& delta);
inline PEntity(const tposition& _pos, const tvelocity& _vel){
pos = _pos;
vel = _vel;
}
inline PEntity(const tposition& _pos,
const tvelocity& _vel,
const tforce_scalar& _tol_single,
const tforce_scalar& _tol_sum,
const tmass& _mass,
const tlength& _radius)
{
mass = _mass;
radius = _radius;
force_single_tolerance = _tol_single;
force_sum_tolerance = _tol_sum;
pos = _pos;
vel = _vel;
}
bool operator== (const PEntity& o);
bool operator!= (const PEntity& o);
inline PEntity(){};
};
#endif