-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswSimulator.cpp
67 lines (60 loc) · 1.54 KB
/
swSimulator.cpp
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
#include "swPhysical.h"
#include "swSimulator.h"
swSimulator::swSimulator() : lastUpdate(0), netObjects(0), simulateOmega(0) {
qtime.start();
lastUpdate = qtime.elapsed();
}
swSimulator::~swSimulator() {
// this can be used as a general cleanup function
foreach(swPhysical* object, objects) {
if(object)
delete object;
}
foreach(swPhysical *object, deadObjects) {
delete object;
}
objects.clear();
deadObjects.clear();
netObjects = 0;
}
void swSimulator::draw() {
foreach(swPhysical* object, objects) {
if(object)
object->draw();
}
}
void swSimulator::insert(swPhysical* object, bool net) {
// connect the object to the simulator
object->parent = this;
// this keeps the synched objects in the low address range
if(net) {
objects.insert(netObjects, object);
netObjects++;
}else{
objects.append(object);
}
}
void swSimulator::update(int index, swPhysical* object) {
if(!object) {
qWarning("Attempt to update dead object at: %d", index);
return;
}
swPhysical* target = objects[index];
target->pos = object->pos;
target->rot = object->rot;
target->vel = object->vel;
target->omega = object->omega;
}
void swSimulator::remove(int index) {
deadObjects.append(objects[index]);
objects[index] = NULL;
}
void swSimulator::simulate() {
while(lastUpdate < qtime.elapsed()) {
foreach(swPhysical* object, objects) {
if(object)
object->tick();
}
lastUpdate += tickLen;
}
}