diff options
Diffstat (limited to 'OpenSim/Region/Physics/OdePlugin/ODEDynamics.cs')
-rw-r--r-- | OpenSim/Region/Physics/OdePlugin/ODEDynamics.cs | 658 |
1 files changed, 658 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/OdePlugin/ODEDynamics.cs b/OpenSim/Region/Physics/OdePlugin/ODEDynamics.cs new file mode 100644 index 0000000..467eba0 --- /dev/null +++ b/OpenSim/Region/Physics/OdePlugin/ODEDynamics.cs | |||
@@ -0,0 +1,658 @@ | |||
1 | /* | ||
2 | * Revised Aug, Sept 2009 by Kitto Flora. ODEDynamics.cs replaces | ||
3 | * ODEVehicleSettings.cs. It and ODEPrim.cs are re-organised: | ||
4 | * ODEPrim.cs contains methods dealing with Prim editing, Prim | ||
5 | * characteristics and Kinetic motion. | ||
6 | * ODEDynamics.cs contains methods dealing with Prim Physical motion | ||
7 | * (dynamics) and the associated settings. Old Linear and angular | ||
8 | * motors for dynamic motion have been replace with MoveLinear() | ||
9 | * and MoveAngular(); 'Physical' is used only to switch ODE dynamic | ||
10 | * simualtion on/off; VEHICAL_TYPE_NONE/VEHICAL_TYPE_<other> is to | ||
11 | * switch between 'VEHICLE' parameter use and general dynamics | ||
12 | * settings use. | ||
13 | * | ||
14 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
15 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
16 | * | ||
17 | * Redistribution and use in source and binary forms, with or without | ||
18 | * modification, are permitted provided that the following conditions are met: | ||
19 | * * Redistributions of source code must retain the above copyright | ||
20 | * notice, this list of conditions and the following disclaimer. | ||
21 | * * Redistributions in binary form must reproduce the above copyright | ||
22 | * notice, this list of conditions and the following disclaimer in the | ||
23 | * documentation and/or other materials provided with the distribution. | ||
24 | * * Neither the name of the OpenSimulator Project nor the | ||
25 | * names of its contributors may be used to endorse or promote products | ||
26 | * derived from this software without specific prior written permission. | ||
27 | * | ||
28 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
29 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
30 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
31 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
32 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
33 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
35 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
36 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
37 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
38 | */ | ||
39 | |||
40 | using System; | ||
41 | using System.Collections.Generic; | ||
42 | using System.Reflection; | ||
43 | using System.Runtime.InteropServices; | ||
44 | using log4net; | ||
45 | using OpenMetaverse; | ||
46 | using Ode.NET; | ||
47 | using OpenSim.Framework; | ||
48 | using OpenSim.Region.Physics.Manager; | ||
49 | |||
50 | namespace OpenSim.Region.Physics.OdePlugin | ||
51 | { | ||
52 | public class ODEDynamics | ||
53 | { | ||
54 | public Vehicle Type | ||
55 | { | ||
56 | get { return m_type; } | ||
57 | } | ||
58 | |||
59 | public IntPtr Body | ||
60 | { | ||
61 | get { return m_body; } | ||
62 | } | ||
63 | |||
64 | private int frcount = 0; // Used to limit dynamics debug output to | ||
65 | // every 100th frame | ||
66 | |||
67 | // private OdeScene m_parentScene = null; | ||
68 | private IntPtr m_body = IntPtr.Zero; | ||
69 | private IntPtr m_jointGroup = IntPtr.Zero; | ||
70 | private IntPtr m_aMotor = IntPtr.Zero; | ||
71 | |||
72 | |||
73 | // Vehicle properties | ||
74 | private Vehicle m_type = Vehicle.TYPE_NONE; // If a 'VEHICLE', and what kind | ||
75 | // private Quaternion m_referenceFrame = Quaternion.Identity; // Axis modifier | ||
76 | private VehicleFlag m_flags = (VehicleFlag) 0; // Boolean settings: | ||
77 | // HOVER_TERRAIN_ONLY | ||
78 | // HOVER_GLOBAL_HEIGHT | ||
79 | // NO_DEFLECTION_UP | ||
80 | // HOVER_WATER_ONLY | ||
81 | // HOVER_UP_ONLY | ||
82 | // LIMIT_MOTOR_UP | ||
83 | // LIMIT_ROLL_ONLY | ||
84 | |||
85 | // Linear properties | ||
86 | private Vector3 m_linearMotorDirection = Vector3.Zero; // velocity requested by LSL, decayed by time | ||
87 | private Vector3 m_linearMotorDirectionLASTSET = Vector3.Zero; // velocity requested by LSL | ||
88 | private Vector3 m_dir = Vector3.Zero; // velocity applied to body | ||
89 | private Vector3 m_linearFrictionTimescale = Vector3.Zero; | ||
90 | private float m_linearMotorDecayTimescale = 0; | ||
91 | private float m_linearMotorTimescale = 0; | ||
92 | private Vector3 m_lastLinearVelocityVector = Vector3.Zero; | ||
93 | // private bool m_LinearMotorSetLastFrame = false; | ||
94 | // private Vector3 m_linearMotorOffset = Vector3.Zero; | ||
95 | |||
96 | //Angular properties | ||
97 | private Vector3 m_angularMotorDirection = Vector3.Zero; // angular velocity requested by LSL motor | ||
98 | private int m_angularMotorApply = 0; // application frame counter | ||
99 | private Vector3 m_angularMotorVelocity = Vector3.Zero; // current angular motor velocity | ||
100 | private float m_angularMotorTimescale = 0; // motor angular velocity ramp up rate | ||
101 | private float m_angularMotorDecayTimescale = 0; // motor angular velocity decay rate | ||
102 | private Vector3 m_angularFrictionTimescale = Vector3.Zero; // body angular velocity decay rate | ||
103 | private Vector3 m_lastAngularVelocity = Vector3.Zero; // what was last applied to body | ||
104 | // private Vector3 m_lastVertAttractor = Vector3.Zero; // what VA was last applied to body | ||
105 | |||
106 | //Deflection properties | ||
107 | // private float m_angularDeflectionEfficiency = 0; | ||
108 | // private float m_angularDeflectionTimescale = 0; | ||
109 | // private float m_linearDeflectionEfficiency = 0; | ||
110 | // private float m_linearDeflectionTimescale = 0; | ||
111 | |||
112 | //Banking properties | ||
113 | // private float m_bankingEfficiency = 0; | ||
114 | // private float m_bankingMix = 0; | ||
115 | // private float m_bankingTimescale = 0; | ||
116 | |||
117 | //Hover and Buoyancy properties | ||
118 | private float m_VhoverHeight = 0f; | ||
119 | private float m_VhoverEfficiency = 0f; | ||
120 | private float m_VhoverTimescale = 0f; | ||
121 | private float m_VhoverTargetHeight = -1.0f; // if <0 then no hover, else its the current target height | ||
122 | private float m_VehicleBuoyancy = 0f; //KF: m_VehicleBuoyancy is set by VEHICLE_BUOYANCY for a vehicle. | ||
123 | // Modifies gravity. Slider between -1 (double-gravity) and 1 (full anti-gravity) | ||
124 | // KF: So far I have found no good method to combine a script-requested .Z velocity and gravity. | ||
125 | // Therefore only m_VehicleBuoyancy=1 (0g) will use the script-requested .Z velocity. | ||
126 | |||
127 | //Attractor properties | ||
128 | private float m_verticalAttractionEfficiency = 1.0f; // damped | ||
129 | private float m_verticalAttractionTimescale = 500f; // Timescale > 300 means no vert attractor. | ||
130 | |||
131 | |||
132 | |||
133 | |||
134 | |||
135 | internal void ProcessFloatVehicleParam(Vehicle pParam, float pValue) | ||
136 | { | ||
137 | switch (pParam) | ||
138 | { | ||
139 | case Vehicle.ANGULAR_DEFLECTION_EFFICIENCY: | ||
140 | if (pValue < 0.01f) pValue = 0.01f; | ||
141 | // m_angularDeflectionEfficiency = pValue; | ||
142 | break; | ||
143 | case Vehicle.ANGULAR_DEFLECTION_TIMESCALE: | ||
144 | if (pValue < 0.01f) pValue = 0.01f; | ||
145 | // m_angularDeflectionTimescale = pValue; | ||
146 | break; | ||
147 | case Vehicle.ANGULAR_MOTOR_DECAY_TIMESCALE: | ||
148 | if (pValue < 0.01f) pValue = 0.01f; | ||
149 | m_angularMotorDecayTimescale = pValue; | ||
150 | break; | ||
151 | case Vehicle.ANGULAR_MOTOR_TIMESCALE: | ||
152 | if (pValue < 0.01f) pValue = 0.01f; | ||
153 | m_angularMotorTimescale = pValue; | ||
154 | break; | ||
155 | case Vehicle.BANKING_EFFICIENCY: | ||
156 | if (pValue < 0.01f) pValue = 0.01f; | ||
157 | // m_bankingEfficiency = pValue; | ||
158 | break; | ||
159 | case Vehicle.BANKING_MIX: | ||
160 | if (pValue < 0.01f) pValue = 0.01f; | ||
161 | // m_bankingMix = pValue; | ||
162 | break; | ||
163 | case Vehicle.BANKING_TIMESCALE: | ||
164 | if (pValue < 0.01f) pValue = 0.01f; | ||
165 | // m_bankingTimescale = pValue; | ||
166 | break; | ||
167 | case Vehicle.BUOYANCY: | ||
168 | if (pValue < -1f) pValue = -1f; | ||
169 | if (pValue > 1f) pValue = 1f; | ||
170 | m_VehicleBuoyancy = pValue; | ||
171 | break; | ||
172 | case Vehicle.HOVER_EFFICIENCY: | ||
173 | if (pValue < 0f) pValue = 0f; | ||
174 | if (pValue > 1f) pValue = 1f; | ||
175 | m_VhoverEfficiency = pValue; | ||
176 | break; | ||
177 | case Vehicle.HOVER_HEIGHT: | ||
178 | m_VhoverHeight = pValue; | ||
179 | break; | ||
180 | case Vehicle.HOVER_TIMESCALE: | ||
181 | if (pValue < 0.01f) pValue = 0.01f; | ||
182 | m_VhoverTimescale = pValue; | ||
183 | break; | ||
184 | case Vehicle.LINEAR_DEFLECTION_EFFICIENCY: | ||
185 | if (pValue < 0.01f) pValue = 0.01f; | ||
186 | // m_linearDeflectionEfficiency = pValue; | ||
187 | break; | ||
188 | case Vehicle.LINEAR_DEFLECTION_TIMESCALE: | ||
189 | if (pValue < 0.01f) pValue = 0.01f; | ||
190 | // m_linearDeflectionTimescale = pValue; | ||
191 | break; | ||
192 | case Vehicle.LINEAR_MOTOR_DECAY_TIMESCALE: | ||
193 | if (pValue < 0.01f) pValue = 0.01f; | ||
194 | m_linearMotorDecayTimescale = pValue; | ||
195 | break; | ||
196 | case Vehicle.LINEAR_MOTOR_TIMESCALE: | ||
197 | if (pValue < 0.01f) pValue = 0.01f; | ||
198 | m_linearMotorTimescale = pValue; | ||
199 | break; | ||
200 | case Vehicle.VERTICAL_ATTRACTION_EFFICIENCY: | ||
201 | if (pValue < 0.1f) pValue = 0.1f; // Less goes unstable | ||
202 | if (pValue > 1.0f) pValue = 1.0f; | ||
203 | m_verticalAttractionEfficiency = pValue; | ||
204 | break; | ||
205 | case Vehicle.VERTICAL_ATTRACTION_TIMESCALE: | ||
206 | if (pValue < 0.01f) pValue = 0.01f; | ||
207 | m_verticalAttractionTimescale = pValue; | ||
208 | break; | ||
209 | |||
210 | // These are vector properties but the engine lets you use a single float value to | ||
211 | // set all of the components to the same value | ||
212 | case Vehicle.ANGULAR_FRICTION_TIMESCALE: | ||
213 | m_angularFrictionTimescale = new Vector3(pValue, pValue, pValue); | ||
214 | break; | ||
215 | case Vehicle.ANGULAR_MOTOR_DIRECTION: | ||
216 | m_angularMotorDirection = new Vector3(pValue, pValue, pValue); | ||
217 | m_angularMotorApply = 10; | ||
218 | break; | ||
219 | case Vehicle.LINEAR_FRICTION_TIMESCALE: | ||
220 | m_linearFrictionTimescale = new Vector3(pValue, pValue, pValue); | ||
221 | break; | ||
222 | case Vehicle.LINEAR_MOTOR_DIRECTION: | ||
223 | m_linearMotorDirection = new Vector3(pValue, pValue, pValue); | ||
224 | m_linearMotorDirectionLASTSET = new Vector3(pValue, pValue, pValue); | ||
225 | break; | ||
226 | case Vehicle.LINEAR_MOTOR_OFFSET: | ||
227 | // m_linearMotorOffset = new Vector3(pValue, pValue, pValue); | ||
228 | break; | ||
229 | |||
230 | } | ||
231 | |||
232 | }//end ProcessFloatVehicleParam | ||
233 | |||
234 | internal void ProcessVectorVehicleParam(Vehicle pParam, PhysicsVector pValue) | ||
235 | { | ||
236 | switch (pParam) | ||
237 | { | ||
238 | case Vehicle.ANGULAR_FRICTION_TIMESCALE: | ||
239 | m_angularFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z); | ||
240 | break; | ||
241 | case Vehicle.ANGULAR_MOTOR_DIRECTION: | ||
242 | m_angularMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z); | ||
243 | // Limit requested angular speed to 2 rps= 4 pi rads/sec | ||
244 | if(m_angularMotorDirection.X > 12.56f) m_angularMotorDirection.X = 12.56f; | ||
245 | if(m_angularMotorDirection.X < - 12.56f) m_angularMotorDirection.X = - 12.56f; | ||
246 | if(m_angularMotorDirection.Y > 12.56f) m_angularMotorDirection.Y = 12.56f; | ||
247 | if(m_angularMotorDirection.Y < - 12.56f) m_angularMotorDirection.Y = - 12.56f; | ||
248 | if(m_angularMotorDirection.Z > 12.56f) m_angularMotorDirection.Z = 12.56f; | ||
249 | if(m_angularMotorDirection.Z < - 12.56f) m_angularMotorDirection.Z = - 12.56f; | ||
250 | m_angularMotorApply = 10; | ||
251 | break; | ||
252 | case Vehicle.LINEAR_FRICTION_TIMESCALE: | ||
253 | m_linearFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z); | ||
254 | break; | ||
255 | case Vehicle.LINEAR_MOTOR_DIRECTION: | ||
256 | m_linearMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z); | ||
257 | m_linearMotorDirectionLASTSET = new Vector3(pValue.X, pValue.Y, pValue.Z); | ||
258 | break; | ||
259 | case Vehicle.LINEAR_MOTOR_OFFSET: | ||
260 | // m_linearMotorOffset = new Vector3(pValue.X, pValue.Y, pValue.Z); | ||
261 | break; | ||
262 | } | ||
263 | |||
264 | }//end ProcessVectorVehicleParam | ||
265 | |||
266 | internal void ProcessRotationVehicleParam(Vehicle pParam, Quaternion pValue) | ||
267 | { | ||
268 | switch (pParam) | ||
269 | { | ||
270 | case Vehicle.REFERENCE_FRAME: | ||
271 | // m_referenceFrame = pValue; | ||
272 | break; | ||
273 | } | ||
274 | |||
275 | }//end ProcessRotationVehicleParam | ||
276 | |||
277 | internal void ProcessTypeChange(Vehicle pType) | ||
278 | { | ||
279 | // Set Defaults For Type | ||
280 | m_type = pType; | ||
281 | switch (pType) | ||
282 | { | ||
283 | case Vehicle.TYPE_SLED: | ||
284 | m_linearFrictionTimescale = new Vector3(30, 1, 1000); | ||
285 | m_angularFrictionTimescale = new Vector3(1000, 1000, 1000); | ||
286 | m_linearMotorDirection = Vector3.Zero; | ||
287 | m_linearMotorTimescale = 1000; | ||
288 | m_linearMotorDecayTimescale = 120; | ||
289 | m_angularMotorDirection = Vector3.Zero; | ||
290 | m_angularMotorTimescale = 1000; | ||
291 | m_angularMotorDecayTimescale = 120; | ||
292 | m_VhoverHeight = 0; | ||
293 | m_VhoverEfficiency = 1; | ||
294 | m_VhoverTimescale = 10; | ||
295 | m_VehicleBuoyancy = 0; | ||
296 | // m_linearDeflectionEfficiency = 1; | ||
297 | // m_linearDeflectionTimescale = 1; | ||
298 | // m_angularDeflectionEfficiency = 1; | ||
299 | // m_angularDeflectionTimescale = 1000; | ||
300 | // m_bankingEfficiency = 0; | ||
301 | // m_bankingMix = 1; | ||
302 | // m_bankingTimescale = 10; | ||
303 | // m_referenceFrame = Quaternion.Identity; | ||
304 | m_flags &= | ||
305 | ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | | ||
306 | VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY); | ||
307 | m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.LIMIT_MOTOR_UP); | ||
308 | break; | ||
309 | case Vehicle.TYPE_CAR: | ||
310 | m_linearFrictionTimescale = new Vector3(100, 2, 1000); | ||
311 | m_angularFrictionTimescale = new Vector3(1000, 1000, 1000); | ||
312 | m_linearMotorDirection = Vector3.Zero; | ||
313 | m_linearMotorTimescale = 1; | ||
314 | m_linearMotorDecayTimescale = 60; | ||
315 | m_angularMotorDirection = Vector3.Zero; | ||
316 | m_angularMotorTimescale = 1; | ||
317 | m_angularMotorDecayTimescale = 0.8f; | ||
318 | m_VhoverHeight = 0; | ||
319 | m_VhoverEfficiency = 0; | ||
320 | m_VhoverTimescale = 1000; | ||
321 | m_VehicleBuoyancy = 0; | ||
322 | // // m_linearDeflectionEfficiency = 1; | ||
323 | // // m_linearDeflectionTimescale = 2; | ||
324 | // // m_angularDeflectionEfficiency = 0; | ||
325 | // m_angularDeflectionTimescale = 10; | ||
326 | m_verticalAttractionEfficiency = 1f; | ||
327 | m_verticalAttractionTimescale = 10f; | ||
328 | // m_bankingEfficiency = -0.2f; | ||
329 | // m_bankingMix = 1; | ||
330 | // m_bankingTimescale = 1; | ||
331 | // m_referenceFrame = Quaternion.Identity; | ||
332 | m_flags &= ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT); | ||
333 | m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.HOVER_UP_ONLY | | ||
334 | VehicleFlag.LIMIT_MOTOR_UP); | ||
335 | break; | ||
336 | case Vehicle.TYPE_BOAT: | ||
337 | m_linearFrictionTimescale = new Vector3(10, 3, 2); | ||
338 | m_angularFrictionTimescale = new Vector3(10,10,10); | ||
339 | m_linearMotorDirection = Vector3.Zero; | ||
340 | m_linearMotorTimescale = 5; | ||
341 | m_linearMotorDecayTimescale = 60; | ||
342 | m_angularMotorDirection = Vector3.Zero; | ||
343 | m_angularMotorTimescale = 4; | ||
344 | m_angularMotorDecayTimescale = 4; | ||
345 | m_VhoverHeight = 0; | ||
346 | m_VhoverEfficiency = 0.5f; | ||
347 | m_VhoverTimescale = 2; | ||
348 | m_VehicleBuoyancy = 1; | ||
349 | // m_linearDeflectionEfficiency = 0.5f; | ||
350 | // m_linearDeflectionTimescale = 3; | ||
351 | // m_angularDeflectionEfficiency = 0.5f; | ||
352 | // m_angularDeflectionTimescale = 5; | ||
353 | m_verticalAttractionEfficiency = 0.5f; | ||
354 | m_verticalAttractionTimescale = 5f; | ||
355 | // m_bankingEfficiency = -0.3f; | ||
356 | // m_bankingMix = 0.8f; | ||
357 | // m_bankingTimescale = 1; | ||
358 | // m_referenceFrame = Quaternion.Identity; | ||
359 | m_flags &= ~(VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.LIMIT_ROLL_ONLY | | ||
360 | VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY); | ||
361 | m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.HOVER_WATER_ONLY | | ||
362 | VehicleFlag.LIMIT_MOTOR_UP); | ||
363 | break; | ||
364 | case Vehicle.TYPE_AIRPLANE: | ||
365 | m_linearFrictionTimescale = new Vector3(200, 10, 5); | ||
366 | m_angularFrictionTimescale = new Vector3(20, 20, 20); | ||
367 | m_linearMotorDirection = Vector3.Zero; | ||
368 | m_linearMotorTimescale = 2; | ||
369 | m_linearMotorDecayTimescale = 60; | ||
370 | m_angularMotorDirection = Vector3.Zero; | ||
371 | m_angularMotorTimescale = 4; | ||
372 | m_angularMotorDecayTimescale = 4; | ||
373 | m_VhoverHeight = 0; | ||
374 | m_VhoverEfficiency = 0.5f; | ||
375 | m_VhoverTimescale = 1000; | ||
376 | m_VehicleBuoyancy = 0; | ||
377 | // m_linearDeflectionEfficiency = 0.5f; | ||
378 | // m_linearDeflectionTimescale = 3; | ||
379 | // m_angularDeflectionEfficiency = 1; | ||
380 | // m_angularDeflectionTimescale = 2; | ||
381 | m_verticalAttractionEfficiency = 0.9f; | ||
382 | m_verticalAttractionTimescale = 2f; | ||
383 | // m_bankingEfficiency = 1; | ||
384 | // m_bankingMix = 0.7f; | ||
385 | // m_bankingTimescale = 2; | ||
386 | // m_referenceFrame = Quaternion.Identity; | ||
387 | m_flags &= ~(VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | | ||
388 | VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY | VehicleFlag.LIMIT_MOTOR_UP); | ||
389 | m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY); | ||
390 | break; | ||
391 | case Vehicle.TYPE_BALLOON: | ||
392 | m_linearFrictionTimescale = new Vector3(5, 5, 5); | ||
393 | m_angularFrictionTimescale = new Vector3(10, 10, 10); | ||
394 | m_linearMotorDirection = Vector3.Zero; | ||
395 | m_linearMotorTimescale = 5; | ||
396 | m_linearMotorDecayTimescale = 60; | ||
397 | m_angularMotorDirection = Vector3.Zero; | ||
398 | m_angularMotorTimescale = 6; | ||
399 | m_angularMotorDecayTimescale = 10; | ||
400 | m_VhoverHeight = 5; | ||
401 | m_VhoverEfficiency = 0.8f; | ||
402 | m_VhoverTimescale = 10; | ||
403 | m_VehicleBuoyancy = 1; | ||
404 | // m_linearDeflectionEfficiency = 0; | ||
405 | // m_linearDeflectionTimescale = 5; | ||
406 | // m_angularDeflectionEfficiency = 0; | ||
407 | // m_angularDeflectionTimescale = 5; | ||
408 | m_verticalAttractionEfficiency = 1f; | ||
409 | m_verticalAttractionTimescale = 100f; | ||
410 | // m_bankingEfficiency = 0; | ||
411 | // m_bankingMix = 0.7f; | ||
412 | // m_bankingTimescale = 5; | ||
413 | // m_referenceFrame = Quaternion.Identity; | ||
414 | m_flags &= ~(VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | | ||
415 | VehicleFlag.HOVER_UP_ONLY | VehicleFlag.LIMIT_MOTOR_UP); | ||
416 | m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT); | ||
417 | break; | ||
418 | |||
419 | } | ||
420 | }//end SetDefaultsForType | ||
421 | |||
422 | internal void Enable(IntPtr pBody, OdeScene pParentScene) | ||
423 | { | ||
424 | if (m_type == Vehicle.TYPE_NONE) | ||
425 | return; | ||
426 | |||
427 | m_body = pBody; | ||
428 | } | ||
429 | |||
430 | internal void Step(float pTimestep, OdeScene pParentScene) | ||
431 | { | ||
432 | if (m_body == IntPtr.Zero || m_type == Vehicle.TYPE_NONE) | ||
433 | return; | ||
434 | frcount++; // used to limit debug comment output | ||
435 | if (frcount > 100) | ||
436 | frcount = 0; | ||
437 | |||
438 | MoveLinear(pTimestep, pParentScene); | ||
439 | MoveAngular(pTimestep); | ||
440 | }// end Step | ||
441 | |||
442 | private void MoveLinear(float pTimestep, OdeScene _pParentScene) | ||
443 | { | ||
444 | if (!m_linearMotorDirection.ApproxEquals(Vector3.Zero, 0.01f)) // requested m_linearMotorDirection is significant | ||
445 | { | ||
446 | if(!d.BodyIsEnabled (Body)) d.BodyEnable (Body); | ||
447 | |||
448 | // add drive to body | ||
449 | Vector3 addAmount = m_linearMotorDirection/(m_linearMotorTimescale/pTimestep); | ||
450 | m_lastLinearVelocityVector += (addAmount*10); // lastLinearVelocityVector is the current body velocity vector? | ||
451 | |||
452 | // This will work temporarily, but we really need to compare speed on an axis | ||
453 | // KF: Limit body velocity to applied velocity? | ||
454 | if (Math.Abs(m_lastLinearVelocityVector.X) > Math.Abs(m_linearMotorDirectionLASTSET.X)) | ||
455 | m_lastLinearVelocityVector.X = m_linearMotorDirectionLASTSET.X; | ||
456 | if (Math.Abs(m_lastLinearVelocityVector.Y) > Math.Abs(m_linearMotorDirectionLASTSET.Y)) | ||
457 | m_lastLinearVelocityVector.Y = m_linearMotorDirectionLASTSET.Y; | ||
458 | if (Math.Abs(m_lastLinearVelocityVector.Z) > Math.Abs(m_linearMotorDirectionLASTSET.Z)) | ||
459 | m_lastLinearVelocityVector.Z = m_linearMotorDirectionLASTSET.Z; | ||
460 | |||
461 | // decay applied velocity | ||
462 | Vector3 decayfraction = ((Vector3.One/(m_linearMotorDecayTimescale/pTimestep))); | ||
463 | //Console.WriteLine("decay: " + decayfraction); | ||
464 | m_linearMotorDirection -= m_linearMotorDirection * decayfraction * 0.5f; | ||
465 | //Console.WriteLine("actual: " + m_linearMotorDirection); | ||
466 | } | ||
467 | else | ||
468 | { // requested is not significant | ||
469 | // if what remains of applied is small, zero it. | ||
470 | if (m_lastLinearVelocityVector.ApproxEquals(Vector3.Zero, 0.01f)) | ||
471 | m_lastLinearVelocityVector = Vector3.Zero; | ||
472 | } | ||
473 | |||
474 | |||
475 | // convert requested object velocity to world-referenced vector | ||
476 | m_dir = m_lastLinearVelocityVector; | ||
477 | d.Quaternion rot = d.BodyGetQuaternion(Body); | ||
478 | Quaternion rotq = new Quaternion(rot.X, rot.Y, rot.Z, rot.W); // rotq = rotation of object | ||
479 | m_dir *= rotq; // apply obj rotation to velocity vector | ||
480 | |||
481 | // add Gravity andBuoyancy | ||
482 | // KF: So far I have found no good method to combine a script-requested | ||
483 | // .Z velocity and gravity. Therefore only 0g will used script-requested | ||
484 | // .Z velocity. >0g (m_VehicleBuoyancy < 1) will used modified gravity only. | ||
485 | Vector3 grav = Vector3.Zero; | ||
486 | if(m_VehicleBuoyancy < 1.0f) | ||
487 | { | ||
488 | // There is some gravity, make a gravity force vector | ||
489 | // that is applied after object velocity. | ||
490 | d.Mass objMass; | ||
491 | d.BodyGetMass(Body, out objMass); | ||
492 | // m_VehicleBuoyancy: -1=2g; 0=1g; 1=0g; | ||
493 | grav.Z = _pParentScene.gravityz * objMass.mass * (1f - m_VehicleBuoyancy); | ||
494 | // Preserve the current Z velocity | ||
495 | d.Vector3 vel_now = d.BodyGetLinearVel(Body); | ||
496 | m_dir.Z = vel_now.Z; // Preserve the accumulated falling velocity | ||
497 | } // else its 1.0, no gravity. | ||
498 | |||
499 | // Check if hovering | ||
500 | if( (m_flags & (VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT)) != 0) | ||
501 | { | ||
502 | // We should hover, get the target height | ||
503 | d.Vector3 pos = d.BodyGetPosition(Body); | ||
504 | if((m_flags & VehicleFlag.HOVER_WATER_ONLY) == VehicleFlag.HOVER_WATER_ONLY) | ||
505 | { | ||
506 | m_VhoverTargetHeight = _pParentScene.GetWaterLevel() + m_VhoverHeight; | ||
507 | } | ||
508 | else if((m_flags & VehicleFlag.HOVER_TERRAIN_ONLY) == VehicleFlag.HOVER_TERRAIN_ONLY) | ||
509 | { | ||
510 | m_VhoverTargetHeight = _pParentScene.GetTerrainHeightAtXY(pos.X, pos.Y) + m_VhoverHeight; | ||
511 | } | ||
512 | else if((m_flags & VehicleFlag.HOVER_GLOBAL_HEIGHT) == VehicleFlag.HOVER_GLOBAL_HEIGHT) | ||
513 | { | ||
514 | m_VhoverTargetHeight = m_VhoverHeight; | ||
515 | } | ||
516 | |||
517 | if((m_flags & VehicleFlag.HOVER_UP_ONLY) == VehicleFlag.HOVER_UP_ONLY) | ||
518 | { | ||
519 | // If body is aready heigher, use its height as target height | ||
520 | if(pos.Z > m_VhoverTargetHeight) m_VhoverTargetHeight = pos.Z; | ||
521 | } | ||
522 | |||
523 | // m_VhoverEfficiency = 0f; // 0=boucy, 1=Crit.damped | ||
524 | // m_VhoverTimescale = 0f; // time to acheive height | ||
525 | // pTimestep is time since last frame,in secs | ||
526 | float herr0 = pos.Z - m_VhoverTargetHeight; | ||
527 | // Replace Vertical speed with correction figure if significant | ||
528 | if(Math.Abs(herr0) > 0.01f ) | ||
529 | { | ||
530 | d.Mass objMass; | ||
531 | d.BodyGetMass(Body, out objMass); | ||
532 | m_dir.Z = - ( (herr0 * pTimestep * 50.0f) / m_VhoverTimescale); | ||
533 | //KF: m_VhoverEfficiency is not yet implemented | ||
534 | } | ||
535 | else | ||
536 | { | ||
537 | m_dir.Z = 0f; | ||
538 | } | ||
539 | } | ||
540 | |||
541 | // Apply velocity | ||
542 | d.BodySetLinearVel(Body, m_dir.X, m_dir.Y, m_dir.Z); | ||
543 | // apply gravity force | ||
544 | d.BodyAddForce(Body, grav.X, grav.Y, grav.Z); | ||
545 | |||
546 | |||
547 | // apply friction | ||
548 | Vector3 decayamount = Vector3.One / (m_linearFrictionTimescale / pTimestep); | ||
549 | m_lastLinearVelocityVector -= m_lastLinearVelocityVector * decayamount; | ||
550 | } // end MoveLinear() | ||
551 | |||
552 | private void MoveAngular(float pTimestep) | ||
553 | { | ||
554 | /* | ||
555 | private Vector3 m_angularMotorDirection = Vector3.Zero; // angular velocity requested by LSL motor | ||
556 | private int m_angularMotorApply = 0; // application frame counter | ||
557 | private float m_angularMotorVelocity = 0; // current angular motor velocity (ramps up and down) | ||
558 | private float m_angularMotorTimescale = 0; // motor angular velocity ramp up rate | ||
559 | private float m_angularMotorDecayTimescale = 0; // motor angular velocity decay rate | ||
560 | private Vector3 m_angularFrictionTimescale = Vector3.Zero; // body angular velocity decay rate | ||
561 | private Vector3 m_lastAngularVelocity = Vector3.Zero; // what was last applied to body | ||
562 | */ | ||
563 | |||
564 | // Get what the body is doing, this includes 'external' influences | ||
565 | d.Vector3 angularVelocity = d.BodyGetAngularVel(Body); | ||
566 | // Vector3 angularVelocity = Vector3.Zero; | ||
567 | |||
568 | if (m_angularMotorApply > 0) | ||
569 | { | ||
570 | // ramp up to new value | ||
571 | // current velocity += error / ( time to get there / step interval ) | ||
572 | // requested speed - last motor speed | ||
573 | m_angularMotorVelocity.X += (m_angularMotorDirection.X - m_angularMotorVelocity.X) / (m_angularMotorTimescale / pTimestep); | ||
574 | m_angularMotorVelocity.Y += (m_angularMotorDirection.Y - m_angularMotorVelocity.Y) / (m_angularMotorTimescale / pTimestep); | ||
575 | m_angularMotorVelocity.Z += (m_angularMotorDirection.Z - m_angularMotorVelocity.Z) / (m_angularMotorTimescale / pTimestep); | ||
576 | |||
577 | m_angularMotorApply--; // This is done so that if script request rate is less than phys frame rate the expected | ||
578 | // velocity may still be acheived. | ||
579 | } | ||
580 | else | ||
581 | { | ||
582 | // no motor recently applied, keep the body velocity | ||
583 | /* m_angularMotorVelocity.X = angularVelocity.X; | ||
584 | m_angularMotorVelocity.Y = angularVelocity.Y; | ||
585 | m_angularMotorVelocity.Z = angularVelocity.Z; */ | ||
586 | |||
587 | // and decay the velocity | ||
588 | m_angularMotorVelocity -= m_angularMotorVelocity / (m_angularMotorDecayTimescale / pTimestep); | ||
589 | } // end motor section | ||
590 | |||
591 | |||
592 | // Vertical attractor section | ||
593 | Vector3 vertattr = Vector3.Zero; | ||
594 | |||
595 | if(m_verticalAttractionTimescale < 300) | ||
596 | { | ||
597 | float VAservo = 0.2f / (m_verticalAttractionTimescale * pTimestep); | ||
598 | // get present body rotation | ||
599 | d.Quaternion rot = d.BodyGetQuaternion(Body); | ||
600 | Quaternion rotq = new Quaternion(rot.X, rot.Y, rot.Z, rot.W); | ||
601 | // make a vector pointing up | ||
602 | Vector3 verterr = Vector3.Zero; | ||
603 | verterr.Z = 1.0f; | ||
604 | // rotate it to Body Angle | ||
605 | verterr = verterr * rotq; | ||
606 | // verterr.X and .Y are the World error ammounts. They are 0 when there is no error (Vehicle Body is 'vertical'), and .Z will be 1. | ||
607 | // As the body leans to its side |.X| will increase to 1 and .Z fall to 0. As body inverts |.X| will fall and .Z will go | ||
608 | // negative. Similar for tilt and |.Y|. .X and .Y must be modulated to prevent a stable inverted body. | ||
609 | if (verterr.Z < 0.0f) | ||
610 | { | ||
611 | verterr.X = 2.0f - verterr.X; | ||
612 | verterr.Y = 2.0f - verterr.Y; | ||
613 | } | ||
614 | // Error is 0 (no error) to +/- 2 (max error) | ||
615 | // scale it by VAservo | ||
616 | verterr = verterr * VAservo; | ||
617 | //if(frcount == 0) Console.WriteLine("VAerr=" + verterr); | ||
618 | |||
619 | // As the body rotates around the X axis, then verterr.Y increases; Rotated around Y then .X increases, so | ||
620 | // Change Body angular velocity X based on Y, and Y based on X. Z is not changed. | ||
621 | vertattr.X = verterr.Y; | ||
622 | vertattr.Y = - verterr.X; | ||
623 | vertattr.Z = 0f; | ||
624 | |||
625 | // scaling appears better usingsquare-law | ||
626 | float bounce = 1.0f - (m_verticalAttractionEfficiency * m_verticalAttractionEfficiency); | ||
627 | vertattr.X += bounce * angularVelocity.X; | ||
628 | vertattr.Y += bounce * angularVelocity.Y; | ||
629 | |||
630 | } // else vertical attractor is off | ||
631 | |||
632 | // m_lastVertAttractor = vertattr; | ||
633 | |||
634 | // Bank section tba | ||
635 | // Deflection section tba | ||
636 | |||
637 | // Sum velocities | ||
638 | m_lastAngularVelocity = m_angularMotorVelocity + vertattr; // + bank + deflection | ||
639 | |||
640 | if (!m_lastAngularVelocity.ApproxEquals(Vector3.Zero, 0.01f)) | ||
641 | { | ||
642 | if(!d.BodyIsEnabled (Body)) d.BodyEnable (Body); | ||
643 | } | ||
644 | else | ||
645 | { | ||
646 | m_lastAngularVelocity = Vector3.Zero; // Reduce small value to zero. | ||
647 | } | ||
648 | |||
649 | // apply friction | ||
650 | Vector3 decayamount = Vector3.One / (m_angularFrictionTimescale / pTimestep); | ||
651 | m_lastAngularVelocity -= m_lastAngularVelocity * decayamount; | ||
652 | |||
653 | // Apply to the body | ||
654 | d.BodySetAngularVel (Body, m_lastAngularVelocity.X, m_lastAngularVelocity.Y, m_lastAngularVelocity.Z); | ||
655 | |||
656 | } //end MoveAngular | ||
657 | } | ||
658 | } | ||