diff options
Diffstat (limited to 'OpenSim/OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs')
-rw-r--r-- | OpenSim/OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs | 449 |
1 files changed, 449 insertions, 0 deletions
diff --git a/OpenSim/OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs b/OpenSim/OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs new file mode 100644 index 0000000..dff1ee2 --- /dev/null +++ b/OpenSim/OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs | |||
@@ -0,0 +1,449 @@ | |||
1 | /* | ||
2 | * Copyright (c) OpenSim project, http://sim.opensecondlife.org/ | ||
3 | * | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions are met: | ||
6 | * * Redistributions of source code must retain the above copyright | ||
7 | * notice, this list of conditions and the following disclaimer. | ||
8 | * * Redistributions in binary form must reproduce the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer in the | ||
10 | * documentation and/or other materials provided with the distribution. | ||
11 | * * Neither the name of the <organization> nor the | ||
12 | * names of its contributors may be used to endorse or promote products | ||
13 | * derived from this software without specific prior written permission. | ||
14 | * | ||
15 | * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY | ||
16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
18 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY | ||
19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
25 | * | ||
26 | */ | ||
27 | /* | ||
28 | * Copyright (c) OpenSim project, http://sim.opensecondlife.org/ | ||
29 | * | ||
30 | * Redistribution and use in source and binary forms, with or without | ||
31 | * modification, are permitted provided that the following conditions are met: | ||
32 | * * Redistributions of source code must retain the above copyright | ||
33 | * notice, this list of conditions and the following disclaimer. | ||
34 | * * Redistributions in binary form must reproduce the above copyright | ||
35 | * notice, this list of conditions and the following disclaimer in the | ||
36 | * documentation and/or other materials provided with the distribution. | ||
37 | * * Neither the name of the <organization> nor the | ||
38 | * names of its contributors may be used to endorse or promote products | ||
39 | * derived from this software without specific prior written permission. | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY | ||
42 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
43 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
44 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY | ||
45 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
46 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
48 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
49 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
50 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
51 | * | ||
52 | */ | ||
53 | using System; | ||
54 | using System.Collections.Generic; | ||
55 | using OpenSim.Physics.Manager; | ||
56 | using PhysXWrapper; | ||
57 | |||
58 | namespace OpenSim.Physics.PhysXPlugin | ||
59 | { | ||
60 | /// <summary> | ||
61 | /// Will be the PhysX plugin but for now will be a very basic physics engine | ||
62 | /// </summary> | ||
63 | public class PhysXPlugin : IPhysicsPlugin | ||
64 | { | ||
65 | private PhysXScene _mScene; | ||
66 | |||
67 | public PhysXPlugin() | ||
68 | { | ||
69 | |||
70 | } | ||
71 | |||
72 | public bool Init() | ||
73 | { | ||
74 | return true; | ||
75 | } | ||
76 | |||
77 | public PhysicsScene GetScene() | ||
78 | { | ||
79 | if(_mScene == null) | ||
80 | { | ||
81 | _mScene = new PhysXScene(); | ||
82 | } | ||
83 | return(_mScene); | ||
84 | } | ||
85 | |||
86 | public string GetName() | ||
87 | { | ||
88 | return("RealPhysX"); | ||
89 | } | ||
90 | |||
91 | public void Dispose() | ||
92 | { | ||
93 | |||
94 | } | ||
95 | } | ||
96 | |||
97 | public class PhysXScene :PhysicsScene | ||
98 | { | ||
99 | private List<PhysXCharacter> _characters = new List<PhysXCharacter>(); | ||
100 | private List<PhysXPrim> _prims = new List<PhysXPrim>(); | ||
101 | private float[] _heightMap = null; | ||
102 | private NxPhysicsSDK mySdk; | ||
103 | private NxScene scene; | ||
104 | |||
105 | public PhysXScene() | ||
106 | { | ||
107 | mySdk = NxPhysicsSDK.CreateSDK(); | ||
108 | Console.WriteLine("Sdk created - now creating scene"); | ||
109 | scene = mySdk.CreateScene(); | ||
110 | |||
111 | } | ||
112 | |||
113 | public override PhysicsActor AddAvatar(PhysicsVector position) | ||
114 | { | ||
115 | Vec3 pos = new Vec3(); | ||
116 | pos.X = position.X; | ||
117 | pos.Y = position.Y; | ||
118 | pos.Z = position.Z; | ||
119 | PhysXCharacter act = new PhysXCharacter( scene.AddCharacter(pos)); | ||
120 | act.Position = position; | ||
121 | _characters.Add(act); | ||
122 | return act; | ||
123 | } | ||
124 | |||
125 | public override void RemoveAvatar(PhysicsActor actor) | ||
126 | { | ||
127 | |||
128 | } | ||
129 | |||
130 | public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size) | ||
131 | { | ||
132 | Vec3 pos = new Vec3(); | ||
133 | pos.X = position.X; | ||
134 | pos.Y = position.Y; | ||
135 | pos.Z = position.Z; | ||
136 | Vec3 siz = new Vec3(); | ||
137 | siz.X = size.X; | ||
138 | siz.Y = size.Y; | ||
139 | siz.Z = size.Z; | ||
140 | PhysXPrim act = new PhysXPrim( scene.AddNewBox(pos, siz)); | ||
141 | _prims.Add(act); | ||
142 | return act; | ||
143 | } | ||
144 | public override void Simulate(float timeStep) | ||
145 | { | ||
146 | try | ||
147 | { | ||
148 | foreach (PhysXCharacter actor in _characters) | ||
149 | { | ||
150 | actor.Move(timeStep); | ||
151 | } | ||
152 | scene.Simulate(timeStep); | ||
153 | scene.FetchResults(); | ||
154 | scene.UpdateControllers(); | ||
155 | |||
156 | foreach (PhysXCharacter actor in _characters) | ||
157 | { | ||
158 | actor.UpdatePosition(); | ||
159 | } | ||
160 | } | ||
161 | catch (Exception e) | ||
162 | { | ||
163 | Console.WriteLine(e.Message); | ||
164 | } | ||
165 | |||
166 | } | ||
167 | |||
168 | public override void GetResults() | ||
169 | { | ||
170 | |||
171 | } | ||
172 | |||
173 | public override bool IsThreaded | ||
174 | { | ||
175 | get | ||
176 | { | ||
177 | return(false); // for now we won't be multithreaded | ||
178 | } | ||
179 | } | ||
180 | |||
181 | public override void SetTerrain(float[] heightMap) | ||
182 | { | ||
183 | if (this._heightMap != null) | ||
184 | { | ||
185 | Console.WriteLine("PhysX - deleting old terrain"); | ||
186 | this.scene.DeleteTerrain(); | ||
187 | } | ||
188 | this._heightMap = heightMap; | ||
189 | this.scene.AddTerrain(heightMap); | ||
190 | } | ||
191 | |||
192 | public override void DeleteTerrain() | ||
193 | { | ||
194 | this.scene.DeleteTerrain(); | ||
195 | } | ||
196 | } | ||
197 | |||
198 | public class PhysXCharacter : PhysicsActor | ||
199 | { | ||
200 | private PhysicsVector _position; | ||
201 | private PhysicsVector _velocity; | ||
202 | private PhysicsVector _acceleration; | ||
203 | private NxCharacter _character; | ||
204 | private bool flying; | ||
205 | private float gravityAccel; | ||
206 | |||
207 | public PhysXCharacter(NxCharacter character) | ||
208 | { | ||
209 | _velocity = new PhysicsVector(); | ||
210 | _position = new PhysicsVector(); | ||
211 | _acceleration = new PhysicsVector(); | ||
212 | _character = character; | ||
213 | } | ||
214 | |||
215 | public override bool Flying | ||
216 | { | ||
217 | get | ||
218 | { | ||
219 | return flying; | ||
220 | } | ||
221 | set | ||
222 | { | ||
223 | flying = value; | ||
224 | } | ||
225 | } | ||
226 | |||
227 | public override PhysicsVector Position | ||
228 | { | ||
229 | get | ||
230 | { | ||
231 | return _position; | ||
232 | } | ||
233 | set | ||
234 | { | ||
235 | _position = value; | ||
236 | Vec3 ps = new Vec3(); | ||
237 | ps.X = value.X; | ||
238 | ps.Y = value.Y; | ||
239 | ps.Z = value.Z; | ||
240 | this._character.Position = ps; | ||
241 | } | ||
242 | } | ||
243 | |||
244 | public override PhysicsVector Velocity | ||
245 | { | ||
246 | get | ||
247 | { | ||
248 | return _velocity; | ||
249 | } | ||
250 | set | ||
251 | { | ||
252 | _velocity = value; | ||
253 | } | ||
254 | } | ||
255 | |||
256 | public override bool Kinematic | ||
257 | { | ||
258 | get | ||
259 | { | ||
260 | return false; | ||
261 | } | ||
262 | set | ||
263 | { | ||
264 | |||
265 | } | ||
266 | } | ||
267 | |||
268 | public override Axiom.MathLib.Quaternion Orientation | ||
269 | { | ||
270 | get | ||
271 | { | ||
272 | return Axiom.MathLib.Quaternion.Identity; | ||
273 | } | ||
274 | set | ||
275 | { | ||
276 | |||
277 | } | ||
278 | } | ||
279 | |||
280 | public override PhysicsVector Acceleration | ||
281 | { | ||
282 | get | ||
283 | { | ||
284 | return _acceleration; | ||
285 | } | ||
286 | |||
287 | } | ||
288 | public void SetAcceleration (PhysicsVector accel) | ||
289 | { | ||
290 | this._acceleration = accel; | ||
291 | } | ||
292 | |||
293 | public override void AddForce(PhysicsVector force) | ||
294 | { | ||
295 | |||
296 | } | ||
297 | |||
298 | public override void SetMomentum(PhysicsVector momentum) | ||
299 | { | ||
300 | |||
301 | } | ||
302 | |||
303 | public void Move(float timeStep) | ||
304 | { | ||
305 | Vec3 vec = new Vec3(); | ||
306 | vec.X = this._velocity.X * timeStep; | ||
307 | vec.Y = this._velocity.Y * timeStep; | ||
308 | if(flying) | ||
309 | { | ||
310 | vec.Z = ( this._velocity.Z) * timeStep; | ||
311 | } | ||
312 | else | ||
313 | { | ||
314 | gravityAccel+= -9.8f; | ||
315 | vec.Z = (gravityAccel + this._velocity.Z) * timeStep; | ||
316 | } | ||
317 | int res = this._character.Move(vec); | ||
318 | if(res == 1) | ||
319 | { | ||
320 | gravityAccel = 0; | ||
321 | } | ||
322 | } | ||
323 | |||
324 | public void UpdatePosition() | ||
325 | { | ||
326 | Vec3 vec = this._character.Position; | ||
327 | this._position.X = vec.X; | ||
328 | this._position.Y = vec.Y; | ||
329 | this._position.Z = vec.Z; | ||
330 | } | ||
331 | } | ||
332 | |||
333 | public class PhysXPrim : PhysicsActor | ||
334 | { | ||
335 | private PhysicsVector _position; | ||
336 | private PhysicsVector _velocity; | ||
337 | private PhysicsVector _acceleration; | ||
338 | private NxActor _prim; | ||
339 | |||
340 | public PhysXPrim(NxActor prim) | ||
341 | { | ||
342 | _velocity = new PhysicsVector(); | ||
343 | _position = new PhysicsVector(); | ||
344 | _acceleration = new PhysicsVector(); | ||
345 | _prim = prim; | ||
346 | } | ||
347 | public override bool Flying | ||
348 | { | ||
349 | get | ||
350 | { | ||
351 | return false; //no flying prims for you | ||
352 | } | ||
353 | set | ||
354 | { | ||
355 | |||
356 | } | ||
357 | } | ||
358 | public override PhysicsVector Position | ||
359 | { | ||
360 | get | ||
361 | { | ||
362 | PhysicsVector pos = new PhysicsVector(); | ||
363 | Vec3 vec = this._prim.Position; | ||
364 | pos.X = vec.X; | ||
365 | pos.Y = vec.Y; | ||
366 | pos.Z = vec.Z; | ||
367 | return pos; | ||
368 | |||
369 | } | ||
370 | set | ||
371 | { | ||
372 | PhysicsVector vec = value; | ||
373 | Vec3 pos = new Vec3(); | ||
374 | pos.X = vec.X; | ||
375 | pos.Y = vec.Y; | ||
376 | pos.Z = vec.Z; | ||
377 | this._prim.Position = pos; | ||
378 | } | ||
379 | } | ||
380 | |||
381 | public override PhysicsVector Velocity | ||
382 | { | ||
383 | get | ||
384 | { | ||
385 | return _velocity; | ||
386 | } | ||
387 | set | ||
388 | { | ||
389 | _velocity = value; | ||
390 | } | ||
391 | } | ||
392 | |||
393 | public override bool Kinematic | ||
394 | { | ||
395 | get | ||
396 | { | ||
397 | return this._prim.Kinematic; | ||
398 | } | ||
399 | set | ||
400 | { | ||
401 | this._prim.Kinematic = value; | ||
402 | } | ||
403 | } | ||
404 | |||
405 | public override Axiom.MathLib.Quaternion Orientation | ||
406 | { | ||
407 | get | ||
408 | { | ||
409 | Axiom.MathLib.Quaternion res = new Axiom.MathLib.Quaternion(); | ||
410 | PhysXWrapper.Quaternion quat = this._prim.GetOrientation(); | ||
411 | res.w = quat.W; | ||
412 | res.x = quat.X; | ||
413 | res.y = quat.Y; | ||
414 | res.z = quat.Z; | ||
415 | return res; | ||
416 | } | ||
417 | set | ||
418 | { | ||
419 | |||
420 | } | ||
421 | } | ||
422 | |||
423 | public override PhysicsVector Acceleration | ||
424 | { | ||
425 | get | ||
426 | { | ||
427 | return _acceleration; | ||
428 | } | ||
429 | |||
430 | } | ||
431 | public void SetAcceleration (PhysicsVector accel) | ||
432 | { | ||
433 | this._acceleration = accel; | ||
434 | } | ||
435 | |||
436 | public override void AddForce(PhysicsVector force) | ||
437 | { | ||
438 | |||
439 | } | ||
440 | |||
441 | public override void SetMomentum(PhysicsVector momentum) | ||
442 | { | ||
443 | |||
444 | } | ||
445 | |||
446 | |||
447 | } | ||
448 | |||
449 | } | ||