diff options
Diffstat (limited to 'OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs')
-rw-r--r-- | OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs | 422 |
1 files changed, 422 insertions, 0 deletions
diff --git a/OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs b/OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs new file mode 100644 index 0000000..043c2f1 --- /dev/null +++ b/OpenSim.Physics/PhysXPlugin/PhysXPlugin.cs | |||
@@ -0,0 +1,422 @@ | |||
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; | ||
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 PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size) | ||
126 | { | ||
127 | Vec3 pos = new Vec3(); | ||
128 | pos.X = position.X; | ||
129 | pos.Y = position.Y; | ||
130 | pos.Z = position.Z; | ||
131 | Vec3 siz = new Vec3(); | ||
132 | siz.X = size.X; | ||
133 | siz.Y = size.Y; | ||
134 | siz.Z = size.Z; | ||
135 | PhysXPrim act = new PhysXPrim( scene.AddNewBox(pos, siz)); | ||
136 | _prims.Add(act); | ||
137 | return act; | ||
138 | } | ||
139 | public override void Simulate(float timeStep) | ||
140 | { | ||
141 | foreach (PhysXCharacter actor in _characters) | ||
142 | { | ||
143 | actor.Move(timeStep); | ||
144 | } | ||
145 | scene.Simulate(timeStep); | ||
146 | scene.FetchResults(); | ||
147 | scene.UpdateControllers(); | ||
148 | |||
149 | foreach (PhysXCharacter actor in _characters) | ||
150 | { | ||
151 | actor.UpdatePosition(); | ||
152 | } | ||
153 | |||
154 | } | ||
155 | |||
156 | public override void GetResults() | ||
157 | { | ||
158 | |||
159 | } | ||
160 | |||
161 | public override bool IsThreaded | ||
162 | { | ||
163 | get | ||
164 | { | ||
165 | return(false); // for now we won't be multithreaded | ||
166 | } | ||
167 | } | ||
168 | |||
169 | public override void SetTerrain(float[] heightMap) | ||
170 | { | ||
171 | this._heightMap = heightMap; | ||
172 | this.scene.AddTerrain(heightMap); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | public class PhysXCharacter : PhysicsActor | ||
177 | { | ||
178 | private PhysicsVector _position; | ||
179 | private PhysicsVector _velocity; | ||
180 | private PhysicsVector _acceleration; | ||
181 | private NxCharacter _character; | ||
182 | private bool flying; | ||
183 | private float gravityAccel; | ||
184 | |||
185 | public PhysXCharacter(NxCharacter character) | ||
186 | { | ||
187 | _velocity = new PhysicsVector(); | ||
188 | _position = new PhysicsVector(); | ||
189 | _acceleration = new PhysicsVector(); | ||
190 | _character = character; | ||
191 | } | ||
192 | |||
193 | public override bool Flying | ||
194 | { | ||
195 | get | ||
196 | { | ||
197 | return flying; | ||
198 | } | ||
199 | set | ||
200 | { | ||
201 | flying = value; | ||
202 | } | ||
203 | } | ||
204 | |||
205 | public override PhysicsVector Position | ||
206 | { | ||
207 | get | ||
208 | { | ||
209 | return _position; | ||
210 | } | ||
211 | set | ||
212 | { | ||
213 | _position = value; | ||
214 | } | ||
215 | } | ||
216 | |||
217 | public override PhysicsVector Velocity | ||
218 | { | ||
219 | get | ||
220 | { | ||
221 | return _velocity; | ||
222 | } | ||
223 | set | ||
224 | { | ||
225 | _velocity = value; | ||
226 | } | ||
227 | } | ||
228 | |||
229 | public override bool Kinematic | ||
230 | { | ||
231 | get | ||
232 | { | ||
233 | return false; | ||
234 | } | ||
235 | set | ||
236 | { | ||
237 | |||
238 | } | ||
239 | } | ||
240 | |||
241 | public override Axiom.MathLib.Quaternion Orientation | ||
242 | { | ||
243 | get | ||
244 | { | ||
245 | return Axiom.MathLib.Quaternion.Identity; | ||
246 | } | ||
247 | set | ||
248 | { | ||
249 | |||
250 | } | ||
251 | } | ||
252 | |||
253 | public override PhysicsVector Acceleration | ||
254 | { | ||
255 | get | ||
256 | { | ||
257 | return _acceleration; | ||
258 | } | ||
259 | |||
260 | } | ||
261 | public void SetAcceleration (PhysicsVector accel) | ||
262 | { | ||
263 | this._acceleration = accel; | ||
264 | } | ||
265 | |||
266 | public override void AddForce(PhysicsVector force) | ||
267 | { | ||
268 | |||
269 | } | ||
270 | |||
271 | public override void SetMomentum(PhysicsVector momentum) | ||
272 | { | ||
273 | |||
274 | } | ||
275 | |||
276 | public void Move(float timeStep) | ||
277 | { | ||
278 | Vec3 vec = new Vec3(); | ||
279 | vec.X = this._velocity.X * timeStep; | ||
280 | vec.Y = this._velocity.Y * timeStep; | ||
281 | if(flying) | ||
282 | { | ||
283 | vec.Z = ( this._velocity.Z) * timeStep; | ||
284 | } | ||
285 | else | ||
286 | { | ||
287 | gravityAccel+= -9.8f; | ||
288 | vec.Z = (gravityAccel + this._velocity.Z) * timeStep; | ||
289 | } | ||
290 | int res = this._character.Move(vec); | ||
291 | if(res == 1) | ||
292 | { | ||
293 | gravityAccel = 0; | ||
294 | } | ||
295 | } | ||
296 | |||
297 | public void UpdatePosition() | ||
298 | { | ||
299 | Vec3 vec = this._character.Position; | ||
300 | this._position.X = vec.X; | ||
301 | this._position.Y = vec.Y; | ||
302 | this._position.Z = vec.Z; | ||
303 | } | ||
304 | } | ||
305 | |||
306 | public class PhysXPrim : PhysicsActor | ||
307 | { | ||
308 | private PhysicsVector _position; | ||
309 | private PhysicsVector _velocity; | ||
310 | private PhysicsVector _acceleration; | ||
311 | private NxActor _prim; | ||
312 | |||
313 | public PhysXPrim(NxActor prim) | ||
314 | { | ||
315 | _velocity = new PhysicsVector(); | ||
316 | _position = new PhysicsVector(); | ||
317 | _acceleration = new PhysicsVector(); | ||
318 | _prim = prim; | ||
319 | } | ||
320 | public override bool Flying | ||
321 | { | ||
322 | get | ||
323 | { | ||
324 | return false; //no flying prims for you | ||
325 | } | ||
326 | set | ||
327 | { | ||
328 | |||
329 | } | ||
330 | } | ||
331 | public override PhysicsVector Position | ||
332 | { | ||
333 | get | ||
334 | { | ||
335 | PhysicsVector pos = new PhysicsVector(); | ||
336 | Vec3 vec = this._prim.Position; | ||
337 | pos.X = vec.X; | ||
338 | pos.Y = vec.Y; | ||
339 | pos.Z = vec.Z; | ||
340 | return pos; | ||
341 | |||
342 | } | ||
343 | set | ||
344 | { | ||
345 | PhysicsVector vec = value; | ||
346 | Vec3 pos = new Vec3(); | ||
347 | pos.X = vec.X; | ||
348 | pos.Y = vec.Y; | ||
349 | pos.Z = vec.Z; | ||
350 | this._prim.Position = pos; | ||
351 | } | ||
352 | } | ||
353 | |||
354 | public override PhysicsVector Velocity | ||
355 | { | ||
356 | get | ||
357 | { | ||
358 | return _velocity; | ||
359 | } | ||
360 | set | ||
361 | { | ||
362 | _velocity = value; | ||
363 | } | ||
364 | } | ||
365 | |||
366 | public override bool Kinematic | ||
367 | { | ||
368 | get | ||
369 | { | ||
370 | return this._prim.Kinematic; | ||
371 | } | ||
372 | set | ||
373 | { | ||
374 | this._prim.Kinematic = value; | ||
375 | } | ||
376 | } | ||
377 | |||
378 | public override Axiom.MathLib.Quaternion Orientation | ||
379 | { | ||
380 | get | ||
381 | { | ||
382 | Axiom.MathLib.Quaternion res = new Axiom.MathLib.Quaternion(); | ||
383 | PhysXWrapper.Quaternion quat = this._prim.GetOrientation(); | ||
384 | res.w = quat.W; | ||
385 | res.x = quat.X; | ||
386 | res.y = quat.Y; | ||
387 | res.z = quat.Z; | ||
388 | return res; | ||
389 | } | ||
390 | set | ||
391 | { | ||
392 | |||
393 | } | ||
394 | } | ||
395 | |||
396 | public override PhysicsVector Acceleration | ||
397 | { | ||
398 | get | ||
399 | { | ||
400 | return _acceleration; | ||
401 | } | ||
402 | |||
403 | } | ||
404 | public void SetAcceleration (PhysicsVector accel) | ||
405 | { | ||
406 | this._acceleration = accel; | ||
407 | } | ||
408 | |||
409 | public override void AddForce(PhysicsVector force) | ||
410 | { | ||
411 | |||
412 | } | ||
413 | |||
414 | public override void SetMomentum(PhysicsVector momentum) | ||
415 | { | ||
416 | |||
417 | } | ||
418 | |||
419 | |||
420 | } | ||
421 | |||
422 | } | ||