Mercurial > projects > openmelee
annotate melee.d @ 7:2217fd1fe384
added angular and linear velocity limit
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 19:34:20 -0400 |
parents | eb6059f7035a |
children | 4ee9e4a0c03b |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright (c) 2009, Mason Green (zzzzrrr) | |
3 * | |
4 * All rights reserved. | |
5 * | |
6 * Redistribution and use in source and binary forms, with or without modification, | |
7 * are permitted provided that the following conditions are met: | |
8 * | |
9 * * Redistributions of source code must retain the above copyright notice, | |
10 * this list of conditions and the following disclaimer. | |
11 * * Redistributions in binary form must reproduce the above copyright notice, | |
12 * this list of conditions and the following disclaimer in the documentation | |
13 * and/or other materials provided with the distribution. | |
14 * * Neither the name of the polygonal nor the names of its contributors may be | |
15 * used to endorse or promote products derived from this software without specific | |
16 * prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
2 | 30 module openmelee.melee; |
0 | 31 |
5 | 32 import tango.io.Stdout: Stdout; |
33 | |
0 | 34 import Integer = tango.text.convert.Integer; |
35 import tango.math.Math; | |
36 import tango.math.random.Kiss; | |
37 | |
38 import xf.hybrid.Event; | |
39 import xf.input.KeySym; | |
40 import xf.omg.core.LinearAlgebra; | |
41 | |
42 public import blaze.all; | |
43 | |
2 | 44 import openmelee.boundaryListener; |
45 import openmelee.contactListener; | |
46 import openmelee.ship; | |
47 import openmelee.urQuan; | |
48 import openmelee.orz; | |
6 | 49 import openmelee.planet; |
0 | 50 |
51 // Cursor scale factor | |
52 const CURSORSIZE = 0.05f; | |
53 | |
54 const INIT_SPAWN_SIZE = 0.5f; | |
55 | |
56 // Dragging stuffs | |
57 const BUNGEE_K = 1.5f; | |
58 // Damping factor for dragging | |
59 const DRAGDAMP = 20.0f; | |
60 | |
61 // Size of hinges | |
62 const HINGE_RADIUS = 0.05f; | |
63 | |
64 // Smallest allowed dimension | |
65 const MIN_DIMENSION = 0.1; | |
66 | |
67 const k_maxContactPoints = 2048; | |
68 | |
69 enum ContactState { | |
70 e_contactAdded, | |
71 e_contactPersisted, | |
72 e_contactRemoved | |
73 } | |
74 | |
75 struct ContactPoint { | |
76 bzShape shape1; | |
77 bzShape shape2; | |
78 bzVec2 normal; | |
79 bzVec2 position; | |
80 bzVec2 velocity; | |
81 bzContactID id; | |
82 ContactState state; | |
83 } | |
84 | |
85 // Melee settings. Some can be controlled in the GUI. | |
86 struct Settings { | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
87 float hz = 30; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
88 int velocityIterations = 5; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
89 int positionIterations = 1; |
0 | 90 bool drawShapes = true; |
91 bool drawJoints = true; | |
92 bool drawControllers; | |
93 bool drawCoreShapes; | |
5 | 94 bool drawAABBs; |
0 | 95 bool drawOBBs; |
96 bool drawPairs; | |
97 bool drawContactPoints; | |
98 bool drawContactNormals; | |
99 bool drawContactForces; | |
100 bool drawFrictionForces; | |
101 bool drawCOMs; | |
102 bool drawStats; | |
103 bool enableWarmStarting; | |
104 bool enableTOI; | |
105 } | |
106 | |
107 // Dirty, dirty hack for communicating config changes to Main | |
108 // TODO: Harass h3 to add .changed to hybrid so this isn't necessary | |
109 struct ConfigChange (T) | |
110 { | |
111 protected | |
112 { | |
113 bool _pending = false; | |
114 T _value; | |
115 } | |
116 | |
117 T value() | |
118 { | |
119 _pending = false; | |
120 return _value; | |
121 } | |
122 | |
123 void value(T change) | |
124 { | |
125 _value = change; | |
126 _pending = true; | |
127 } | |
128 | |
129 bool pending() | |
130 { | |
131 return _pending; | |
132 } | |
133 | |
134 T opAssign(T change) | |
135 { | |
136 value(change); | |
137 return _value; | |
138 } | |
139 | |
140 bool opEquals(T other) | |
141 { | |
142 return _value == other; | |
143 } | |
144 } | |
145 | |
146 T randomRange(T = int) (T min, T max) | |
147 { | |
148 return min + Kiss.instance.natural() % (max + 1 - min); | |
149 } | |
150 | |
151 class Melee | |
152 { | |
153 | |
154 this(Settings *settings) | |
155 { | |
156 this.settings = settings; | |
157 spawnRect = vec2(INIT_SPAWN_SIZE, INIT_SPAWN_SIZE); | |
158 // bzWorld boundary callback | |
159 m_boundaryListener = new BoundaryListener(this); | |
160 // bzContact callback | |
161 m_contactListener = new ContactListener(this); | |
5 | 162 init(); |
0 | 163 } |
164 | |
165 void init() { | |
166 // Define world boundaries | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
167 worldAABB.lowerBound.set(-200.0f, -200.0f); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
168 worldAABB.upperBound.set(200.0f, 200.0f); |
0 | 169 world = new bzWorld(worldAABB, gravity, allowSleep); |
170 world.boundaryListener = m_boundaryListener; | |
171 world.contactListener = m_contactListener; | |
172 viewCenter = vec2(10, 10); | |
173 ship1 = new Orz(world); | |
174 ship2 = new UrQuan(world); | |
6 | 175 auto planet = new Planet(world); |
0 | 176 } |
177 | |
178 void drag() | |
179 { | |
180 | |
181 } | |
182 | |
183 EventHandling onClick(MouseButtonEvent e) | |
184 { | |
185 return EventHandling.Stop; | |
186 } | |
187 | |
188 EventHandling onKey(KeyboardEvent e) | |
189 { | |
5 | 190 // Key pressed |
0 | 191 if (e.down) { |
192 switch (e.keySym) { | |
5 | 193 case KeySym.space: |
194 settings.drawAABBs = !settings.drawAABBs; | |
195 break; | |
0 | 196 case KeySym.Escape: |
197 quit = true; | |
198 break; | |
5 | 199 case KeySym.Up: |
200 thrust = true; | |
201 break; | |
202 case KeySym.Left: | |
203 ship1.turnLeft(); | |
204 break; | |
205 case KeySym.Right: | |
206 ship1.turnRight(); | |
207 break; | |
208 case KeySym.Down: | |
209 break; | |
0 | 210 default: |
211 break; | |
212 } | |
213 // Key released | |
214 } else { | |
5 | 215 if(e.keySym == KeySym.Up) { |
0 | 216 thrust = false; |
5 | 217 } else if (e.keySym == KeySym.Left || e.keySym == KeySym.Right) { |
0 | 218 ship1.rBody.angularVelocity = 0.0f; |
219 } | |
220 } | |
221 return EventHandling.Stop; | |
222 } | |
223 | |
224 // Mouse move | |
225 EventHandling onMove(MouseMoveEvent e) | |
226 { | |
227 return EventHandling.Stop; | |
228 } | |
229 | |
230 EventHandling onDT(TimeUpdateEvent e) | |
231 { | |
232 return EventHandling.Continue; | |
233 } | |
234 | |
235 EventHandling onMouseEnter(MouseEnterEvent e) | |
236 { | |
237 return EventHandling.Continue; | |
238 } | |
239 | |
240 EventHandling onMouseLeave(MouseLeaveEvent e) | |
241 { | |
242 return EventHandling.Continue; | |
243 } | |
244 | |
245 protected | |
246 { | |
247 const bzVec2 gravity = bzVec2(0.0f, 0.0f); | |
248 bool allowSleep = false; | |
249 | |
250 vec2 spawnRect; | |
251 | |
252 vec2[] drawing; | |
253 | |
254 vec2i screenSize = vec2i.zero; | |
255 vec2 mousePos = vec2.zero; | |
256 | |
257 bool scaling = false; | |
258 bool full = false; | |
259 | |
260 vec2 spawnStart; | |
261 | |
262 bool preserveBullet = false; | |
263 | |
264 float waterDelta = 0; | |
265 } | |
266 | |
267 void boundaryViolated(bzBody rBody) | |
268 { | |
5 | 269 float x,y; |
270 | |
271 if(rBody.position.x > worldAABB.upperBound.x) { | |
272 x = worldAABB.lowerBound.x + 5; | |
273 rBody.position = bzVec2(x, rBody.position.y); | |
274 } else if (rBody.position.x < worldAABB.lowerBound.x) { | |
275 x = worldAABB.upperBound.x - 5; | |
276 rBody.position = bzVec2(x, rBody.position.y); | |
277 } else if (rBody.position.y > worldAABB.upperBound.y) { | |
278 y = worldAABB.lowerBound.y + 5; | |
279 rBody.position = bzVec2(rBody.position.x, y); | |
280 } else if(rBody.position.y < worldAABB.lowerBound.y) { | |
281 y = worldAABB.upperBound.y - 5; | |
282 rBody.position = bzVec2(rBody.position.x, y); | |
283 } | |
0 | 284 } |
285 | |
286 bool quit; | |
287 | |
288 // Ortho view zoom | |
289 float zoom = 40; | |
290 int pointCount; | |
291 vec2 viewCenter; | |
292 | |
293 bzWorld world; | |
294 Settings *settings; | |
295 | |
296 // bzWorld boundary listener. Destroy bodies that leave world bzAABB | |
297 bzBoundaryListener m_boundaryListener; | |
298 bzContactListener m_contactListener; | |
299 ContactPoint[k_maxContactPoints] points; | |
300 | |
301 ConfigChange!(vec2) editChange; | |
302 bool thrust; | |
2 | 303 bzAABB worldAABB; |
304 | |
0 | 305 Ship ship1; |
306 Ship ship2; | |
307 } | |
308 | |
309 // Utility functions | |
310 bzVec2 toBlaze(vec2 vec) | |
311 { | |
312 auto ret = bzVec2(vec.x, vec.y); | |
313 return ret; | |
314 } | |
315 | |
316 vec2 rotate(vec2 point, float rad) | |
317 { | |
318 return vec2(cos(rad) * point.x - sin(rad) * point.y, sin(rad) * point.x + cos(rad) * point.y); | |
319 } | |
320 | |
321 class Select | |
322 { | |
323 bool select; | |
324 } |