forked from eldraco/oip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentityset.cpp
More file actions
158 lines (143 loc) · 3.99 KB
/
entityset.cpp
File metadata and controls
158 lines (143 loc) · 3.99 KB
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
/*
Copyright 2008 Utah State University
This file is part of OIP.
OIP 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, either version 3 of the License, or
(at your option) any later version.
OIP 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 OIP. If not, see <http://www.gnu.org/licenses/>.
*/
#include "entityset.h"
entity& entityset::add(int s)
{
entity& r = elist[s];
if (!r.isvalid())
{
entity& r2 = elist[s] = entity(s);
if ((s & mask) == net)
r2.setlocal();
return r2;
}
r.touch();
return r;
}
float ipdist(unsigned int s1, unsigned int s2)
{
if ((s1 & 0xff000000) == (s2 & 0xff000000))
{
if ((s1 & 0xffff0000) == (s2 & 0xffff0000))
{
if ((s1 & 0xffffff00) == (s2 & 0xffffff00))
return 1.75;
else
return 1.5;
}
return 1.25;
}
else
return 1;
}
void entityset::process(double dt)
{
//go through all of the entities, and apply hookes law to each of them
map<int, entity>::iterator i = elist.begin();
for(;i != elist.end(); i++)
{
float fx=0, fy=0;
map<int, entity>::iterator j = elist.begin();
//sum up the total force applied by all the others
float rsq;
for (;j != elist.end(); j++)
{
if ((*i).first != (*j).first)
{
float nk = k * ipdist((*i).second.label, (*j).second.label);
//hookes law; linear attraction
fx -= nk*(((*i).second.getX() - (*j).second.getX()));
fy -= nk*(((*i).second.getY() - (*j).second.getY()));
//coulomb repulsion
rsq =
((*i).second.getX() - (*j).second.getX()) *
((*i).second.getX() - (*j).second.getX()) +
((*i).second.getY() - (*j).second.getY()) *
((*i).second.getY() - (*j).second.getY());
//some magic numbers. The "charge" of the particle goes away as it fades
rsq /= k/(4); // * (1+(*j).second.getfadeval()));
fx += ((*i).second.getX() - (*j).second.getX())/rsq;
fy += ((*i).second.getY() - (*j).second.getY())/rsq;
}
//add a slight jitter so that they don't get stuck in the middle
fx += (float)rand()/RAND_MAX * .001 - .0005;
fy += (float)rand()/RAND_MAX * .001 - .0005;
//rather than using hookes law, what if we just repulse from the sides?
//they should distribute themselves neatly
//They appear to just slap themselves on the sides
/*
//left side
rsq =
((*i).second.getX()) *
((*i).second.getX());
rsq /= k/100;
fx += ((*i).second.getX())/rsq;
//right side
rsq =
((*i).second.getX() - 1) *
((*i).second.getX() - 1);
rsq /= k/100;
fx += ((*i).second.getX() - 1)/rsq;
//top
rsq =
((*i).second.getY()) *
((*i).second.getY());
rsq /= k/100;
fy += ((*i).second.getY())/rsq;
//bottom
rsq =
((*i).second.getY() - 1) *
((*i).second.getY() - 1);
rsq /= k/100;
fy += ((*i).second.getY() - 1)/rsq;
// */
}
(*i).second.move(fx, fy, damp, dt);
}
}
void entityset::draw(SDL_Surface*s)
{
xscale = s->w;
yscale = s->h;
map<int, entity>::iterator j = elist.begin();
//draw them, and do some housekeeping
while (j != elist.end())
{
(*j).second.draw(s);
if ((*j).second.deleteme())
elist.erase(j++);
else
j++;
}
}
/*
* find an entity that contains the location, given in screen coords
*/
entity* entityset::find(int x, int y)
{
map<int, entity>::iterator j = elist.begin();
for (;j != elist.end();j++)
{
//TODO getX and getY are in world coordinates, yet getW and getH are not
if (
(*j).second.getX() * xscale - (*j).second.getW()/2 < x &&
(*j).second.getY() * yscale - (*j).second.getH()/2 < y &&
(*j).second.getX() * xscale + (*j).second.getW()/2 > x &&
(*j).second.getY() * yscale + (*j).second.getH()/2 > y
)
return &((*j).second);
}
return NULL;
}