-
Notifications
You must be signed in to change notification settings - Fork 266
/
world.cpp
93 lines (77 loc) · 2.16 KB
/
world.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
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
/*
* Implementation file for random number generation.
*
* Part of the Cyclone physics system.
*
* Copyright (c) Icosagon 2003. All Rights Reserved.
*
* This software is distributed under licence. Use of this software
* implies agreement with all terms and conditions of the accompanying
* software licence.
*/
#include <cstdlib>
#include <cyclone/world.h>
using namespace cyclone;
World::World(unsigned maxContacts, unsigned iterations)
:
firstBody(NULL),
resolver(iterations),
firstContactGen(NULL),
maxContacts(maxContacts)
{
contacts = new Contact[maxContacts];
calculateIterations = (iterations == 0);
}
World::~World()
{
delete[] contacts;
}
void World::startFrame()
{
BodyRegistration *reg = firstBody;
while (reg)
{
// Remove all forces from the accumulator
reg->body->clearAccumulators();
reg->body->calculateDerivedData();
// Get the next registration
reg = reg->next;
}
}
unsigned World::generateContacts()
{
unsigned limit = maxContacts;
Contact *nextContact = contacts;
ContactGenRegistration * reg = firstContactGen;
while (reg)
{
unsigned used = reg->gen->addContact(nextContact, limit);
limit -= used;
nextContact += used;
// We've run out of contacts to fill. This means we're missing
// contacts.
if (limit <= 0) break;
reg = reg->next;
}
// Return the number of contacts used.
return maxContacts - limit;
}
void World::runPhysics(real duration)
{
// First apply the force generators
//registry.updateForces(duration);
// Then integrate the objects
BodyRegistration *reg = firstBody;
while (reg)
{
// Remove all forces from the accumulator
reg->body->integrate(duration);
// Get the next registration
reg = reg->next;
}
// Generate contacts
unsigned usedContacts = generateContacts();
// And process them
if (calculateIterations) resolver.setIterations(usedContacts * 4);
resolver.resolveContacts(contacts, usedContacts, duration);
}