diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs')
-rw-r--r-- | OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs | 1157 |
1 files changed, 554 insertions, 603 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs b/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs index 9c20004..6a4365c 100644 --- a/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs | |||
@@ -24,6 +24,9 @@ | |||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 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. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ | 26 | */ |
27 | |||
28 | // Uncomment this it enable code to do all shape an body memory management | ||
29 | // in the C# code. | ||
27 | using System; | 30 | using System; |
28 | using System.Reflection; | 31 | using System.Reflection; |
29 | using System.Collections.Generic; | 32 | using System.Collections.Generic; |
@@ -36,32 +39,20 @@ using OpenSim.Region.Physics.ConvexDecompositionDotNet; | |||
36 | 39 | ||
37 | namespace OpenSim.Region.Physics.BulletSPlugin | 40 | namespace OpenSim.Region.Physics.BulletSPlugin |
38 | { | 41 | { |
42 | |||
39 | [Serializable] | 43 | [Serializable] |
40 | public sealed class BSPrim : PhysicsActor | 44 | public sealed class BSPrim : BSPhysObject |
41 | { | 45 | { |
42 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 46 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
43 | private static readonly string LogHeader = "[BULLETS PRIM]"; | 47 | private static readonly string LogHeader = "[BULLETS PRIM]"; |
44 | 48 | ||
45 | private void DebugLog(string mm, params Object[] xx) { if (_scene.ShouldDebugLog) m_log.DebugFormat(mm, xx); } | ||
46 | |||
47 | private IMesh _mesh; | ||
48 | private PrimitiveBaseShape _pbs; | 49 | private PrimitiveBaseShape _pbs; |
49 | private ShapeData.PhysicsShapeType _shapeType; | ||
50 | private ulong _meshKey; | ||
51 | private ulong _hullKey; | ||
52 | private List<ConvexResult> _hulls; | ||
53 | |||
54 | private BSScene _scene; | ||
55 | public BSScene Scene { get { return _scene; } } | ||
56 | private String _avName; | ||
57 | private uint _localID = 0; | ||
58 | 50 | ||
59 | // _size is what the user passed. _scale is what we pass to the physics engine with the mesh. | 51 | // _size is what the user passed. _scale is what we pass to the physics engine with the mesh. |
60 | // Often _scale is unity because the meshmerizer will apply _size when creating the mesh. | 52 | // Often _scale is unity because the meshmerizer will apply _size when creating the mesh. |
61 | private OMV.Vector3 _size; // the multiplier for each mesh dimension as passed by the user | 53 | private OMV.Vector3 _size; // the multiplier for each mesh dimension as passed by the user |
62 | private OMV.Vector3 _scale; // the multiplier for each mesh dimension for the mesh as created by the meshmerizer | 54 | private OMV.Vector3 _scale; // the multiplier for each mesh dimension for the mesh as created by the meshmerizer |
63 | 55 | ||
64 | private bool _stopped; | ||
65 | private bool _grabbed; | 56 | private bool _grabbed; |
66 | private bool _isSelected; | 57 | private bool _isSelected; |
67 | private bool _isVolumeDetect; | 58 | private bool _isVolumeDetect; |
@@ -89,25 +80,6 @@ public sealed class BSPrim : PhysicsActor | |||
89 | private bool _kinematic; | 80 | private bool _kinematic; |
90 | private float _buoyancy; | 81 | private float _buoyancy; |
91 | 82 | ||
92 | // Membership in a linkset is controlled by this class. | ||
93 | private BSLinkset _linkset; | ||
94 | public BSLinkset Linkset | ||
95 | { | ||
96 | get { return _linkset; } | ||
97 | set { _linkset = value; } | ||
98 | } | ||
99 | |||
100 | private int _subscribedEventsMs = 0; | ||
101 | private int _nextCollisionOkTime = 0; | ||
102 | long _collidingStep; | ||
103 | long _collidingGroundStep; | ||
104 | |||
105 | private BulletBody m_body; | ||
106 | public BulletBody Body { | ||
107 | get { return m_body; } | ||
108 | set { m_body = value; } | ||
109 | } | ||
110 | |||
111 | private BSDynamics _vehicle; | 83 | private BSDynamics _vehicle; |
112 | 84 | ||
113 | private OMV.Vector3 _PIDTarget; | 85 | private OMV.Vector3 _PIDTarget; |
@@ -122,9 +94,8 @@ public sealed class BSPrim : PhysicsActor | |||
122 | OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical) | 94 | OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical) |
123 | { | 95 | { |
124 | // m_log.DebugFormat("{0}: BSPrim creation of {1}, id={2}", LogHeader, primName, localID); | 96 | // m_log.DebugFormat("{0}: BSPrim creation of {1}, id={2}", LogHeader, primName, localID); |
125 | _localID = localID; | 97 | base.BaseInitialize(parent_scene, localID, primName, "BSPrim"); |
126 | _avName = primName; | 98 | _physicsActorType = (int)ActorTypes.Prim; |
127 | _scene = parent_scene; | ||
128 | _position = pos; | 99 | _position = pos; |
129 | _size = size; | 100 | _size = size; |
130 | _scale = new OMV.Vector3(1f, 1f, 1f); // the scale will be set by CreateGeom depending on object type | 101 | _scale = new OMV.Vector3(1f, 1f, 1f); // the scale will be set by CreateGeom depending on object type |
@@ -132,98 +103,103 @@ public sealed class BSPrim : PhysicsActor | |||
132 | _buoyancy = 1f; | 103 | _buoyancy = 1f; |
133 | _velocity = OMV.Vector3.Zero; | 104 | _velocity = OMV.Vector3.Zero; |
134 | _rotationalVelocity = OMV.Vector3.Zero; | 105 | _rotationalVelocity = OMV.Vector3.Zero; |
135 | _hullKey = 0; | ||
136 | _meshKey = 0; | ||
137 | _pbs = pbs; | 106 | _pbs = pbs; |
138 | _isPhysical = pisPhysical; | 107 | _isPhysical = pisPhysical; |
139 | _isVolumeDetect = false; | 108 | _isVolumeDetect = false; |
140 | _subscribedEventsMs = 0; | 109 | _friction = PhysicsScene.Params.defaultFriction; // TODO: compute based on object material |
141 | _friction = _scene.Params.defaultFriction; // TODO: compute based on object material | 110 | _density = PhysicsScene.Params.defaultDensity; // TODO: compute based on object material |
142 | _density = _scene.Params.defaultDensity; // TODO: compute based on object material | 111 | _restitution = PhysicsScene.Params.defaultRestitution; |
143 | _restitution = _scene.Params.defaultRestitution; | 112 | _vehicle = new BSDynamics(PhysicsScene, this); // add vehicleness |
144 | _linkset = new BSLinkset(_scene, this); // a linkset of one | ||
145 | _vehicle = new BSDynamics(this); // add vehicleness | ||
146 | _mass = CalculateMass(); | 113 | _mass = CalculateMass(); |
147 | // do the actual object creation at taint time | 114 | |
115 | // No body or shape yet | ||
116 | BSBody = new BulletBody(LocalID, IntPtr.Zero); | ||
117 | BSShape = new BulletShape(IntPtr.Zero); | ||
118 | |||
148 | DetailLog("{0},BSPrim.constructor,call", LocalID); | 119 | DetailLog("{0},BSPrim.constructor,call", LocalID); |
149 | _scene.TaintedObject("BSPrim.create", delegate() | 120 | // do the actual object creation at taint time |
121 | PhysicsScene.TaintedObject("BSPrim.create", delegate() | ||
150 | { | 122 | { |
151 | RecreateGeomAndObject(); | 123 | CreateGeomAndObject(true); |
152 | 124 | ||
153 | // Get the pointer to the physical body for this object. | 125 | CurrentCollisionFlags = BulletSimAPI.GetCollisionFlags2(BSBody.ptr); |
154 | // At the moment, we're still letting BulletSim manage the creation and destruction | ||
155 | // of the object. Someday we'll move that into the C# code. | ||
156 | m_body = new BulletBody(LocalID, BulletSimAPI.GetBodyHandle2(_scene.World.Ptr, LocalID)); | ||
157 | }); | 126 | }); |
158 | } | 127 | } |
159 | 128 | ||
160 | // called when this prim is being destroyed and we should free all the resources | 129 | // called when this prim is being destroyed and we should free all the resources |
161 | public void Destroy() | 130 | public override void Destroy() |
162 | { | 131 | { |
163 | // m_log.DebugFormat("{0}: Destroy, id={1}", LogHeader, LocalID); | 132 | // m_log.DebugFormat("{0}: Destroy, id={1}", LogHeader, LocalID); |
164 | 133 | ||
165 | // Undo any links between me and any other object | 134 | // Undo any links between me and any other object |
166 | BSPrim parentBefore = _linkset.LinksetRoot; | 135 | BSPhysObject parentBefore = Linkset.LinksetRoot; |
167 | int childrenBefore = _linkset.NumberOfChildren; | 136 | int childrenBefore = Linkset.NumberOfChildren; |
168 | 137 | ||
169 | _linkset = _linkset.RemoveMeFromLinkset(this); | 138 | Linkset = Linkset.RemoveMeFromLinkset(this); |
170 | 139 | ||
171 | DetailLog("{0},BSPrim.Destroy,call,parentBefore={1},childrenBefore={2},parentAfter={3},childrenAfter={4}", | 140 | DetailLog("{0},BSPrim.Destroy,call,parentBefore={1},childrenBefore={2},parentAfter={3},childrenAfter={4}", |
172 | LocalID, parentBefore.LocalID, childrenBefore, _linkset.LinksetRoot.LocalID, _linkset.NumberOfChildren); | 141 | LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren); |
173 | 142 | ||
174 | // Undo any vehicle properties | 143 | // Undo any vehicle properties |
175 | this.VehicleType = (int)Vehicle.TYPE_NONE; | 144 | this.VehicleType = (int)Vehicle.TYPE_NONE; |
176 | 145 | ||
177 | _scene.TaintedObject("BSPrim.destroy", delegate() | 146 | PhysicsScene.TaintedObject("BSPrim.destroy", delegate() |
178 | { | 147 | { |
179 | DetailLog("{0},BSPrim.Destroy,taint,", LocalID); | 148 | DetailLog("{0},BSPrim.Destroy,taint,", LocalID); |
180 | // everything in the C# world will get garbage collected. Tell the C++ world to free stuff. | 149 | // If there are physical body and shape, release my use of same. |
181 | BulletSimAPI.DestroyObject(_scene.WorldID, LocalID); | 150 | PhysicsScene.Shapes.DereferenceBody(BSBody, true, null); |
151 | PhysicsScene.Shapes.DereferenceShape(BSShape, true, null); | ||
182 | }); | 152 | }); |
183 | } | 153 | } |
184 | 154 | ||
185 | public override bool Stopped { | 155 | // No one uses this property. |
186 | get { return _stopped; } | 156 | public override bool Stopped { |
157 | get { return false; } | ||
187 | } | 158 | } |
188 | public override OMV.Vector3 Size { | 159 | public override OMV.Vector3 Size { |
189 | get { return _size; } | 160 | get { return _size; } |
190 | set { | 161 | set { |
191 | _size = value; | 162 | _size = value; |
192 | _scene.TaintedObject("BSPrim.setSize", delegate() | 163 | PhysicsScene.TaintedObject("BSPrim.setSize", delegate() |
193 | { | 164 | { |
194 | _mass = CalculateMass(); // changing size changes the mass | 165 | _mass = CalculateMass(); // changing size changes the mass |
195 | BulletSimAPI.SetObjectScaleMass(_scene.WorldID, _localID, _scale, (IsPhysical ? _mass : 0f), IsPhysical); | 166 | // Since _size changed, the mesh needs to be rebuilt. If rebuilt, all the correct |
196 | // DetailLog("{0}: BSPrim.setSize: size={1}, mass={2}, physical={3}", LocalID, _size, _mass, IsPhysical); | 167 | // scale and margins are set. |
197 | RecreateGeomAndObject(); | 168 | CreateGeomAndObject(true); |
169 | // DetailLog("{0},BSPrim.setSize,size={1},scale={2},mass={3},physical={4}", LocalID, _size, _scale, _mass, IsPhysical); | ||
198 | }); | 170 | }); |
199 | } | 171 | } |
172 | } | ||
173 | // Scale is what we set in the physics engine. It is different than 'size' in that | ||
174 | // 'size' can be encorporated into the mesh. In that case, the scale is <1,1,1>. | ||
175 | public OMV.Vector3 Scale | ||
176 | { | ||
177 | get { return _scale; } | ||
178 | set { _scale = value; } | ||
200 | } | 179 | } |
201 | public override PrimitiveBaseShape Shape { | 180 | public override PrimitiveBaseShape Shape { |
202 | set { | 181 | set { |
203 | _pbs = value; | 182 | _pbs = value; |
204 | _scene.TaintedObject("BSPrim.setShape", delegate() | 183 | PhysicsScene.TaintedObject("BSPrim.setShape", delegate() |
205 | { | 184 | { |
206 | _mass = CalculateMass(); // changing the shape changes the mass | 185 | _mass = CalculateMass(); // changing the shape changes the mass |
207 | RecreateGeomAndObject(); | 186 | CreateGeomAndObject(true); |
208 | }); | 187 | }); |
209 | } | 188 | } |
210 | } | ||
211 | public override uint LocalID { | ||
212 | set { _localID = value; } | ||
213 | get { return _localID; } | ||
214 | } | 189 | } |
215 | public override bool Grabbed { | 190 | public override bool Grabbed { |
216 | set { _grabbed = value; | 191 | set { _grabbed = value; |
217 | } | 192 | } |
218 | } | 193 | } |
219 | public override bool Selected { | 194 | public override bool Selected { |
220 | set { | 195 | set { |
221 | _isSelected = value; | 196 | _isSelected = value; |
222 | _scene.TaintedObject("BSPrim.setSelected", delegate() | 197 | PhysicsScene.TaintedObject("BSPrim.setSelected", delegate() |
223 | { | 198 | { |
224 | SetObjectDynamic(); | 199 | DetailLog("{0},BSPrim.selected,taint,selected={1}", LocalID, _isSelected); |
200 | SetObjectDynamic(false); | ||
225 | }); | 201 | }); |
226 | } | 202 | } |
227 | } | 203 | } |
228 | public override void CrossingFailure() { return; } | 204 | public override void CrossingFailure() { return; } |
229 | 205 | ||
@@ -232,158 +208,227 @@ public sealed class BSPrim : PhysicsActor | |||
232 | BSPrim parent = obj as BSPrim; | 208 | BSPrim parent = obj as BSPrim; |
233 | if (parent != null) | 209 | if (parent != null) |
234 | { | 210 | { |
235 | DebugLog("{0}: link {1}/{2} to {3}", LogHeader, _avName, _localID, parent.LocalID); | 211 | BSPhysObject parentBefore = Linkset.LinksetRoot; |
236 | BSPrim parentBefore = _linkset.LinksetRoot; | 212 | int childrenBefore = Linkset.NumberOfChildren; |
237 | int childrenBefore = _linkset.NumberOfChildren; | ||
238 | 213 | ||
239 | _linkset = parent.Linkset.AddMeToLinkset(this); | 214 | Linkset = parent.Linkset.AddMeToLinkset(this); |
240 | 215 | ||
241 | DetailLog("{0},BSPrim.link,call,parentBefore={1}, childrenBefore=={2}, parentAfter={3}, childrenAfter={4}", | 216 | DetailLog("{0},BSPrim.link,call,parentBefore={1}, childrenBefore=={2}, parentAfter={3}, childrenAfter={4}", |
242 | LocalID, parentBefore.LocalID, childrenBefore, _linkset.LinksetRoot.LocalID, _linkset.NumberOfChildren); | 217 | LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren); |
243 | } | 218 | } |
244 | return; | 219 | return; |
245 | } | 220 | } |
246 | 221 | ||
247 | // delink me from my linkset | 222 | // delink me from my linkset |
248 | public override void delink() { | 223 | public override void delink() { |
249 | // TODO: decide if this parent checking needs to happen at taint time | 224 | // TODO: decide if this parent checking needs to happen at taint time |
250 | // Race condition here: if link() and delink() in same simulation tick, the delink will not happen | 225 | // Race condition here: if link() and delink() in same simulation tick, the delink will not happen |
251 | DebugLog("{0}: delink {1}/{2}. Parent={3}", LogHeader, _avName, _localID, | ||
252 | _linkset.LinksetRoot._avName+"/"+_linkset.LinksetRoot.LocalID.ToString()); | ||
253 | 226 | ||
254 | BSPrim parentBefore = _linkset.LinksetRoot; | 227 | BSPhysObject parentBefore = Linkset.LinksetRoot; |
255 | int childrenBefore = _linkset.NumberOfChildren; | 228 | int childrenBefore = Linkset.NumberOfChildren; |
256 | 229 | ||
257 | _linkset = _linkset.RemoveMeFromLinkset(this); | 230 | Linkset = Linkset.RemoveMeFromLinkset(this); |
258 | 231 | ||
259 | DetailLog("{0},BSPrim.delink,parentBefore={1},childrenBefore={2},parentAfter={3},childrenAfter={4}, ", | 232 | DetailLog("{0},BSPrim.delink,parentBefore={1},childrenBefore={2},parentAfter={3},childrenAfter={4}, ", |
260 | LocalID, parentBefore.LocalID, childrenBefore, _linkset.LinksetRoot.LocalID, _linkset.NumberOfChildren); | 233 | LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren); |
261 | return; | 234 | return; |
262 | } | 235 | } |
263 | 236 | ||
264 | // Set motion values to zero. | 237 | // Set motion values to zero. |
265 | // Do it to the properties so the values get set in the physics engine. | 238 | // Do it to the properties so the values get set in the physics engine. |
266 | // Push the setting of the values to the viewer. | 239 | // Push the setting of the values to the viewer. |
267 | // Called at taint time! | 240 | // Called at taint time! |
268 | public void ZeroMotion() | 241 | public override void ZeroMotion() |
269 | { | 242 | { |
270 | _velocity = OMV.Vector3.Zero; | 243 | _velocity = OMV.Vector3.Zero; |
271 | _acceleration = OMV.Vector3.Zero; | 244 | _acceleration = OMV.Vector3.Zero; |
272 | _rotationalVelocity = OMV.Vector3.Zero; | 245 | _rotationalVelocity = OMV.Vector3.Zero; |
273 | 246 | ||
274 | // Zero some other properties directly into the physics engine | 247 | // Zero some other properties directly into the physics engine |
275 | BulletSimAPI.SetVelocity2(Body.Ptr, OMV.Vector3.Zero); | 248 | BulletSimAPI.ClearForces2(BSBody.ptr); |
276 | BulletSimAPI.SetAngularVelocity2(Body.Ptr, OMV.Vector3.Zero); | ||
277 | BulletSimAPI.SetInterpolation2(Body.Ptr, OMV.Vector3.Zero, OMV.Vector3.Zero); | ||
278 | BulletSimAPI.ClearForces2(Body.Ptr); | ||
279 | } | 249 | } |
280 | 250 | ||
281 | public override void LockAngularMotion(OMV.Vector3 axis) | 251 | public override void LockAngularMotion(OMV.Vector3 axis) |
282 | { | 252 | { |
283 | // DetailLog("{0},BSPrim.LockAngularMotion,call,axis={1}", LocalID, axis); | 253 | DetailLog("{0},BSPrim.LockAngularMotion,call,axis={1}", LocalID, axis); |
284 | return; | 254 | return; |
285 | } | 255 | } |
286 | 256 | ||
287 | public override OMV.Vector3 Position { | 257 | public override OMV.Vector3 Position { |
288 | get { | 258 | get { |
289 | if (!_linkset.IsRoot(this)) | 259 | if (!Linkset.IsRoot(this)) |
290 | // child prims move around based on their parent. Need to get the latest location | 260 | // child prims move around based on their parent. Need to get the latest location |
291 | _position = BulletSimAPI.GetObjectPosition(_scene.WorldID, _localID); | 261 | _position = BulletSimAPI.GetPosition2(BSBody.ptr); |
292 | 262 | ||
293 | // don't do the GetObjectPosition for root elements because this function is called a zillion times | 263 | // don't do the GetObjectPosition for root elements because this function is called a zillion times |
294 | // _position = BulletSimAPI.GetObjectPosition(_scene.WorldID, _localID); | 264 | // _position = BulletSimAPI.GetObjectPosition2(PhysicsScene.World.ptr, BSBody.ptr); |
295 | return _position; | 265 | return _position; |
296 | } | 266 | } |
297 | set { | 267 | set { |
268 | // If you must push the position into the physics engine, use ForcePosition. | ||
269 | if (_position == value) | ||
270 | { | ||
271 | return; | ||
272 | } | ||
298 | _position = value; | 273 | _position = value; |
299 | // TODO: what does it mean to set the position of a child prim?? Rebuild the constraint? | 274 | // TODO: what does it mean to set the position of a child prim?? Rebuild the constraint? |
300 | _scene.TaintedObject("BSPrim.setPosition", delegate() | 275 | PositionSanityCheck(); |
276 | PhysicsScene.TaintedObject("BSPrim.setPosition", delegate() | ||
301 | { | 277 | { |
302 | // DetailLog("{0},BSPrim.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation); | 278 | // DetailLog("{0},BSPrim.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation); |
303 | BulletSimAPI.SetObjectTranslation(_scene.WorldID, _localID, _position, _orientation); | 279 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); |
304 | }); | 280 | }); |
305 | } | 281 | } |
282 | } | ||
283 | public override OMV.Vector3 ForcePosition { | ||
284 | get { | ||
285 | _position = BulletSimAPI.GetPosition2(BSBody.ptr); | ||
286 | return _position; | ||
287 | } | ||
288 | set { | ||
289 | _position = value; | ||
290 | PositionSanityCheck(); | ||
291 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); | ||
292 | } | ||
293 | } | ||
294 | |||
295 | // Check that the current position is sane and, if not, modify the position to make it so. | ||
296 | // Check for being below terrain and being out of bounds. | ||
297 | // Returns 'true' of the position was made sane by some action. | ||
298 | private bool PositionSanityCheck() | ||
299 | { | ||
300 | bool ret = false; | ||
301 | |||
302 | // If totally below the ground, move the prim up | ||
303 | // TODO: figure out the right solution for this... only for dynamic objects? | ||
304 | /* | ||
305 | float terrainHeight = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(_position); | ||
306 | if (Position.Z < terrainHeight) | ||
307 | { | ||
308 | DetailLog("{0},BSPrim.PositionAdjustUnderGround,call,pos={1},terrain={2}", LocalID, _position, terrainHeight); | ||
309 | _position.Z = terrainHeight + 2.0f; | ||
310 | ret = true; | ||
311 | } | ||
312 | */ | ||
313 | if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0) | ||
314 | { | ||
315 | float waterHeight = PhysicsScene.GetWaterLevelAtXYZ(_position); | ||
316 | if (Position.Z < waterHeight) | ||
317 | { | ||
318 | _position.Z = waterHeight; | ||
319 | ret = true; | ||
320 | } | ||
321 | } | ||
322 | |||
323 | // TODO: check for out of bounds | ||
324 | return ret; | ||
325 | } | ||
326 | |||
327 | // A version of the sanity check that also makes sure a new position value is | ||
328 | // pushed back to the physics engine. This routine would be used by anyone | ||
329 | // who is not already pushing the value. | ||
330 | private bool PositionSanityCheck2(bool inTaintTime) | ||
331 | { | ||
332 | bool ret = false; | ||
333 | if (PositionSanityCheck()) | ||
334 | { | ||
335 | // The new position value must be pushed into the physics engine but we can't | ||
336 | // just assign to "Position" because of potential call loops. | ||
337 | BSScene.TaintCallback sanityOperation = delegate() | ||
338 | { | ||
339 | DetailLog("{0},BSPrim.PositionSanityCheck,taint,pos={1},orient={2}", LocalID, _position, _orientation); | ||
340 | BulletSimAPI.SetObjectTranslation(PhysicsScene.WorldID, LocalID, _position, _orientation); | ||
341 | }; | ||
342 | if (inTaintTime) | ||
343 | sanityOperation(); | ||
344 | else | ||
345 | PhysicsScene.TaintedObject("BSPrim.PositionSanityCheck", sanityOperation); | ||
346 | |||
347 | ret = true; | ||
348 | } | ||
349 | return ret; | ||
306 | } | 350 | } |
307 | 351 | ||
308 | // Return the effective mass of the object. | 352 | // Return the effective mass of the object. |
309 | // If there are multiple items in the linkset, add them together for the root | 353 | // If there are multiple items in the linkset, add them together for the root |
310 | public override float Mass | 354 | public override float Mass |
311 | { | 355 | { |
312 | get | 356 | get |
313 | { | 357 | { |
314 | return _linkset.LinksetMass; | 358 | // return Linkset.LinksetMass; |
359 | return _mass; | ||
315 | } | 360 | } |
316 | } | 361 | } |
317 | 362 | ||
318 | // used when we only want this prim's mass and not the linkset thing | 363 | // used when we only want this prim's mass and not the linkset thing |
319 | public float MassRaw { get { return _mass; } } | 364 | public override float MassRaw { get { return _mass; } } |
320 | 365 | ||
321 | // Is this used? | 366 | // Is this used? |
322 | public override OMV.Vector3 CenterOfMass | 367 | public override OMV.Vector3 CenterOfMass |
323 | { | 368 | { |
324 | get { return _linkset.CenterOfMass; } | 369 | get { return Linkset.CenterOfMass; } |
325 | } | 370 | } |
326 | 371 | ||
327 | // Is this used? | 372 | // Is this used? |
328 | public override OMV.Vector3 GeometricCenter | 373 | public override OMV.Vector3 GeometricCenter |
329 | { | 374 | { |
330 | get { return _linkset.GeometricCenter; } | 375 | get { return Linkset.GeometricCenter; } |
331 | } | 376 | } |
332 | 377 | ||
333 | public override OMV.Vector3 Force { | 378 | public override OMV.Vector3 Force { |
334 | get { return _force; } | 379 | get { return _force; } |
335 | set { | 380 | set { |
336 | _force = value; | 381 | _force = value; |
337 | _scene.TaintedObject("BSPrim.setForce", delegate() | 382 | PhysicsScene.TaintedObject("BSPrim.setForce", delegate() |
338 | { | 383 | { |
339 | // DetailLog("{0},BSPrim.setForce,taint,force={1}", LocalID, _force); | 384 | // DetailLog("{0},BSPrim.setForce,taint,force={1}", LocalID, _force); |
340 | // BulletSimAPI.SetObjectForce(_scene.WorldID, _localID, _force); | 385 | BulletSimAPI.SetObjectForce2(BSBody.ptr, _force); |
341 | BulletSimAPI.SetObjectForce2(Body.Ptr, _force); | ||
342 | }); | 386 | }); |
343 | } | 387 | } |
344 | } | 388 | } |
345 | 389 | ||
346 | public override int VehicleType { | 390 | public override int VehicleType { |
347 | get { | 391 | get { |
348 | return (int)_vehicle.Type; // if we are a vehicle, return that type | 392 | return (int)_vehicle.Type; // if we are a vehicle, return that type |
349 | } | 393 | } |
350 | set { | 394 | set { |
351 | Vehicle type = (Vehicle)value; | 395 | Vehicle type = (Vehicle)value; |
352 | BSPrim vehiclePrim = this; | 396 | |
353 | _scene.TaintedObject("setVehicleType", delegate() | 397 | // Tell the scene about the vehicle so it will get processing each frame. |
398 | PhysicsScene.VehicleInSceneTypeChanged(this, type); | ||
399 | |||
400 | PhysicsScene.TaintedObject("setVehicleType", delegate() | ||
354 | { | 401 | { |
355 | // Done at taint time so we're sure the physics engine is not using the variables | 402 | // Done at taint time so we're sure the physics engine is not using the variables |
356 | // Vehicle code changes the parameters for this vehicle type. | 403 | // Vehicle code changes the parameters for this vehicle type. |
357 | _vehicle.ProcessTypeChange(type); | 404 | this._vehicle.ProcessTypeChange(type); |
358 | // Tell the scene about the vehicle so it will get processing each frame. | ||
359 | _scene.VehicleInSceneTypeChanged(this, type); | ||
360 | }); | 405 | }); |
361 | } | 406 | } |
362 | } | 407 | } |
363 | public override void VehicleFloatParam(int param, float value) | 408 | public override void VehicleFloatParam(int param, float value) |
364 | { | 409 | { |
365 | _scene.TaintedObject("BSPrim.VehicleFloatParam", delegate() | 410 | PhysicsScene.TaintedObject("BSPrim.VehicleFloatParam", delegate() |
366 | { | 411 | { |
367 | _vehicle.ProcessFloatVehicleParam((Vehicle)param, value, _scene.LastSimulatedTimestep); | 412 | _vehicle.ProcessFloatVehicleParam((Vehicle)param, value); |
368 | }); | 413 | }); |
369 | } | 414 | } |
370 | public override void VehicleVectorParam(int param, OMV.Vector3 value) | 415 | public override void VehicleVectorParam(int param, OMV.Vector3 value) |
371 | { | 416 | { |
372 | _scene.TaintedObject("BSPrim.VehicleVectorParam", delegate() | 417 | PhysicsScene.TaintedObject("BSPrim.VehicleVectorParam", delegate() |
373 | { | 418 | { |
374 | _vehicle.ProcessVectorVehicleParam((Vehicle)param, value, _scene.LastSimulatedTimestep); | 419 | _vehicle.ProcessVectorVehicleParam((Vehicle)param, value); |
375 | }); | 420 | }); |
376 | } | 421 | } |
377 | public override void VehicleRotationParam(int param, OMV.Quaternion rotation) | 422 | public override void VehicleRotationParam(int param, OMV.Quaternion rotation) |
378 | { | 423 | { |
379 | _scene.TaintedObject("BSPrim.VehicleRotationParam", delegate() | 424 | PhysicsScene.TaintedObject("BSPrim.VehicleRotationParam", delegate() |
380 | { | 425 | { |
381 | _vehicle.ProcessRotationVehicleParam((Vehicle)param, rotation); | 426 | _vehicle.ProcessRotationVehicleParam((Vehicle)param, rotation); |
382 | }); | 427 | }); |
383 | } | 428 | } |
384 | public override void VehicleFlags(int param, bool remove) | 429 | public override void VehicleFlags(int param, bool remove) |
385 | { | 430 | { |
386 | _scene.TaintedObject("BSPrim.VehicleFlags", delegate() | 431 | PhysicsScene.TaintedObject("BSPrim.VehicleFlags", delegate() |
387 | { | 432 | { |
388 | _vehicle.ProcessVehicleFlags(param, remove); | 433 | _vehicle.ProcessVehicleFlags(param, remove); |
389 | }); | 434 | }); |
@@ -391,83 +436,114 @@ public sealed class BSPrim : PhysicsActor | |||
391 | 436 | ||
392 | // Called each simulation step to advance vehicle characteristics. | 437 | // Called each simulation step to advance vehicle characteristics. |
393 | // Called from Scene when doing simulation step so we're in taint processing time. | 438 | // Called from Scene when doing simulation step so we're in taint processing time. |
394 | public void StepVehicle(float timeStep) | 439 | public override void StepVehicle(float timeStep) |
395 | { | 440 | { |
396 | if (IsPhysical) | 441 | if (IsPhysical) |
442 | { | ||
397 | _vehicle.Step(timeStep); | 443 | _vehicle.Step(timeStep); |
444 | } | ||
398 | } | 445 | } |
399 | 446 | ||
400 | // Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more | 447 | // Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more |
401 | public override void SetVolumeDetect(int param) { | 448 | public override void SetVolumeDetect(int param) { |
402 | bool newValue = (param != 0); | 449 | bool newValue = (param != 0); |
403 | _isVolumeDetect = newValue; | 450 | if (_isVolumeDetect != newValue) |
404 | _scene.TaintedObject("BSPrim.SetVolumeDetect", delegate() | ||
405 | { | 451 | { |
406 | SetObjectDynamic(); | 452 | _isVolumeDetect = newValue; |
407 | }); | 453 | PhysicsScene.TaintedObject("BSPrim.SetVolumeDetect", delegate() |
408 | return; | 454 | { |
455 | // DetailLog("{0},setVolumeDetect,taint,volDetect={1}", LocalID, _isVolumeDetect); | ||
456 | SetObjectDynamic(true); | ||
457 | }); | ||
458 | } | ||
459 | return; | ||
409 | } | 460 | } |
410 | 461 | public override OMV.Vector3 Velocity { | |
411 | public override OMV.Vector3 Velocity { | 462 | get { return _velocity; } |
412 | get { return _velocity; } | ||
413 | set { | 463 | set { |
414 | _velocity = value; | 464 | _velocity = value; |
415 | _scene.TaintedObject("BSPrim.setVelocity", delegate() | 465 | PhysicsScene.TaintedObject("BSPrim.setVelocity", delegate() |
416 | { | 466 | { |
417 | // DetailLog("{0},BSPrim.SetVelocity,taint,vel={1}", LocalID, _velocity); | 467 | // DetailLog("{0},BSPrim.SetVelocity,taint,vel={1}", LocalID, _velocity); |
418 | BulletSimAPI.SetObjectVelocity(_scene.WorldID, LocalID, _velocity); | 468 | BulletSimAPI.SetLinearVelocity2(BSBody.ptr, _velocity); |
419 | }); | 469 | }); |
420 | } | 470 | } |
421 | } | 471 | } |
422 | public override OMV.Vector3 Torque { | 472 | public override OMV.Vector3 ForceVelocity { |
423 | get { return _torque; } | 473 | get { return _velocity; } |
424 | set { _torque = value; | 474 | set { |
475 | _velocity = value; | ||
476 | BulletSimAPI.SetLinearVelocity2(BSBody.ptr, _velocity); | ||
477 | } | ||
478 | } | ||
479 | public override OMV.Vector3 Torque { | ||
480 | get { return _torque; } | ||
481 | set { _torque = value; | ||
425 | // DetailLog("{0},BSPrim.SetTorque,call,torque={1}", LocalID, _torque); | 482 | // DetailLog("{0},BSPrim.SetTorque,call,torque={1}", LocalID, _torque); |
426 | } | 483 | } |
427 | } | 484 | } |
428 | public override float CollisionScore { | 485 | public override float CollisionScore { |
429 | get { return _collisionScore; } | 486 | get { return _collisionScore; } |
430 | set { _collisionScore = value; | 487 | set { _collisionScore = value; |
431 | } | 488 | } |
432 | } | 489 | } |
433 | public override OMV.Vector3 Acceleration { | 490 | public override OMV.Vector3 Acceleration { |
434 | get { return _acceleration; } | 491 | get { return _acceleration; } |
435 | set { _acceleration = value; } | 492 | set { _acceleration = value; } |
436 | } | 493 | } |
437 | public override OMV.Quaternion Orientation { | 494 | public override OMV.Quaternion Orientation { |
438 | get { | 495 | get { |
439 | if (!_linkset.IsRoot(this)) | 496 | if (!Linkset.IsRoot(this)) |
440 | { | 497 | { |
441 | // Children move around because tied to parent. Get a fresh value. | 498 | // Children move around because tied to parent. Get a fresh value. |
442 | _orientation = BulletSimAPI.GetObjectOrientation(_scene.WorldID, LocalID); | 499 | _orientation = BulletSimAPI.GetOrientation2(BSBody.ptr); |
443 | } | 500 | } |
444 | return _orientation; | 501 | return _orientation; |
445 | } | 502 | } |
446 | set { | 503 | set { |
504 | if (_orientation == value) | ||
505 | return; | ||
447 | _orientation = value; | 506 | _orientation = value; |
448 | // TODO: what does it mean if a child in a linkset changes its orientation? Rebuild the constraint? | 507 | // TODO: what does it mean if a child in a linkset changes its orientation? Rebuild the constraint? |
449 | _scene.TaintedObject("BSPrim.setOrientation", delegate() | 508 | PhysicsScene.TaintedObject("BSPrim.setOrientation", delegate() |
450 | { | 509 | { |
451 | // _position = BulletSimAPI.GetObjectPosition(_scene.WorldID, _localID); | 510 | // _position = BulletSimAPI.GetObjectPosition2(PhysicsScene.World.ptr, BSBody.ptr); |
452 | // DetailLog("{0},BSPrim.setOrientation,taint,pos={1},orient={2}", LocalID, _position, _orientation); | 511 | // DetailLog("{0},BSPrim.setOrientation,taint,pos={1},orient={2}", LocalID, _position, _orientation); |
453 | BulletSimAPI.SetObjectTranslation(_scene.WorldID, _localID, _position, _orientation); | 512 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); |
454 | }); | 513 | }); |
455 | } | 514 | } |
515 | } | ||
516 | // Go directly to Bullet to get/set the value. | ||
517 | public override OMV.Quaternion ForceOrientation | ||
518 | { | ||
519 | get | ||
520 | { | ||
521 | _orientation = BulletSimAPI.GetOrientation2(BSBody.ptr); | ||
522 | return _orientation; | ||
523 | } | ||
524 | set | ||
525 | { | ||
526 | _orientation = value; | ||
527 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); | ||
528 | } | ||
456 | } | 529 | } |
457 | public override int PhysicsActorType { | 530 | public override int PhysicsActorType { |
458 | get { return _physicsActorType; } | 531 | get { return _physicsActorType; } |
459 | set { _physicsActorType = value; | 532 | set { _physicsActorType = value; } |
460 | } | ||
461 | } | 533 | } |
462 | public override bool IsPhysical { | 534 | public override bool IsPhysical { |
463 | get { return _isPhysical; } | 535 | get { return _isPhysical; } |
464 | set { | 536 | set { |
465 | _isPhysical = value; | 537 | if (_isPhysical != value) |
466 | _scene.TaintedObject("BSPrim.setIsPhysical", delegate() | ||
467 | { | 538 | { |
468 | SetObjectDynamic(); | 539 | _isPhysical = value; |
469 | }); | 540 | PhysicsScene.TaintedObject("BSPrim.setIsPhysical", delegate() |
470 | } | 541 | { |
542 | // DetailLog("{0},setIsPhysical,taint,isPhys={1}", LocalID, _isPhysical); | ||
543 | SetObjectDynamic(true); | ||
544 | }); | ||
545 | } | ||
546 | } | ||
471 | } | 547 | } |
472 | 548 | ||
473 | // An object is static (does not move) if selected or not physical | 549 | // An object is static (does not move) if selected or not physical |
@@ -477,57 +553,201 @@ public sealed class BSPrim : PhysicsActor | |||
477 | } | 553 | } |
478 | 554 | ||
479 | // An object is solid if it's not phantom and if it's not doing VolumeDetect | 555 | // An object is solid if it's not phantom and if it's not doing VolumeDetect |
480 | private bool IsSolid | 556 | public bool IsSolid |
481 | { | 557 | { |
482 | get { return !IsPhantom && !_isVolumeDetect; } | 558 | get { return !IsPhantom && !_isVolumeDetect; } |
483 | } | 559 | } |
484 | 560 | ||
485 | // Make gravity work if the object is physical and not selected | 561 | // Make gravity work if the object is physical and not selected |
486 | // No locking here because only called when it is safe | 562 | // Called at taint-time!! |
487 | private void SetObjectDynamic() | 563 | private void SetObjectDynamic(bool forceRebuild) |
564 | { | ||
565 | // Recreate the physical object if necessary | ||
566 | CreateGeomAndObject(forceRebuild); | ||
567 | } | ||
568 | |||
569 | // Convert the simulator's physical properties into settings on BulletSim objects. | ||
570 | // There are four flags we're interested in: | ||
571 | // IsStatic: Object does not move, otherwise the object has mass and moves | ||
572 | // isSolid: other objects bounce off of this object | ||
573 | // isVolumeDetect: other objects pass through but can generate collisions | ||
574 | // collisionEvents: whether this object returns collision events | ||
575 | private void UpdatePhysicalParameters() | ||
576 | { | ||
577 | // DetailLog("{0},BSPrim.UpdatePhysicalParameters,entry,body={1},shape={2}", LocalID, BSBody, BSShape); | ||
578 | |||
579 | // Mangling all the physical properties requires the object not be in the physical world. | ||
580 | // This is a NOOP if the object is not in the world (BulletSim and Bullet ignore objects not found). | ||
581 | BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, BSBody.ptr); | ||
582 | |||
583 | // Set up the object physicalness (does gravity and collisions move this object) | ||
584 | MakeDynamic(IsStatic); | ||
585 | |||
586 | // Update vehicle specific parameters (after MakeDynamic() so can change physical parameters) | ||
587 | _vehicle.Refresh(); | ||
588 | |||
589 | // Arrange for collision events if the simulator wants them | ||
590 | EnableCollisions(SubscribedEvents()); | ||
591 | |||
592 | // Make solid or not (do things bounce off or pass through this object). | ||
593 | MakeSolid(IsSolid); | ||
594 | |||
595 | BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, BSBody.ptr); | ||
596 | |||
597 | // Rebuild its shape | ||
598 | BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, BSBody.ptr); | ||
599 | |||
600 | // Collision filter can be set only when the object is in the world | ||
601 | if (BSBody.collisionFilter != 0 || BSBody.collisionMask != 0) | ||
602 | { | ||
603 | BulletSimAPI.SetCollisionFilterMask2(BSBody.ptr, (uint)BSBody.collisionFilter, (uint)BSBody.collisionMask); | ||
604 | } | ||
605 | |||
606 | // Recompute any linkset parameters. | ||
607 | // When going from non-physical to physical, this re-enables the constraints that | ||
608 | // had been automatically disabled when the mass was set to zero. | ||
609 | Linkset.Refresh(this, true); | ||
610 | |||
611 | DetailLog("{0},BSPrim.UpdatePhysicalParameters,exit,static={1},solid={2},mass={3},collide={4},cf={5:X},body={6},shape={7}", | ||
612 | LocalID, IsStatic, IsSolid, _mass, SubscribedEvents(), CurrentCollisionFlags, BSBody, BSShape); | ||
613 | } | ||
614 | |||
615 | // "Making dynamic" means changing to and from static. | ||
616 | // When static, gravity does not effect the object and it is fixed in space. | ||
617 | // When dynamic, the object can fall and be pushed by others. | ||
618 | // This is independent of its 'solidness' which controls what passes through | ||
619 | // this object and what interacts with it. | ||
620 | private void MakeDynamic(bool makeStatic) | ||
488 | { | 621 | { |
489 | // RA: remove this for the moment. | 622 | if (makeStatic) |
490 | // The problem is that dynamic objects are hulls so if we are becoming physical | 623 | { |
491 | // the shape has to be checked and possibly built. | 624 | // Become a Bullet 'static' object type |
492 | // Maybe a VerifyCorrectPhysicalShape() routine? | 625 | CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(BSBody.ptr, CollisionFlags.CF_STATIC_OBJECT); |
493 | // RecreateGeomAndObject(); | 626 | // Stop all movement |
627 | BulletSimAPI.ClearAllForces2(BSBody.ptr); | ||
628 | // Center of mass is at the center of the object | ||
629 | BulletSimAPI.SetCenterOfMassByPosRot2(Linkset.LinksetRoot.BSBody.ptr, _position, _orientation); | ||
630 | // Mass is zero which disables a bunch of physics stuff in Bullet | ||
631 | BulletSimAPI.SetMassProps2(BSBody.ptr, 0f, OMV.Vector3.Zero); | ||
632 | // There is no inertia in a static object | ||
633 | BulletSimAPI.UpdateInertiaTensor2(BSBody.ptr); | ||
634 | // There can be special things needed for implementing linksets | ||
635 | Linkset.MakeStatic(this); | ||
636 | // The activation state is 'disabled' so Bullet will not try to act on it. | ||
637 | BulletSimAPI.ForceActivationState2(BSBody.ptr, ActivationState.DISABLE_SIMULATION); | ||
638 | // Start it out sleeping and physical actions could wake it up. | ||
639 | // BulletSimAPI.ForceActivationState2(BSBody.ptr, ActivationState.ISLAND_SLEEPING); | ||
640 | |||
641 | BSBody.collisionFilter = CollisionFilterGroups.StaticObjectFilter; | ||
642 | BSBody.collisionMask = CollisionFilterGroups.StaticObjectMask; | ||
643 | } | ||
644 | else | ||
645 | { | ||
646 | // Not a Bullet static object | ||
647 | CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(BSBody.ptr, CollisionFlags.CF_STATIC_OBJECT); | ||
648 | |||
649 | // Set various physical properties so internal dynamic properties will get computed correctly as they are set | ||
650 | BulletSimAPI.SetFriction2(BSBody.ptr, PhysicsScene.Params.defaultFriction); | ||
651 | BulletSimAPI.SetRestitution2(BSBody.ptr, PhysicsScene.Params.defaultRestitution); | ||
652 | |||
653 | // per http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=3382 | ||
654 | BulletSimAPI.ClearAllForces2(BSBody.ptr); | ||
655 | |||
656 | // For good measure, make sure the transform is set through to the motion state | ||
657 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); | ||
658 | |||
659 | // A dynamic object has mass | ||
660 | IntPtr collisionShapePtr = BulletSimAPI.GetCollisionShape2(BSBody.ptr); | ||
661 | OMV.Vector3 inertia = BulletSimAPI.CalculateLocalInertia2(collisionShapePtr, Mass); | ||
662 | BulletSimAPI.SetMassProps2(BSBody.ptr, _mass, inertia); | ||
663 | BulletSimAPI.UpdateInertiaTensor2(BSBody.ptr); | ||
494 | 664 | ||
495 | // Bullet wants static objects to have a mass of zero | 665 | // Various values for simulation limits |
496 | float mass = IsStatic ? 0f : _mass; | 666 | BulletSimAPI.SetDamping2(BSBody.ptr, PhysicsScene.Params.linearDamping, PhysicsScene.Params.angularDamping); |
667 | BulletSimAPI.SetDeactivationTime2(BSBody.ptr, PhysicsScene.Params.deactivationTime); | ||
668 | BulletSimAPI.SetSleepingThresholds2(BSBody.ptr, PhysicsScene.Params.linearSleepingThreshold, PhysicsScene.Params.angularSleepingThreshold); | ||
669 | BulletSimAPI.SetContactProcessingThreshold2(BSBody.ptr, PhysicsScene.Params.contactProcessingThreshold); | ||
497 | 670 | ||
498 | BulletSimAPI.SetObjectProperties(_scene.WorldID, LocalID, IsStatic, IsSolid, SubscribedEvents(), mass); | 671 | // There might be special things needed for implementing linksets. |
672 | Linkset.MakeDynamic(this); | ||
673 | |||
674 | // Force activation of the object so Bullet will act on it. | ||
675 | // Must do the ForceActivationState2() to overcome the DISABLE_SIMULATION from static objects. | ||
676 | BulletSimAPI.ForceActivationState2(BSBody.ptr, ActivationState.ACTIVE_TAG); | ||
677 | // BulletSimAPI.Activate2(BSBody.ptr, true); | ||
678 | |||
679 | BSBody.collisionFilter = CollisionFilterGroups.ObjectFilter; | ||
680 | BSBody.collisionMask = CollisionFilterGroups.ObjectMask; | ||
681 | } | ||
682 | } | ||
499 | 683 | ||
500 | // recompute any linkset parameters | 684 | // "Making solid" means that other object will not pass through this object. |
501 | _linkset.Refresh(this); | 685 | // To make transparent, we create a Bullet ghost object. |
686 | // Note: This expects to be called from the UpdatePhysicalParameters() routine as | ||
687 | // the functions after this one set up the state of a possibly newly created collision body. | ||
688 | private void MakeSolid(bool makeSolid) | ||
689 | { | ||
690 | CollisionObjectTypes bodyType = (CollisionObjectTypes)BulletSimAPI.GetBodyType2(BSBody.ptr); | ||
691 | if (makeSolid) | ||
692 | { | ||
693 | // Verify the previous code created the correct shape for this type of thing. | ||
694 | if ((bodyType & CollisionObjectTypes.CO_RIGID_BODY) == 0) | ||
695 | { | ||
696 | m_log.ErrorFormat("{0} MakeSolid: physical body of wrong type for solidity. id={1}, type={2}", LogHeader, LocalID, bodyType); | ||
697 | } | ||
698 | CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(BSBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); | ||
699 | } | ||
700 | else | ||
701 | { | ||
702 | if ((bodyType & CollisionObjectTypes.CO_GHOST_OBJECT) == 0) | ||
703 | { | ||
704 | m_log.ErrorFormat("{0} MakeSolid: physical body of wrong type for non-solidness. id={1}, type={2}", LogHeader, LocalID, bodyType); | ||
705 | } | ||
706 | CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(BSBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); | ||
707 | BSBody.collisionFilter = CollisionFilterGroups.VolumeDetectFilter; | ||
708 | BSBody.collisionMask = CollisionFilterGroups.VolumeDetectMask; | ||
709 | } | ||
710 | } | ||
502 | 711 | ||
503 | CollisionFlags cf = BulletSimAPI.GetCollisionFlags2(Body.Ptr); | 712 | // Turn on or off the flag controlling whether collision events are returned to the simulator. |
504 | // DetailLog("{0},BSPrim.SetObjectDynamic,taint,static={1},solid={2},mass={3}, cf={4}", LocalID, IsStatic, IsSolid, mass, cf); | 713 | private void EnableCollisions(bool wantsCollisionEvents) |
714 | { | ||
715 | if (wantsCollisionEvents) | ||
716 | { | ||
717 | CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(BSBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); | ||
718 | } | ||
719 | else | ||
720 | { | ||
721 | CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(BSBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); | ||
722 | } | ||
505 | } | 723 | } |
506 | 724 | ||
507 | // prims don't fly | 725 | // prims don't fly |
508 | public override bool Flying { | 726 | public override bool Flying { |
509 | get { return _flying; } | 727 | get { return _flying; } |
510 | set { _flying = value; } | 728 | set { |
729 | _flying = value; | ||
730 | } | ||
511 | } | 731 | } |
512 | public override bool SetAlwaysRun { | 732 | public override bool SetAlwaysRun { |
513 | get { return _setAlwaysRun; } | 733 | get { return _setAlwaysRun; } |
514 | set { _setAlwaysRun = value; } | 734 | set { _setAlwaysRun = value; } |
515 | } | 735 | } |
516 | public override bool ThrottleUpdates { | 736 | public override bool ThrottleUpdates { |
517 | get { return _throttleUpdates; } | 737 | get { return _throttleUpdates; } |
518 | set { _throttleUpdates = value; } | 738 | set { _throttleUpdates = value; } |
519 | } | 739 | } |
520 | public override bool IsColliding { | 740 | public override bool IsColliding { |
521 | get { return (_collidingStep == _scene.SimulationStep); } | 741 | get { return (CollidingStep == PhysicsScene.SimulationStep); } |
522 | set { _isColliding = value; } | 742 | set { _isColliding = value; } |
523 | } | 743 | } |
524 | public override bool CollidingGround { | 744 | public override bool CollidingGround { |
525 | get { return (_collidingGroundStep == _scene.SimulationStep); } | 745 | get { return (CollidingGroundStep == PhysicsScene.SimulationStep); } |
526 | set { _collidingGround = value; } | 746 | set { _collidingGround = value; } |
527 | } | 747 | } |
528 | public override bool CollidingObj { | 748 | public override bool CollidingObj { |
529 | get { return _collidingObj; } | 749 | get { return _collidingObj; } |
530 | set { _collidingObj = value; } | 750 | set { _collidingObj = value; } |
531 | } | 751 | } |
532 | public bool IsPhantom { | 752 | public bool IsPhantom { |
533 | get { | 753 | get { |
@@ -537,10 +757,19 @@ public sealed class BSPrim : PhysicsActor | |||
537 | return false; | 757 | return false; |
538 | } | 758 | } |
539 | } | 759 | } |
540 | public override bool FloatOnWater { | 760 | public override bool FloatOnWater { |
541 | set { _floatOnWater = value; } | 761 | set { |
762 | _floatOnWater = value; | ||
763 | PhysicsScene.TaintedObject("BSPrim.setFloatOnWater", delegate() | ||
764 | { | ||
765 | if (_floatOnWater) | ||
766 | CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(BSBody.ptr, CollisionFlags.BS_FLOATS_ON_WATER); | ||
767 | else | ||
768 | CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(BSBody.ptr, CollisionFlags.BS_FLOATS_ON_WATER); | ||
769 | }); | ||
770 | } | ||
542 | } | 771 | } |
543 | public override OMV.Vector3 RotationalVelocity { | 772 | public override OMV.Vector3 RotationalVelocity { |
544 | get { | 773 | get { |
545 | /* | 774 | /* |
546 | OMV.Vector3 pv = OMV.Vector3.Zero; | 775 | OMV.Vector3 pv = OMV.Vector3.Zero; |
@@ -552,58 +781,69 @@ public sealed class BSPrim : PhysicsActor | |||
552 | */ | 781 | */ |
553 | 782 | ||
554 | return _rotationalVelocity; | 783 | return _rotationalVelocity; |
555 | } | 784 | } |
556 | set { | 785 | set { |
557 | _rotationalVelocity = value; | 786 | _rotationalVelocity = value; |
558 | // m_log.DebugFormat("{0}: RotationalVelocity={1}", LogHeader, _rotationalVelocity); | 787 | // m_log.DebugFormat("{0}: RotationalVelocity={1}", LogHeader, _rotationalVelocity); |
559 | _scene.TaintedObject("BSPrim.setRotationalVelocity", delegate() | 788 | PhysicsScene.TaintedObject("BSPrim.setRotationalVelocity", delegate() |
560 | { | 789 | { |
561 | // DetailLog("{0},BSPrim.SetRotationalVel,taint,rotvel={1}", LocalID, _rotationalVelocity); | 790 | // DetailLog("{0},BSPrim.SetRotationalVel,taint,rotvel={1}", LocalID, _rotationalVelocity); |
562 | BulletSimAPI.SetObjectAngularVelocity(_scene.WorldID, LocalID, _rotationalVelocity); | 791 | BulletSimAPI.SetAngularVelocity2(BSBody.ptr, _rotationalVelocity); |
563 | }); | 792 | }); |
564 | } | 793 | } |
794 | } | ||
795 | public override OMV.Vector3 ForceRotationalVelocity { | ||
796 | get { | ||
797 | return _rotationalVelocity; | ||
798 | } | ||
799 | set { | ||
800 | _rotationalVelocity = value; | ||
801 | BulletSimAPI.SetAngularVelocity2(BSBody.ptr, _rotationalVelocity); | ||
802 | } | ||
565 | } | 803 | } |
566 | public override bool Kinematic { | 804 | public override bool Kinematic { |
567 | get { return _kinematic; } | 805 | get { return _kinematic; } |
568 | set { _kinematic = value; | 806 | set { _kinematic = value; |
569 | // m_log.DebugFormat("{0}: Kinematic={1}", LogHeader, _kinematic); | 807 | // m_log.DebugFormat("{0}: Kinematic={1}", LogHeader, _kinematic); |
570 | } | 808 | } |
571 | } | 809 | } |
572 | public override float Buoyancy { | 810 | public override float Buoyancy { |
573 | get { return _buoyancy; } | 811 | get { return _buoyancy; } |
574 | set { | 812 | set { |
575 | _buoyancy = value; | 813 | _buoyancy = value; |
576 | _scene.TaintedObject("BSPrim.setBuoyancy", delegate() | 814 | PhysicsScene.TaintedObject("BSPrim.setBuoyancy", delegate() |
577 | { | 815 | { |
578 | // DetailLog("{0},BSPrim.SetBuoyancy,taint,buoy={1}", LocalID, _buoyancy); | 816 | // DetailLog("{0},BSPrim.SetBuoyancy,taint,buoy={1}", LocalID, _buoyancy); |
579 | BulletSimAPI.SetObjectBuoyancy(_scene.WorldID, _localID, _buoyancy); | 817 | // Buoyancy is faked by changing the gravity applied to the object |
818 | float grav = PhysicsScene.Params.gravity * (1f - _buoyancy); | ||
819 | BulletSimAPI.SetGravity2(BSBody.ptr, new OMV.Vector3(0f, 0f, grav)); | ||
580 | }); | 820 | }); |
581 | } | 821 | } |
582 | } | 822 | } |
583 | 823 | ||
584 | // Used for MoveTo | 824 | // Used for MoveTo |
585 | public override OMV.Vector3 PIDTarget { | 825 | public override OMV.Vector3 PIDTarget { |
586 | set { _PIDTarget = value; } | 826 | set { _PIDTarget = value; } |
587 | } | 827 | } |
588 | public override bool PIDActive { | 828 | public override bool PIDActive { |
589 | set { _usePID = value; } | 829 | set { _usePID = value; } |
590 | } | 830 | } |
591 | public override float PIDTau { | 831 | public override float PIDTau { |
592 | set { _PIDTau = value; } | 832 | set { _PIDTau = value; } |
593 | } | 833 | } |
594 | 834 | ||
595 | // Used for llSetHoverHeight and maybe vehicle height | 835 | // Used for llSetHoverHeight and maybe vehicle height |
596 | // Hover Height will override MoveTo target's Z | 836 | // Hover Height will override MoveTo target's Z |
597 | public override bool PIDHoverActive { | 837 | public override bool PIDHoverActive { |
598 | set { _useHoverPID = value; } | 838 | set { _useHoverPID = value; } |
599 | } | 839 | } |
600 | public override float PIDHoverHeight { | 840 | public override float PIDHoverHeight { |
601 | set { _PIDHoverHeight = value; } | 841 | set { _PIDHoverHeight = value; } |
602 | } | 842 | } |
603 | public override PIDHoverType PIDHoverType { | 843 | public override PIDHoverType PIDHoverType { |
604 | set { _PIDHoverType = value; } | 844 | set { _PIDHoverType = value; } |
605 | } | 845 | } |
606 | public override float PIDHoverTau { | 846 | public override float PIDHoverTau { |
607 | set { _PIDHoverTao = value; } | 847 | set { _PIDHoverTao = value; } |
608 | } | 848 | } |
609 | 849 | ||
@@ -615,6 +855,9 @@ public sealed class BSPrim : PhysicsActor | |||
615 | 855 | ||
616 | private List<OMV.Vector3> m_accumulatedForces = new List<OMV.Vector3>(); | 856 | private List<OMV.Vector3> m_accumulatedForces = new List<OMV.Vector3>(); |
617 | public override void AddForce(OMV.Vector3 force, bool pushforce) { | 857 | public override void AddForce(OMV.Vector3 force, bool pushforce) { |
858 | AddForce(force, pushforce, false); | ||
859 | } | ||
860 | public void AddForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) { | ||
618 | // for an object, doesn't matter if force is a pushforce or not | 861 | // for an object, doesn't matter if force is a pushforce or not |
619 | if (force.IsFinite()) | 862 | if (force.IsFinite()) |
620 | { | 863 | { |
@@ -624,56 +867,38 @@ public sealed class BSPrim : PhysicsActor | |||
624 | } | 867 | } |
625 | else | 868 | else |
626 | { | 869 | { |
627 | m_log.WarnFormat("{0}: Got a NaN force applied to a Character", LogHeader); | 870 | m_log.WarnFormat("{0}: Got a NaN force applied to a prim. LocalID={1}", LogHeader, LocalID); |
628 | return; | 871 | return; |
629 | } | 872 | } |
630 | _scene.TaintedObject("BSPrim.AddForce", delegate() | 873 | BSScene.TaintCallback addForceOperation = delegate() |
631 | { | 874 | { |
632 | OMV.Vector3 fSum = OMV.Vector3.Zero; | 875 | OMV.Vector3 fSum = OMV.Vector3.Zero; |
633 | lock (m_accumulatedForces) | 876 | lock (m_accumulatedForces) |
634 | { | 877 | { |
878 | // Sum the accumulated additional forces for one big force to apply once. | ||
635 | foreach (OMV.Vector3 v in m_accumulatedForces) | 879 | foreach (OMV.Vector3 v in m_accumulatedForces) |
636 | { | 880 | { |
637 | fSum += v; | 881 | fSum += v; |
638 | } | 882 | } |
639 | m_accumulatedForces.Clear(); | 883 | m_accumulatedForces.Clear(); |
640 | } | 884 | } |
641 | // DetailLog("{0},BSPrim.AddObjectForce,taint,force={1}", LocalID, _force); | 885 | // DetailLog("{0},BSPrim.AddObjectForce,taint,force={1}", LocalID, fSum); |
642 | BulletSimAPI.AddObjectForce2(Body.Ptr, fSum); | 886 | // For unknown reasons, "ApplyCentralForce" adds this force to the total force on the object. |
643 | }); | 887 | BulletSimAPI.ApplyCentralForce2(BSBody.ptr, fSum); |
888 | }; | ||
889 | if (inTaintTime) | ||
890 | addForceOperation(); | ||
891 | else | ||
892 | PhysicsScene.TaintedObject("BSPrim.AddForce", addForceOperation); | ||
644 | } | 893 | } |
645 | 894 | ||
646 | public override void AddAngularForce(OMV.Vector3 force, bool pushforce) { | 895 | public override void AddAngularForce(OMV.Vector3 force, bool pushforce) { |
647 | // DetailLog("{0},BSPrim.AddAngularForce,call,angForce={1},push={2}", LocalID, force, pushforce); | 896 | // DetailLog("{0},BSPrim.AddAngularForce,call,angForce={1},push={2}", LocalID, force, pushforce); |
648 | // m_log.DebugFormat("{0}: AddAngularForce. f={1}, push={2}", LogHeader, force, pushforce); | 897 | // m_log.DebugFormat("{0}: AddAngularForce. f={1}, push={2}", LogHeader, force, pushforce); |
649 | } | 898 | } |
650 | public override void SetMomentum(OMV.Vector3 momentum) { | 899 | public override void SetMomentum(OMV.Vector3 momentum) { |
651 | // DetailLog("{0},BSPrim.SetMomentum,call,mom={1}", LocalID, momentum); | 900 | // DetailLog("{0},BSPrim.SetMomentum,call,mom={1}", LocalID, momentum); |
652 | } | 901 | } |
653 | public override void SubscribeEvents(int ms) { | ||
654 | _subscribedEventsMs = ms; | ||
655 | if (ms > 0) | ||
656 | { | ||
657 | // make sure first collision happens | ||
658 | _nextCollisionOkTime = Util.EnvironmentTickCount() - _subscribedEventsMs; | ||
659 | |||
660 | Scene.TaintedObject("BSPrim.SubscribeEvents", delegate() | ||
661 | { | ||
662 | BulletSimAPI.AddToCollisionFlags2(Body.Ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); | ||
663 | }); | ||
664 | } | ||
665 | } | ||
666 | public override void UnSubscribeEvents() { | ||
667 | _subscribedEventsMs = 0; | ||
668 | Scene.TaintedObject("BSPrim.UnSubscribeEvents", delegate() | ||
669 | { | ||
670 | BulletSimAPI.RemoveFromCollisionFlags2(Body.Ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); | ||
671 | }); | ||
672 | } | ||
673 | public override bool SubscribedEvents() { | ||
674 | return (_subscribedEventsMs > 0); | ||
675 | } | ||
676 | |||
677 | #region Mass Calculation | 902 | #region Mass Calculation |
678 | 903 | ||
679 | private float CalculateMass() | 904 | private float CalculateMass() |
@@ -683,8 +908,8 @@ public sealed class BSPrim : PhysicsActor | |||
683 | 908 | ||
684 | float returnMass = 0; | 909 | float returnMass = 0; |
685 | float hollowAmount = (float)_pbs.ProfileHollow * 2.0e-5f; | 910 | float hollowAmount = (float)_pbs.ProfileHollow * 2.0e-5f; |
686 | float hollowVolume = hollowAmount * hollowAmount; | 911 | float hollowVolume = hollowAmount * hollowAmount; |
687 | 912 | ||
688 | switch (_pbs.ProfileShape) | 913 | switch (_pbs.ProfileShape) |
689 | { | 914 | { |
690 | case ProfileShape.Square: | 915 | case ProfileShape.Square: |
@@ -720,16 +945,16 @@ public sealed class BSPrim : PhysicsActor | |||
720 | 945 | ||
721 | else if (_pbs.PathCurve == (byte)Extrusion.Curve1) | 946 | else if (_pbs.PathCurve == (byte)Extrusion.Curve1) |
722 | { | 947 | { |
723 | //a tube | 948 | //a tube |
724 | 949 | ||
725 | volume *= 0.78539816339e-2f * (float)(200 - _pbs.PathScaleX); | 950 | volume *= 0.78539816339e-2f * (float)(200 - _pbs.PathScaleX); |
726 | tmp= 1.0f -2.0e-2f * (float)(200 - _pbs.PathScaleY); | 951 | tmp= 1.0f -2.0e-2f * (float)(200 - _pbs.PathScaleY); |
727 | volume -= volume*tmp*tmp; | 952 | volume -= volume*tmp*tmp; |
728 | 953 | ||
729 | if (hollowAmount > 0.0) | 954 | if (hollowAmount > 0.0) |
730 | { | 955 | { |
731 | hollowVolume *= hollowAmount; | 956 | hollowVolume *= hollowAmount; |
732 | 957 | ||
733 | switch (_pbs.HollowShape) | 958 | switch (_pbs.HollowShape) |
734 | { | 959 | { |
735 | case HollowShape.Square: | 960 | case HollowShape.Square: |
@@ -788,7 +1013,7 @@ public sealed class BSPrim : PhysicsActor | |||
788 | volume *= 0.61685027506808491367715568749226e-2f * (float)(200 - _pbs.PathScaleX); | 1013 | volume *= 0.61685027506808491367715568749226e-2f * (float)(200 - _pbs.PathScaleX); |
789 | tmp = 1.0f - .02f * (float)(200 - _pbs.PathScaleY); | 1014 | tmp = 1.0f - .02f * (float)(200 - _pbs.PathScaleY); |
790 | volume *= (1.0f - tmp * tmp); | 1015 | volume *= (1.0f - tmp * tmp); |
791 | 1016 | ||
792 | if (hollowAmount > 0.0) | 1017 | if (hollowAmount > 0.0) |
793 | { | 1018 | { |
794 | 1019 | ||
@@ -967,296 +1192,65 @@ public sealed class BSPrim : PhysicsActor | |||
967 | if (returnMass <= 0) | 1192 | if (returnMass <= 0) |
968 | returnMass = 0.0001f; | 1193 | returnMass = 0.0001f; |
969 | 1194 | ||
970 | if (returnMass > _scene.MaximumObjectMass) | 1195 | if (returnMass > PhysicsScene.MaximumObjectMass) |
971 | returnMass = _scene.MaximumObjectMass; | 1196 | returnMass = PhysicsScene.MaximumObjectMass; |
972 | 1197 | ||
973 | return returnMass; | 1198 | return returnMass; |
974 | }// end CalculateMass | 1199 | }// end CalculateMass |
975 | #endregion Mass Calculation | 1200 | #endregion Mass Calculation |
976 | 1201 | ||
977 | // Create the geometry information in Bullet for later use | ||
978 | // The objects needs a hull if it's physical otherwise a mesh is enough | ||
979 | // No locking here because this is done when we know physics is not simulating | ||
980 | // if 'forceRebuild' is true, the geometry is rebuilt. Otherwise a previously built version is used | ||
981 | // Returns 'true' if the geometry was rebuilt | ||
982 | private bool CreateGeom(bool forceRebuild) | ||
983 | { | ||
984 | // the mesher thought this was too simple to mesh. Use a native Bullet collision shape. | ||
985 | bool ret = false; | ||
986 | if (!_scene.NeedsMeshing(_pbs)) | ||
987 | { | ||
988 | if (_pbs.ProfileShape == ProfileShape.HalfCircle && _pbs.PathCurve == (byte)Extrusion.Curve1) | ||
989 | { | ||
990 | // if (_size.X == _size.Y && _size.Y == _size.Z && _size.X == _size.Z) | ||
991 | // { | ||
992 | // m_log.DebugFormat("{0}: CreateGeom: Defaulting to sphere of size {1}", LogHeader, _size); | ||
993 | if (forceRebuild || (_shapeType != ShapeData.PhysicsShapeType.SHAPE_SPHERE)) | ||
994 | { | ||
995 | // DetailLog("{0},BSPrim.CreateGeom,sphere (force={1}", LocalID, forceRebuild); | ||
996 | _shapeType = ShapeData.PhysicsShapeType.SHAPE_SPHERE; | ||
997 | // Bullet native objects are scaled by the Bullet engine so pass the size in | ||
998 | _scale = _size; | ||
999 | // TODO: do we need to check for and destroy a mesh or hull that might have been left from before? | ||
1000 | ret = true; | ||
1001 | } | ||
1002 | // } | ||
1003 | } | ||
1004 | else | ||
1005 | { | ||
1006 | // m_log.DebugFormat("{0}: CreateGeom: Defaulting to box. lid={1}, type={2}, size={3}", LogHeader, LocalID, _shapeType, _size); | ||
1007 | if (forceRebuild || (_shapeType != ShapeData.PhysicsShapeType.SHAPE_BOX)) | ||
1008 | { | ||
1009 | // DetailLog("{0},BSPrim.CreateGeom,box (force={1})", LocalID, forceRebuild); | ||
1010 | _shapeType = ShapeData.PhysicsShapeType.SHAPE_BOX; | ||
1011 | _scale = _size; | ||
1012 | // TODO: do we need to check for and destroy a mesh or hull that might have been left from before? | ||
1013 | ret = true; | ||
1014 | } | ||
1015 | } | ||
1016 | } | ||
1017 | else | ||
1018 | { | ||
1019 | if (IsPhysical) | ||
1020 | { | ||
1021 | if (forceRebuild || _hullKey == 0) | ||
1022 | { | ||
1023 | // physical objects require a hull for interaction. | ||
1024 | // This will create the mesh if it doesn't already exist | ||
1025 | CreateGeomHull(); | ||
1026 | ret = true; | ||
1027 | } | ||
1028 | } | ||
1029 | else | ||
1030 | { | ||
1031 | if (forceRebuild || _meshKey == 0) | ||
1032 | { | ||
1033 | // Static (non-physical) objects only need a mesh for bumping into | ||
1034 | CreateGeomMesh(); | ||
1035 | ret = true; | ||
1036 | } | ||
1037 | } | ||
1038 | } | ||
1039 | return ret; | ||
1040 | } | ||
1041 | |||
1042 | // No locking here because this is done when we know physics is not simulating | ||
1043 | private void CreateGeomMesh() | ||
1044 | { | ||
1045 | float lod = _pbs.SculptEntry ? _scene.SculptLOD : _scene.MeshLOD; | ||
1046 | ulong newMeshKey = (ulong)_pbs.GetMeshKey(_size, lod); | ||
1047 | // m_log.DebugFormat("{0}: CreateGeomMesh: lID={1}, oldKey={2}, newKey={3}", LogHeader, _localID, _meshKey, newMeshKey); | ||
1048 | |||
1049 | // if this new shape is the same as last time, don't recreate the mesh | ||
1050 | if (_meshKey == newMeshKey) return; | ||
1051 | |||
1052 | // DetailLog("{0},BSPrim.CreateGeomMesh,create,key={1}", LocalID, newMeshKey); | ||
1053 | // Since we're recreating new, get rid of any previously generated shape | ||
1054 | if (_meshKey != 0) | ||
1055 | { | ||
1056 | // m_log.DebugFormat("{0}: CreateGeom: deleting old mesh. lID={1}, Key={2}", LogHeader, _localID, _meshKey); | ||
1057 | // DetailLog("{0},BSPrim.CreateGeomMesh,deleteOld,key={1}", LocalID, _meshKey); | ||
1058 | BulletSimAPI.DestroyMesh(_scene.WorldID, _meshKey); | ||
1059 | _mesh = null; | ||
1060 | _meshKey = 0; | ||
1061 | } | ||
1062 | |||
1063 | _meshKey = newMeshKey; | ||
1064 | // always pass false for physicalness as this creates some sort of bounding box which we don't need | ||
1065 | _mesh = _scene.mesher.CreateMesh(_avName, _pbs, _size, lod, false); | ||
1066 | |||
1067 | int[] indices = _mesh.getIndexListAsInt(); | ||
1068 | List<OMV.Vector3> vertices = _mesh.getVertexList(); | ||
1069 | |||
1070 | float[] verticesAsFloats = new float[vertices.Count * 3]; | ||
1071 | int vi = 0; | ||
1072 | foreach (OMV.Vector3 vv in vertices) | ||
1073 | { | ||
1074 | verticesAsFloats[vi++] = vv.X; | ||
1075 | verticesAsFloats[vi++] = vv.Y; | ||
1076 | verticesAsFloats[vi++] = vv.Z; | ||
1077 | } | ||
1078 | |||
1079 | // m_log.DebugFormat("{0}: CreateGeomMesh: calling CreateMesh. lid={1}, key={2}, indices={3}, vertices={4}", | ||
1080 | // LogHeader, _localID, _meshKey, indices.Length, vertices.Count); | ||
1081 | BulletSimAPI.CreateMesh(_scene.WorldID, _meshKey, indices.GetLength(0), indices, | ||
1082 | vertices.Count, verticesAsFloats); | ||
1083 | |||
1084 | _shapeType = ShapeData.PhysicsShapeType.SHAPE_MESH; | ||
1085 | // meshes are already scaled by the meshmerizer | ||
1086 | _scale = new OMV.Vector3(1f, 1f, 1f); | ||
1087 | // DetailLog("{0},BSPrim.CreateGeomMesh,done", LocalID); | ||
1088 | return; | ||
1089 | } | ||
1090 | |||
1091 | // No locking here because this is done when we know physics is not simulating | ||
1092 | private void CreateGeomHull() | ||
1093 | { | ||
1094 | float lod = _pbs.SculptEntry ? _scene.SculptLOD : _scene.MeshLOD; | ||
1095 | ulong newHullKey = (ulong)_pbs.GetMeshKey(_size, lod); | ||
1096 | // m_log.DebugFormat("{0}: CreateGeomHull: lID={1}, oldKey={2}, newKey={3}", LogHeader, _localID, _hullKey, newHullKey); | ||
1097 | |||
1098 | // if the hull hasn't changed, don't rebuild it | ||
1099 | if (newHullKey == _hullKey) return; | ||
1100 | |||
1101 | // DetailLog("{0},BSPrim.CreateGeomHull,create,oldKey={1},newKey={2}", LocalID, _hullKey, newHullKey); | ||
1102 | |||
1103 | // Since we're recreating new, get rid of any previously generated shape | ||
1104 | if (_hullKey != 0) | ||
1105 | { | ||
1106 | // m_log.DebugFormat("{0}: CreateGeom: deleting old hull. Key={1}", LogHeader, _hullKey); | ||
1107 | // DetailLog("{0},BSPrim.CreateGeomHull,deleteOldHull,key={1}", LocalID, _hullKey); | ||
1108 | BulletSimAPI.DestroyHull(_scene.WorldID, _hullKey); | ||
1109 | _hullKey = 0; | ||
1110 | } | ||
1111 | |||
1112 | _hullKey = newHullKey; | ||
1113 | |||
1114 | // Make sure the underlying mesh exists and is correct | ||
1115 | CreateGeomMesh(); | ||
1116 | |||
1117 | int[] indices = _mesh.getIndexListAsInt(); | ||
1118 | List<OMV.Vector3> vertices = _mesh.getVertexList(); | ||
1119 | |||
1120 | //format conversion from IMesh format to DecompDesc format | ||
1121 | List<int> convIndices = new List<int>(); | ||
1122 | List<float3> convVertices = new List<float3>(); | ||
1123 | for (int ii = 0; ii < indices.GetLength(0); ii++) | ||
1124 | { | ||
1125 | convIndices.Add(indices[ii]); | ||
1126 | } | ||
1127 | foreach (OMV.Vector3 vv in vertices) | ||
1128 | { | ||
1129 | convVertices.Add(new float3(vv.X, vv.Y, vv.Z)); | ||
1130 | } | ||
1131 | |||
1132 | // setup and do convex hull conversion | ||
1133 | _hulls = new List<ConvexResult>(); | ||
1134 | DecompDesc dcomp = new DecompDesc(); | ||
1135 | dcomp.mIndices = convIndices; | ||
1136 | dcomp.mVertices = convVertices; | ||
1137 | ConvexBuilder convexBuilder = new ConvexBuilder(HullReturn); | ||
1138 | // create the hull into the _hulls variable | ||
1139 | convexBuilder.process(dcomp); | ||
1140 | |||
1141 | // Convert the vertices and indices for passing to unmanaged. | ||
1142 | // The hull information is passed as a large floating point array. | ||
1143 | // The format is: | ||
1144 | // convHulls[0] = number of hulls | ||
1145 | // convHulls[1] = number of vertices in first hull | ||
1146 | // convHulls[2] = hull centroid X coordinate | ||
1147 | // convHulls[3] = hull centroid Y coordinate | ||
1148 | // convHulls[4] = hull centroid Z coordinate | ||
1149 | // convHulls[5] = first hull vertex X | ||
1150 | // convHulls[6] = first hull vertex Y | ||
1151 | // convHulls[7] = first hull vertex Z | ||
1152 | // convHulls[8] = second hull vertex X | ||
1153 | // ... | ||
1154 | // convHulls[n] = number of vertices in second hull | ||
1155 | // convHulls[n+1] = second hull centroid X coordinate | ||
1156 | // ... | ||
1157 | // | ||
1158 | // TODO: is is very inefficient. Someday change the convex hull generator to return | ||
1159 | // data structures that do not need to be converted in order to pass to Bullet. | ||
1160 | // And maybe put the values directly into pinned memory rather than marshaling. | ||
1161 | int hullCount = _hulls.Count; | ||
1162 | int totalVertices = 1; // include one for the count of the hulls | ||
1163 | foreach (ConvexResult cr in _hulls) | ||
1164 | { | ||
1165 | totalVertices += 4; // add four for the vertex count and centroid | ||
1166 | totalVertices += cr.HullIndices.Count * 3; // we pass just triangles | ||
1167 | } | ||
1168 | float[] convHulls = new float[totalVertices]; | ||
1169 | |||
1170 | convHulls[0] = (float)hullCount; | ||
1171 | int jj = 1; | ||
1172 | foreach (ConvexResult cr in _hulls) | ||
1173 | { | ||
1174 | // copy vertices for index access | ||
1175 | float3[] verts = new float3[cr.HullVertices.Count]; | ||
1176 | int kk = 0; | ||
1177 | foreach (float3 ff in cr.HullVertices) | ||
1178 | { | ||
1179 | verts[kk++] = ff; | ||
1180 | } | ||
1181 | |||
1182 | // add to the array one hull's worth of data | ||
1183 | convHulls[jj++] = cr.HullIndices.Count; | ||
1184 | convHulls[jj++] = 0f; // centroid x,y,z | ||
1185 | convHulls[jj++] = 0f; | ||
1186 | convHulls[jj++] = 0f; | ||
1187 | foreach (int ind in cr.HullIndices) | ||
1188 | { | ||
1189 | convHulls[jj++] = verts[ind].x; | ||
1190 | convHulls[jj++] = verts[ind].y; | ||
1191 | convHulls[jj++] = verts[ind].z; | ||
1192 | } | ||
1193 | } | ||
1194 | |||
1195 | // create the hull definition in Bullet | ||
1196 | // m_log.DebugFormat("{0}: CreateGeom: calling CreateHull. lid={1}, key={2}, hulls={3}", LogHeader, _localID, _hullKey, hullCount); | ||
1197 | BulletSimAPI.CreateHull(_scene.WorldID, _hullKey, hullCount, convHulls); | ||
1198 | _shapeType = ShapeData.PhysicsShapeType.SHAPE_HULL; | ||
1199 | // meshes are already scaled by the meshmerizer | ||
1200 | _scale = new OMV.Vector3(1f, 1f, 1f); | ||
1201 | // DetailLog("{0},BSPrim.CreateGeomHull,done", LocalID); | ||
1202 | return; | ||
1203 | } | ||
1204 | |||
1205 | // Callback from convex hull creater with a newly created hull. | ||
1206 | // Just add it to the collection of hulls for this shape. | ||
1207 | private void HullReturn(ConvexResult result) | ||
1208 | { | ||
1209 | _hulls.Add(result); | ||
1210 | return; | ||
1211 | } | ||
1212 | |||
1213 | // Create an object in Bullet if it has not already been created | ||
1214 | // No locking here because this is done when the physics engine is not simulating | ||
1215 | // Returns 'true' if an object was actually created. | ||
1216 | private bool CreateObject() | ||
1217 | { | ||
1218 | // this routine is called when objects are rebuilt. | ||
1219 | |||
1220 | // the mesh or hull must have already been created in Bullet | ||
1221 | ShapeData shape; | ||
1222 | FillShapeInfo(out shape); | ||
1223 | // m_log.DebugFormat("{0}: CreateObject: lID={1}, shape={2}", LogHeader, _localID, shape.Type); | ||
1224 | bool ret = BulletSimAPI.CreateObject(_scene.WorldID, shape); | ||
1225 | |||
1226 | // the CreateObject() may have recreated the rigid body. Make sure we have the latest. | ||
1227 | Body = new BulletBody(LocalID, BulletSimAPI.GetBodyHandle2(_scene.World.Ptr, LocalID)); | ||
1228 | |||
1229 | return ret; | ||
1230 | } | ||
1231 | |||
1232 | // Copy prim's info into the BulletSim shape description structure | 1202 | // Copy prim's info into the BulletSim shape description structure |
1233 | public void FillShapeInfo(out ShapeData shape) | 1203 | public void FillShapeInfo(out ShapeData shape) |
1234 | { | 1204 | { |
1235 | shape.ID = _localID; | 1205 | shape.ID = LocalID; |
1236 | shape.Type = _shapeType; | 1206 | shape.Type = ShapeData.PhysicsShapeType.SHAPE_UNKNOWN; |
1237 | shape.Position = _position; | 1207 | shape.Position = _position; |
1238 | shape.Rotation = _orientation; | 1208 | shape.Rotation = _orientation; |
1239 | shape.Velocity = _velocity; | 1209 | shape.Velocity = _velocity; |
1240 | shape.Scale = _scale; | 1210 | shape.Scale = _scale; |
1241 | shape.Mass = _isPhysical ? _mass : 0f; | 1211 | shape.Mass = _isPhysical ? _mass : 0f; |
1242 | shape.Buoyancy = _buoyancy; | 1212 | shape.Buoyancy = _buoyancy; |
1243 | shape.HullKey = _hullKey; | 1213 | shape.HullKey = 0; |
1244 | shape.MeshKey = _meshKey; | 1214 | shape.MeshKey = 0; |
1245 | shape.Friction = _friction; | 1215 | shape.Friction = _friction; |
1246 | shape.Restitution = _restitution; | 1216 | shape.Restitution = _restitution; |
1247 | shape.Collidable = (!IsPhantom) ? ShapeData.numericTrue : ShapeData.numericFalse; | 1217 | shape.Collidable = (!IsPhantom) ? ShapeData.numericTrue : ShapeData.numericFalse; |
1248 | shape.Static = _isPhysical ? ShapeData.numericFalse : ShapeData.numericTrue; | 1218 | shape.Static = _isPhysical ? ShapeData.numericFalse : ShapeData.numericTrue; |
1219 | shape.Solid = IsSolid ? ShapeData.numericFalse : ShapeData.numericTrue; | ||
1220 | shape.Size = _size; | ||
1249 | } | 1221 | } |
1250 | |||
1251 | |||
1252 | // Rebuild the geometry and object. | 1222 | // Rebuild the geometry and object. |
1253 | // This is called when the shape changes so we need to recreate the mesh/hull. | 1223 | // This is called when the shape changes so we need to recreate the mesh/hull. |
1254 | // No locking here because this is done when the physics engine is not simulating | 1224 | // Called at taint-time!!! |
1255 | private void RecreateGeomAndObject() | 1225 | private void CreateGeomAndObject(bool forceRebuild) |
1256 | { | 1226 | { |
1257 | // m_log.DebugFormat("{0}: RecreateGeomAndObject. lID={1}", LogHeader, _localID); | 1227 | ShapeData shapeData; |
1258 | if (CreateGeom(true)) | 1228 | FillShapeInfo(out shapeData); |
1259 | CreateObject(); | 1229 | |
1230 | // If this prim is part of a linkset, we must remove and restore the physical | ||
1231 | // links of the body is rebuilt. | ||
1232 | bool needToRestoreLinkset = false; | ||
1233 | |||
1234 | // Create the correct physical representation for this type of object. | ||
1235 | // Updates BSBody and BSShape with the new information. | ||
1236 | // Ignore 'forceRebuild'. This routine makes the right choices and changes of necessary. | ||
1237 | PhysicsScene.Shapes.GetBodyAndShape(false, PhysicsScene.World, this, shapeData, _pbs, | ||
1238 | null, delegate(BulletBody dBody) | ||
1239 | { | ||
1240 | // Called if the current prim body is about to be destroyed. | ||
1241 | // Remove all the physical dependencies on the old body. | ||
1242 | // (Maybe someday make the changing of BSShape an event handled by BSLinkset.) | ||
1243 | needToRestoreLinkset = Linkset.RemoveBodyDependencies(this); | ||
1244 | }); | ||
1245 | |||
1246 | if (needToRestoreLinkset) | ||
1247 | { | ||
1248 | // If physical body dependencies were removed, restore them | ||
1249 | Linkset.RestoreBodyDependencies(this); | ||
1250 | } | ||
1251 | |||
1252 | // Make sure the properties are set on the new object | ||
1253 | UpdatePhysicalParameters(); | ||
1260 | return; | 1254 | return; |
1261 | } | 1255 | } |
1262 | 1256 | ||
@@ -1277,7 +1271,7 @@ public sealed class BSPrim : PhysicsActor | |||
1277 | const float ACCELERATION_TOLERANCE = 0.01f; | 1271 | const float ACCELERATION_TOLERANCE = 0.01f; |
1278 | const float ROTATIONAL_VELOCITY_TOLERANCE = 0.01f; | 1272 | const float ROTATIONAL_VELOCITY_TOLERANCE = 0.01f; |
1279 | 1273 | ||
1280 | public void UpdateProperties(EntityProperties entprop) | 1274 | public override void UpdateProperties(EntityProperties entprop) |
1281 | { | 1275 | { |
1282 | /* | 1276 | /* |
1283 | UpdatedProperties changed = 0; | 1277 | UpdatedProperties changed = 0; |
@@ -1325,7 +1319,7 @@ public sealed class BSPrim : PhysicsActor | |||
1325 | // Don't check for damping here -- it's done in BulletSim and SceneObjectPart. | 1319 | // Don't check for damping here -- it's done in BulletSim and SceneObjectPart. |
1326 | 1320 | ||
1327 | // Updates only for individual prims and for the root object of a linkset. | 1321 | // Updates only for individual prims and for the root object of a linkset. |
1328 | if (_linkset.IsRoot(this)) | 1322 | if (Linkset.IsRoot(this)) |
1329 | { | 1323 | { |
1330 | // Assign to the local variables so the normal set action does not happen | 1324 | // Assign to the local variables so the normal set action does not happen |
1331 | _position = entprop.Position; | 1325 | _position = entprop.Position; |
@@ -1334,69 +1328,26 @@ public sealed class BSPrim : PhysicsActor | |||
1334 | _acceleration = entprop.Acceleration; | 1328 | _acceleration = entprop.Acceleration; |
1335 | _rotationalVelocity = entprop.RotationalVelocity; | 1329 | _rotationalVelocity = entprop.RotationalVelocity; |
1336 | 1330 | ||
1337 | // m_log.DebugFormat("{0}: RequestTerseUpdate. id={1}, ch={2}, pos={3}, rot={4}, vel={5}, acc={6}, rvel={7}", | 1331 | PositionSanityCheck2(true); |
1338 | // LogHeader, LocalID, changed, _position, _orientation, _velocity, _acceleration, _rotationalVelocity); | 1332 | |
1339 | // DetailLog("{0},BSPrim.UpdateProperties,call,pos={1},orient={2},vel={3},accel={4},rotVel={5}", | 1333 | Linkset.UpdateProperties(this); |
1340 | // LocalID, _position, _orientation, _velocity, _acceleration, _rotationalVelocity); | 1334 | |
1335 | DetailLog("{0},BSPrim.UpdateProperties,call,pos={1},orient={2},vel={3},accel={4},rotVel={5}", | ||
1336 | LocalID, _position, _orientation, _velocity, _acceleration, _rotationalVelocity); | ||
1337 | |||
1338 | // BulletSimAPI.DumpRigidBody2(Scene.World.Ptr, BSBody.Ptr); | ||
1341 | 1339 | ||
1342 | base.RequestPhysicsterseUpdate(); | 1340 | base.RequestPhysicsterseUpdate(); |
1343 | } | 1341 | } |
1344 | /* | 1342 | /* |
1345 | else | 1343 | else |
1346 | { | 1344 | { |
1347 | // For debugging, we also report the movement of children | 1345 | // For debugging, report the movement of children |
1348 | DetailLog("{0},BSPrim.UpdateProperties,child,pos={1},orient={2},vel={3},accel={4},rotVel={5}", | 1346 | DetailLog("{0},BSPrim.UpdateProperties,child,pos={1},orient={2},vel={3},accel={4},rotVel={5}", |
1349 | LocalID, entprop.Position, entprop.Rotation, entprop.Velocity, | 1347 | LocalID, entprop.Position, entprop.Rotation, entprop.Velocity, |
1350 | entprop.Acceleration, entprop.RotationalVelocity); | 1348 | entprop.Acceleration, entprop.RotationalVelocity); |
1351 | } | 1349 | } |
1352 | */ | 1350 | */ |
1353 | } | 1351 | } |
1354 | |||
1355 | // I've collided with something | ||
1356 | CollisionEventUpdate collisionCollection; | ||
1357 | public void Collide(uint collidingWith, ActorTypes type, OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth) | ||
1358 | { | ||
1359 | // m_log.DebugFormat("{0}: Collide: ms={1}, id={2}, with={3}", LogHeader, _subscribedEventsMs, LocalID, collidingWith); | ||
1360 | |||
1361 | // The following lines make IsColliding() and IsCollidingGround() work | ||
1362 | _collidingStep = _scene.SimulationStep; | ||
1363 | if (collidingWith == BSScene.TERRAIN_ID || collidingWith == BSScene.GROUNDPLANE_ID) | ||
1364 | { | ||
1365 | _collidingGroundStep = _scene.SimulationStep; | ||
1366 | } | ||
1367 | |||
1368 | // DetailLog("{0},BSPrim.Collison,call,with={1}", LocalID, collidingWith); | ||
1369 | |||
1370 | // if someone is subscribed to collision events.... | ||
1371 | if (_subscribedEventsMs != 0) { | ||
1372 | // throttle the collisions to the number of milliseconds specified in the subscription | ||
1373 | int nowTime = _scene.SimulationNowTime; | ||
1374 | if (nowTime >= _nextCollisionOkTime) { | ||
1375 | _nextCollisionOkTime = nowTime + _subscribedEventsMs; | ||
1376 | |||
1377 | if (collisionCollection == null) | ||
1378 | collisionCollection = new CollisionEventUpdate(); | ||
1379 | collisionCollection.AddCollider(collidingWith, new ContactPoint(contactPoint, contactNormal, pentrationDepth)); | ||
1380 | } | ||
1381 | } | ||
1382 | } | ||
1383 | |||
1384 | // The scene is telling us it's time to pass our collected collisions into the simulator | ||
1385 | public void SendCollisions() | ||
1386 | { | ||
1387 | if (collisionCollection != null && collisionCollection.Count > 0) | ||
1388 | { | ||
1389 | base.SendCollisionUpdate(collisionCollection); | ||
1390 | // The collisionCollection structure is passed around in the simulator. | ||
1391 | // Make sure we don't have a handle to that one and that a new one is used next time. | ||
1392 | collisionCollection = null; | ||
1393 | } | ||
1394 | } | ||
1395 | |||
1396 | // Invoke the detailed logger and output something if it's enabled. | ||
1397 | private void DetailLog(string msg, params Object[] args) | ||
1398 | { | ||
1399 | Scene.PhysicsLogging.Write(msg, args); | ||
1400 | } | ||
1401 | } | 1352 | } |
1402 | } | 1353 | } |