-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathpworld.cpp
139 lines (118 loc) · 3.34 KB
/
pworld.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
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
/*
* 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 <cstddef>
#include <cyclone/pworld.h>
using namespace cyclone;
ParticleWorld::ParticleWorld(unsigned maxContacts, unsigned iterations)
:
resolver(iterations),
maxContacts(maxContacts)
{
contacts = new ParticleContact[maxContacts];
calculateIterations = (iterations == 0);
}
ParticleWorld::~ParticleWorld()
{
delete[] contacts;
}
void ParticleWorld::startFrame()
{
for (Particles::iterator p = particles.begin();
p != particles.end();
p++)
{
// Remove all forces from the accumulator
(*p)->clearAccumulator();
}
}
unsigned ParticleWorld::generateContacts()
{
unsigned limit = maxContacts;
ParticleContact *nextContact = contacts;
for (ContactGenerators::iterator g = contactGenerators.begin();
g != contactGenerators.end();
g++)
{
unsigned used =(*g)->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;
}
// Return the number of contacts used.
return maxContacts - limit;
}
void ParticleWorld::integrate(real duration)
{
for (Particles::iterator p = particles.begin();
p != particles.end();
p++)
{
// Remove all forces from the accumulator
(*p)->integrate(duration);
}
}
void ParticleWorld::runPhysics(real duration)
{
// First apply the force generators
registry.updateForces(duration);
// Then integrate the objects
integrate(duration);
// Generate contacts
unsigned usedContacts = generateContacts();
// And process them
if (usedContacts)
{
if (calculateIterations) resolver.setIterations(usedContacts * 2);
resolver.resolveContacts(contacts, usedContacts, duration);
}
}
ParticleWorld::Particles& ParticleWorld::getParticles()
{
return particles;
}
ParticleWorld::ContactGenerators& ParticleWorld::getContactGenerators()
{
return contactGenerators;
}
ParticleForceRegistry& ParticleWorld::getForceRegistry()
{
return registry;
}
void GroundContacts::init(cyclone::ParticleWorld::Particles *particles)
{
GroundContacts::particles = particles;
}
unsigned GroundContacts::addContact(cyclone::ParticleContact *contact,
unsigned limit) const
{
unsigned count = 0;
for (cyclone::ParticleWorld::Particles::iterator p = particles->begin();
p != particles->end();
p++)
{
cyclone::real y = (*p)->getPosition().y;
if (y < 0.0f)
{
contact->contactNormal = cyclone::Vector3::UP;
contact->particle[0] = *p;
contact->particle[1] = NULL;
contact->penetration = -y;
contact->restitution = 0.2f;
contact++;
count++;
}
if (count >= limit) return count;
}
return count;
}