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