diff options
Diffstat (limited to 'OpenSim/Region/Physics/PhysXPlugin/PhysXPlugin.cs')
-rw-r--r-- | OpenSim/Region/Physics/PhysXPlugin/PhysXPlugin.cs | 425 |
1 files changed, 425 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..4c82549 --- /dev/null +++ b/OpenSim/Region/Physics/PhysXPlugin/PhysXPlugin.cs | |||
@@ -0,0 +1,425 @@ | |||
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 | using Quaternion=Axiom.Math.Quaternion; | ||
33 | |||
34 | namespace OpenSim.Region.Physics.PhysXPlugin | ||
35 | { | ||
36 | /// <summary> | ||
37 | /// Will be the PhysX plugin but for now will be a very basic physics engine | ||
38 | /// </summary> | ||
39 | public class PhysXPlugin : IPhysicsPlugin | ||
40 | { | ||
41 | private PhysXScene _mScene; | ||
42 | |||
43 | public PhysXPlugin() | ||
44 | { | ||
45 | |||
46 | } | ||
47 | |||
48 | public bool Init() | ||
49 | { | ||
50 | return true; | ||
51 | } | ||
52 | |||
53 | public PhysicsScene GetScene() | ||
54 | { | ||
55 | if(_mScene == null) | ||
56 | { | ||
57 | _mScene = new PhysXScene(); | ||
58 | } | ||
59 | return(_mScene); | ||
60 | } | ||
61 | |||
62 | public string GetName() | ||
63 | { | ||
64 | return("RealPhysX"); | ||
65 | } | ||
66 | |||
67 | public void Dispose() | ||
68 | { | ||
69 | |||
70 | } | ||
71 | } | ||
72 | |||
73 | public class PhysXScene :PhysicsScene | ||
74 | { | ||
75 | private List<PhysXCharacter> _characters = new List<PhysXCharacter>(); | ||
76 | private List<PhysXPrim> _prims = new List<PhysXPrim>(); | ||
77 | private float[] _heightMap = null; | ||
78 | private NxPhysicsSDK mySdk; | ||
79 | private NxScene scene; | ||
80 | |||
81 | public PhysXScene() | ||
82 | { | ||
83 | mySdk = NxPhysicsSDK.CreateSDK(); | ||
84 | Console.WriteLine("Sdk created - now creating scene"); | ||
85 | scene = mySdk.CreateScene(); | ||
86 | |||
87 | } | ||
88 | |||
89 | public override PhysicsActor AddAvatar(PhysicsVector position) | ||
90 | { | ||
91 | Vec3 pos = new Vec3(); | ||
92 | pos.X = position.X; | ||
93 | pos.Y = position.Y; | ||
94 | pos.Z = position.Z; | ||
95 | PhysXCharacter act = new PhysXCharacter( scene.AddCharacter(pos)); | ||
96 | act.Position = position; | ||
97 | _characters.Add(act); | ||
98 | return act; | ||
99 | } | ||
100 | |||
101 | public override void RemoveAvatar(PhysicsActor actor) | ||
102 | { | ||
103 | |||
104 | } | ||
105 | |||
106 | public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size) | ||
107 | { | ||
108 | Vec3 pos = new Vec3(); | ||
109 | pos.X = position.X; | ||
110 | pos.Y = position.Y; | ||
111 | pos.Z = position.Z; | ||
112 | Vec3 siz = new Vec3(); | ||
113 | siz.X = size.X; | ||
114 | siz.Y = size.Y; | ||
115 | siz.Z = size.Z; | ||
116 | PhysXPrim act = new PhysXPrim( scene.AddNewBox(pos, siz)); | ||
117 | _prims.Add(act); | ||
118 | return act; | ||
119 | } | ||
120 | public override void Simulate(float timeStep) | ||
121 | { | ||
122 | try | ||
123 | { | ||
124 | foreach (PhysXCharacter actor in _characters) | ||
125 | { | ||
126 | actor.Move(timeStep); | ||
127 | } | ||
128 | scene.Simulate(timeStep); | ||
129 | scene.FetchResults(); | ||
130 | scene.UpdateControllers(); | ||
131 | |||
132 | foreach (PhysXCharacter actor in _characters) | ||
133 | { | ||
134 | actor.UpdatePosition(); | ||
135 | } | ||
136 | } | ||
137 | catch (Exception e) | ||
138 | { | ||
139 | Console.WriteLine(e.Message); | ||
140 | } | ||
141 | |||
142 | } | ||
143 | |||
144 | public override void GetResults() | ||
145 | { | ||
146 | |||
147 | } | ||
148 | |||
149 | public override bool IsThreaded | ||
150 | { | ||
151 | get | ||
152 | { | ||
153 | return(false); // for now we won't be multithreaded | ||
154 | } | ||
155 | } | ||
156 | |||
157 | public override void SetTerrain(float[] heightMap) | ||
158 | { | ||
159 | if (this._heightMap != null) | ||
160 | { | ||
161 | Console.WriteLine("PhysX - deleting old terrain"); | ||
162 | this.scene.DeleteTerrain(); | ||
163 | } | ||
164 | this._heightMap = heightMap; | ||
165 | this.scene.AddTerrain(heightMap); | ||
166 | } | ||
167 | |||
168 | public override void DeleteTerrain() | ||
169 | { | ||
170 | this.scene.DeleteTerrain(); | ||
171 | } | ||
172 | } | ||
173 | |||
174 | public class PhysXCharacter : PhysicsActor | ||
175 | { | ||
176 | private PhysicsVector _position; | ||
177 | private PhysicsVector _velocity; | ||
178 | private PhysicsVector _acceleration; | ||
179 | private NxCharacter _character; | ||
180 | private bool flying; | ||
181 | private float gravityAccel; | ||
182 | |||
183 | public PhysXCharacter(NxCharacter character) | ||
184 | { | ||
185 | _velocity = new PhysicsVector(); | ||
186 | _position = new PhysicsVector(); | ||
187 | _acceleration = new PhysicsVector(); | ||
188 | _character = character; | ||
189 | } | ||
190 | |||
191 | public override bool Flying | ||
192 | { | ||
193 | get | ||
194 | { | ||
195 | return flying; | ||
196 | } | ||
197 | set | ||
198 | { | ||
199 | flying = value; | ||
200 | } | ||
201 | } | ||
202 | |||
203 | public override PhysicsVector Position | ||
204 | { | ||
205 | get | ||
206 | { | ||
207 | return _position; | ||
208 | } | ||
209 | set | ||
210 | { | ||
211 | _position = value; | ||
212 | Vec3 ps = new Vec3(); | ||
213 | ps.X = value.X; | ||
214 | ps.Y = value.Y; | ||
215 | ps.Z = value.Z; | ||
216 | this._character.Position = ps; | ||
217 | } | ||
218 | } | ||
219 | |||
220 | public override PhysicsVector Velocity | ||
221 | { | ||
222 | get | ||
223 | { | ||
224 | return _velocity; | ||
225 | } | ||
226 | set | ||
227 | { | ||
228 | _velocity = value; | ||
229 | } | ||
230 | } | ||
231 | |||
232 | public override bool Kinematic | ||
233 | { | ||
234 | get | ||
235 | { | ||
236 | return false; | ||
237 | } | ||
238 | set | ||
239 | { | ||
240 | |||
241 | } | ||
242 | } | ||
243 | |||
244 | public override Quaternion Orientation | ||
245 | { | ||
246 | get | ||
247 | { | ||
248 | return Quaternion.Identity; | ||
249 | } | ||
250 | set | ||
251 | { | ||
252 | |||
253 | } | ||
254 | } | ||
255 | |||
256 | public override PhysicsVector Acceleration | ||
257 | { | ||
258 | get | ||
259 | { | ||
260 | return _acceleration; | ||
261 | } | ||
262 | |||
263 | } | ||
264 | public void SetAcceleration (PhysicsVector accel) | ||
265 | { | ||
266 | this._acceleration = accel; | ||
267 | } | ||
268 | |||
269 | public override void AddForce(PhysicsVector force) | ||
270 | { | ||
271 | |||
272 | } | ||
273 | |||
274 | public override void SetMomentum(PhysicsVector momentum) | ||
275 | { | ||
276 | |||
277 | } | ||
278 | |||
279 | public void Move(float timeStep) | ||
280 | { | ||
281 | Vec3 vec = new Vec3(); | ||
282 | vec.X = this._velocity.X * timeStep; | ||
283 | vec.Y = this._velocity.Y * timeStep; | ||
284 | if(flying) | ||
285 | { | ||
286 | vec.Z = ( this._velocity.Z) * timeStep; | ||
287 | } | ||
288 | else | ||
289 | { | ||
290 | gravityAccel+= -9.8f; | ||
291 | vec.Z = (gravityAccel + this._velocity.Z) * timeStep; | ||
292 | } | ||
293 | int res = this._character.Move(vec); | ||
294 | if(res == 1) | ||
295 | { | ||
296 | gravityAccel = 0; | ||
297 | } | ||
298 | } | ||
299 | |||
300 | public void UpdatePosition() | ||
301 | { | ||
302 | Vec3 vec = this._character.Position; | ||
303 | this._position.X = vec.X; | ||
304 | this._position.Y = vec.Y; | ||
305 | this._position.Z = vec.Z; | ||
306 | } | ||
307 | } | ||
308 | |||
309 | public class PhysXPrim : PhysicsActor | ||
310 | { | ||
311 | private PhysicsVector _position; | ||
312 | private PhysicsVector _velocity; | ||
313 | private PhysicsVector _acceleration; | ||
314 | private NxActor _prim; | ||
315 | |||
316 | public PhysXPrim(NxActor prim) | ||
317 | { | ||
318 | _velocity = new PhysicsVector(); | ||
319 | _position = new PhysicsVector(); | ||
320 | _acceleration = new PhysicsVector(); | ||
321 | _prim = prim; | ||
322 | } | ||
323 | public override bool Flying | ||
324 | { | ||
325 | get | ||
326 | { | ||
327 | return false; //no flying prims for you | ||
328 | } | ||
329 | set | ||
330 | { | ||
331 | |||
332 | } | ||
333 | } | ||
334 | public override PhysicsVector Position | ||
335 | { | ||
336 | get | ||
337 | { | ||
338 | PhysicsVector pos = new PhysicsVector(); | ||
339 | Vec3 vec = this._prim.Position; | ||
340 | pos.X = vec.X; | ||
341 | pos.Y = vec.Y; | ||
342 | pos.Z = vec.Z; | ||
343 | return pos; | ||
344 | |||
345 | } | ||
346 | set | ||
347 | { | ||
348 | PhysicsVector vec = value; | ||
349 | Vec3 pos = new Vec3(); | ||
350 | pos.X = vec.X; | ||
351 | pos.Y = vec.Y; | ||
352 | pos.Z = vec.Z; | ||
353 | this._prim.Position = pos; | ||
354 | } | ||
355 | } | ||
356 | |||
357 | public override PhysicsVector Velocity | ||
358 | { | ||
359 | get | ||
360 | { | ||
361 | return _velocity; | ||
362 | } | ||
363 | set | ||
364 | { | ||
365 | _velocity = value; | ||
366 | } | ||
367 | } | ||
368 | |||
369 | public override bool Kinematic | ||
370 | { | ||
371 | get | ||
372 | { | ||
373 | return this._prim.Kinematic; | ||
374 | } | ||
375 | set | ||
376 | { | ||
377 | this._prim.Kinematic = value; | ||
378 | } | ||
379 | } | ||
380 | |||
381 | public override Quaternion Orientation | ||
382 | { | ||
383 | get | ||
384 | { | ||
385 | Quaternion res = new Quaternion(); | ||
386 | PhysXWrapper.Quaternion quat = this._prim.GetOrientation(); | ||
387 | res.w = quat.W; | ||
388 | res.x = quat.X; | ||
389 | res.y = quat.Y; | ||
390 | res.z = quat.Z; | ||
391 | return res; | ||
392 | } | ||
393 | set | ||
394 | { | ||
395 | |||
396 | } | ||
397 | } | ||
398 | |||
399 | public override PhysicsVector Acceleration | ||
400 | { | ||
401 | get | ||
402 | { | ||
403 | return _acceleration; | ||
404 | } | ||
405 | |||
406 | } | ||
407 | public void SetAcceleration (PhysicsVector accel) | ||
408 | { | ||
409 | this._acceleration = accel; | ||
410 | } | ||
411 | |||
412 | public override void AddForce(PhysicsVector force) | ||
413 | { | ||
414 | |||
415 | } | ||
416 | |||
417 | public override void SetMomentum(PhysicsVector momentum) | ||
418 | { | ||
419 | |||
420 | } | ||
421 | |||
422 | |||
423 | } | ||
424 | |||
425 | } | ||