diff options
author | Robert Adams | 2013-04-07 08:27:49 -0700 |
---|---|---|
committer | Robert Adams | 2013-04-08 06:27:43 -0700 |
commit | fe16dc09da3f2736fad5a9e792f5f81098b5f9a1 (patch) | |
tree | 14df85296a103e7326726ccfb9a017bc9ecb669f /OpenSim/Region/Physics/BulletSPlugin/BSActorHover.cs | |
parent | Optimize the number of Simian calls to get the initial presence (diff) | |
download | opensim-SC_OLD-fe16dc09da3f2736fad5a9e792f5f81098b5f9a1.zip opensim-SC_OLD-fe16dc09da3f2736fad5a9e792f5f81098b5f9a1.tar.gz opensim-SC_OLD-fe16dc09da3f2736fad5a9e792f5f81098b5f9a1.tar.bz2 opensim-SC_OLD-fe16dc09da3f2736fad5a9e792f5f81098b5f9a1.tar.xz |
BulletSim: complete movement of physical object action code out of the
physical object and into actors for setForce, setTorque, hover, lock
axis and avatar move.
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSActorHover.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSActorHover.cs | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSActorHover.cs b/OpenSim/Region/Physics/BulletSPlugin/BSActorHover.cs new file mode 100755 index 0000000..8dd3700 --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSActorHover.cs | |||
@@ -0,0 +1,176 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.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 copyrightD | ||
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 OpenSimulator 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 System.Linq; | ||
31 | using System.Text; | ||
32 | |||
33 | using OpenSim.Region.Physics.Manager; | ||
34 | |||
35 | using OMV = OpenMetaverse; | ||
36 | |||
37 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
38 | { | ||
39 | public class BSActorHover : BSActor | ||
40 | { | ||
41 | private BSFMotor m_hoverMotor; | ||
42 | |||
43 | public BSActorHover(BSScene physicsScene, BSPhysObject pObj, string actorName) | ||
44 | : base(physicsScene, pObj, actorName) | ||
45 | { | ||
46 | m_hoverMotor = null; | ||
47 | m_physicsScene.DetailLog("{0},BSActorHover,constructor", m_controllingPrim.LocalID); | ||
48 | } | ||
49 | |||
50 | // BSActor.isActive | ||
51 | public override bool isActive | ||
52 | { | ||
53 | get { return Enabled && m_controllingPrim.IsPhysicallyActive; } | ||
54 | } | ||
55 | |||
56 | // Release any connections and resources used by the actor. | ||
57 | // BSActor.Dispose() | ||
58 | public override void Dispose() | ||
59 | { | ||
60 | Enabled = false; | ||
61 | } | ||
62 | |||
63 | // Called when physical parameters (properties set in Bullet) need to be re-applied. | ||
64 | // Called at taint-time. | ||
65 | // BSActor.Refresh() | ||
66 | public override void Refresh() | ||
67 | { | ||
68 | m_physicsScene.DetailLog("{0},BSActorHover,refresh", m_controllingPrim.LocalID); | ||
69 | |||
70 | // If not active any more, get rid of me (shouldn't ever happen, but just to be safe) | ||
71 | if (!m_controllingPrim.HoverActive) | ||
72 | { | ||
73 | m_physicsScene.DetailLog("{0},BSActorHover,refresh,notHovering,removing={1}", m_controllingPrim.LocalID, ActorName); | ||
74 | m_controllingPrim.PhysicalActors.RemoveAndRelease(ActorName); | ||
75 | return; | ||
76 | } | ||
77 | |||
78 | // If the object is physically active, add the hoverer prestep action | ||
79 | if (isActive) | ||
80 | { | ||
81 | ActivateHover(); | ||
82 | } | ||
83 | else | ||
84 | { | ||
85 | DeactivateHover(); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...). | ||
90 | // Register a prestep action to restore physical requirements before the next simulation step. | ||
91 | // Called at taint-time. | ||
92 | // BSActor.RemoveBodyDependencies() | ||
93 | public override void RemoveBodyDependencies() | ||
94 | { | ||
95 | // Nothing to do for the hoverer since it is all software at pre-step action time. | ||
96 | } | ||
97 | |||
98 | // If a hover motor has not been created, create one and start the hovering. | ||
99 | private void ActivateHover() | ||
100 | { | ||
101 | if (m_hoverMotor == null) | ||
102 | { | ||
103 | // Turning the target on | ||
104 | m_hoverMotor = new BSFMotor("BSActorHover", | ||
105 | m_controllingPrim.HoverTau, // timeScale | ||
106 | BSMotor.Infinite, // decay time scale | ||
107 | BSMotor.Infinite, // friction timescale | ||
108 | 1f // efficiency | ||
109 | ); | ||
110 | m_hoverMotor.SetTarget(ComputeCurrentHoverHeight()); | ||
111 | m_hoverMotor.SetCurrent(m_controllingPrim.RawPosition.Z); | ||
112 | m_hoverMotor.PhysicsScene = m_physicsScene; // DEBUG DEBUG so motor will output detail log messages. | ||
113 | |||
114 | m_physicsScene.BeforeStep += Hoverer; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | private void DeactivateHover() | ||
119 | { | ||
120 | if (m_hoverMotor != null) | ||
121 | { | ||
122 | m_physicsScene.BeforeStep -= Hoverer; | ||
123 | m_hoverMotor = null; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | // Called just before the simulation step. Update the vertical position for hoverness. | ||
128 | private void Hoverer(float timeStep) | ||
129 | { | ||
130 | // Don't do hovering while the object is selected. | ||
131 | if (!isActive) | ||
132 | return; | ||
133 | |||
134 | m_hoverMotor.SetCurrent(m_controllingPrim.RawPosition.Z); | ||
135 | m_hoverMotor.SetTarget(ComputeCurrentHoverHeight()); | ||
136 | float targetHeight = m_hoverMotor.Step(timeStep); | ||
137 | |||
138 | // 'targetHeight' is where we'd like the Z of the prim to be at this moment. | ||
139 | // Compute the amount of force to push us there. | ||
140 | float moveForce = (targetHeight - m_controllingPrim.RawPosition.Z) * m_controllingPrim.RawMass; | ||
141 | // Undo anything the object thinks it's doing at the moment | ||
142 | moveForce = -m_controllingPrim.RawVelocity.Z * m_controllingPrim.Mass; | ||
143 | |||
144 | m_physicsScene.PE.ApplyCentralImpulse(m_controllingPrim.PhysBody, new OMV.Vector3(0f, 0f, moveForce)); | ||
145 | m_physicsScene.DetailLog("{0},BSPrim.Hover,move,targHt={1},moveForce={2},mass={3}", | ||
146 | m_controllingPrim.LocalID, targetHeight, moveForce, m_controllingPrim.RawMass); | ||
147 | } | ||
148 | |||
149 | // Based on current position, determine what we should be hovering at now. | ||
150 | // Must recompute often. What if we walked offa cliff> | ||
151 | private float ComputeCurrentHoverHeight() | ||
152 | { | ||
153 | float ret = m_controllingPrim.HoverHeight; | ||
154 | float groundHeight = m_physicsScene.TerrainManager.GetTerrainHeightAtXYZ(m_controllingPrim.RawPosition); | ||
155 | |||
156 | switch (m_controllingPrim.HoverType) | ||
157 | { | ||
158 | case PIDHoverType.Ground: | ||
159 | ret = groundHeight + m_controllingPrim.HoverHeight; | ||
160 | break; | ||
161 | case PIDHoverType.GroundAndWater: | ||
162 | float waterHeight = m_physicsScene.TerrainManager.GetWaterLevelAtXYZ(m_controllingPrim.RawPosition); | ||
163 | if (groundHeight > waterHeight) | ||
164 | { | ||
165 | ret = groundHeight + m_controllingPrim.HoverHeight; | ||
166 | } | ||
167 | else | ||
168 | { | ||
169 | ret = waterHeight + m_controllingPrim.HoverHeight; | ||
170 | } | ||
171 | break; | ||
172 | } | ||
173 | return ret; | ||
174 | } | ||
175 | } | ||
176 | } | ||