diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs b/OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs new file mode 100755 index 0000000..991e5b1 --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs | |||
@@ -0,0 +1,263 @@ | |||
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 | using System; | ||
28 | using System.Collections.Generic; | ||
29 | using System.Text; | ||
30 | |||
31 | using OMV = OpenMetaverse; | ||
32 | using OpenSim.Framework; | ||
33 | using OpenSim.Region.Physics.Manager; | ||
34 | |||
35 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
36 | { | ||
37 | /* | ||
38 | * Class to wrap all objects. | ||
39 | * The rest of BulletSim doesn't need to keep checking for avatars or prims | ||
40 | * unless the difference is significant. | ||
41 | * | ||
42 | * Variables in the physicsl objects are in three forms: | ||
43 | * VariableName: used by the simulator and performs taint operations, etc | ||
44 | * RawVariableName: direct reference to the BulletSim storage for the variable value | ||
45 | * ForceVariableName: direct reference (store and fetch) to the value in the physics engine. | ||
46 | * The last two (and certainly the last one) should be referenced only in taint-time. | ||
47 | */ | ||
48 | public abstract class BSPhysObject : PhysicsActor | ||
49 | { | ||
50 | protected BSPhysObject() | ||
51 | { | ||
52 | } | ||
53 | protected BSPhysObject(BSScene parentScene, uint localID, string name, string typeName) | ||
54 | { | ||
55 | PhysicsScene = parentScene; | ||
56 | LocalID = localID; | ||
57 | PhysObjectName = name; | ||
58 | TypeName = typeName; | ||
59 | |||
60 | Linkset = BSLinkset.Factory(PhysicsScene, this); | ||
61 | LastAssetBuildFailed = false; | ||
62 | |||
63 | CollisionCollection = new CollisionEventUpdate(); | ||
64 | SubscribedEventsMs = 0; | ||
65 | CollidingStep = 0; | ||
66 | CollidingGroundStep = 0; | ||
67 | } | ||
68 | |||
69 | public BSScene PhysicsScene { get; protected set; } | ||
70 | // public override uint LocalID { get; set; } // Use the LocalID definition in PhysicsActor | ||
71 | public string PhysObjectName { get; protected set; } | ||
72 | public string TypeName { get; protected set; } | ||
73 | |||
74 | public BSLinkset Linkset { get; set; } | ||
75 | |||
76 | // Return the object mass without calculating it or having side effects | ||
77 | public abstract float RawMass { get; } | ||
78 | // Set the raw mass but also update physical mass properties (inertia, ...) | ||
79 | public abstract void UpdatePhysicalMassProperties(float mass); | ||
80 | |||
81 | // The last value calculated for the prim's inertia | ||
82 | public OMV.Vector3 Inertia { get; set; } | ||
83 | |||
84 | // Reference to the physical body (btCollisionObject) of this object | ||
85 | public BulletBody PhysBody; | ||
86 | // Reference to the physical shape (btCollisionShape) of this object | ||
87 | public BulletShape PhysShape; | ||
88 | |||
89 | // 'true' if the mesh's underlying asset failed to build. | ||
90 | // This will keep us from looping after the first time the build failed. | ||
91 | public bool LastAssetBuildFailed { get; set; } | ||
92 | |||
93 | // The objects base shape information. Null if not a prim type shape. | ||
94 | public PrimitiveBaseShape BaseShape { get; protected set; } | ||
95 | // Some types of objects have preferred physical representations. | ||
96 | // Returns SHAPE_UNKNOWN if there is no preference. | ||
97 | public virtual ShapeData.PhysicsShapeType PreferredPhysicalShape | ||
98 | { | ||
99 | get { return ShapeData.PhysicsShapeType.SHAPE_UNKNOWN; } | ||
100 | } | ||
101 | |||
102 | // When the physical properties are updated, an EntityProperty holds the update values. | ||
103 | // Keep the current and last EntityProperties to enable computation of differences | ||
104 | // between the current update and the previous values. | ||
105 | public EntityProperties CurrentEntityProperties { get; set; } | ||
106 | public EntityProperties LastEntityProperties { get; set; } | ||
107 | |||
108 | public abstract OMV.Vector3 Scale { get; set; } | ||
109 | public abstract bool IsSolid { get; } | ||
110 | public abstract bool IsStatic { get; } | ||
111 | |||
112 | // Stop all physical motion. | ||
113 | public abstract void ZeroMotion(bool inTaintTime); | ||
114 | public abstract void ZeroAngularMotion(bool inTaintTime); | ||
115 | |||
116 | // Step the vehicle simulation for this object. A NOOP if the vehicle was not configured. | ||
117 | public virtual void StepVehicle(float timeStep) { } | ||
118 | |||
119 | // Update the physical location and motion of the object. Called with data from Bullet. | ||
120 | public abstract void UpdateProperties(EntityProperties entprop); | ||
121 | |||
122 | // Tell the object to clean up. | ||
123 | public abstract void Destroy(); | ||
124 | |||
125 | public abstract OMV.Vector3 RawPosition { get; set; } | ||
126 | public abstract OMV.Vector3 ForcePosition { get; set; } | ||
127 | |||
128 | public abstract OMV.Quaternion RawOrientation { get; set; } | ||
129 | public abstract OMV.Quaternion ForceOrientation { get; set; } | ||
130 | |||
131 | public abstract OMV.Vector3 ForceVelocity { get; set; } | ||
132 | |||
133 | public abstract OMV.Vector3 ForceRotationalVelocity { get; set; } | ||
134 | |||
135 | public abstract float ForceBuoyancy { get; set; } | ||
136 | |||
137 | public virtual bool ForceBodyShapeRebuild(bool inTaintTime) { return false; } | ||
138 | |||
139 | #region Collisions | ||
140 | |||
141 | // Requested number of milliseconds between collision events. Zero means disabled. | ||
142 | protected int SubscribedEventsMs { get; set; } | ||
143 | // Given subscription, the time that a collision may be passed up | ||
144 | protected int NextCollisionOkTime { get; set; } | ||
145 | // The simulation step that last had a collision | ||
146 | protected long CollidingStep { get; set; } | ||
147 | // The simulation step that last had a collision with the ground | ||
148 | protected long CollidingGroundStep { get; set; } | ||
149 | // The collision flags we think are set in Bullet | ||
150 | protected CollisionFlags CurrentCollisionFlags { get; set; } | ||
151 | |||
152 | // The collisions that have been collected this tick | ||
153 | protected CollisionEventUpdate CollisionCollection; | ||
154 | |||
155 | // The simulation step is telling this object about a collision. | ||
156 | // Return 'true' if a collision was processed and should be sent up. | ||
157 | // Called at taint time from within the Step() function | ||
158 | public virtual bool Collide(uint collidingWith, BSPhysObject collidee, | ||
159 | OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth) | ||
160 | { | ||
161 | bool ret = false; | ||
162 | |||
163 | // The following lines make IsColliding() and IsCollidingGround() work | ||
164 | CollidingStep = PhysicsScene.SimulationStep; | ||
165 | if (collidingWith <= PhysicsScene.TerrainManager.HighestTerrainID) | ||
166 | { | ||
167 | CollidingGroundStep = PhysicsScene.SimulationStep; | ||
168 | } | ||
169 | |||
170 | // prims in the same linkset cannot collide with each other | ||
171 | if (collidee != null && (this.Linkset.LinksetID == collidee.Linkset.LinksetID)) | ||
172 | { | ||
173 | return ret; | ||
174 | } | ||
175 | |||
176 | // if someone has subscribed for collision events.... | ||
177 | if (SubscribedEvents()) { | ||
178 | CollisionCollection.AddCollider(collidingWith, new ContactPoint(contactPoint, contactNormal, pentrationDepth)); | ||
179 | DetailLog("{0},{1}.Collison.AddCollider,call,with={2},point={3},normal={4},depth={5}", | ||
180 | LocalID, TypeName, collidingWith, contactPoint, contactNormal, pentrationDepth); | ||
181 | |||
182 | ret = true; | ||
183 | } | ||
184 | return ret; | ||
185 | } | ||
186 | |||
187 | // Send the collected collisions into the simulator. | ||
188 | // Called at taint time from within the Step() function thus no locking problems | ||
189 | // with CollisionCollection and ObjectsWithNoMoreCollisions. | ||
190 | // Return 'true' if there were some actual collisions passed up | ||
191 | public virtual bool SendCollisions() | ||
192 | { | ||
193 | bool ret = true; | ||
194 | // If the 'no collision' call, force it to happen right now so quick collision_end | ||
195 | bool force = CollisionCollection.Count == 0; | ||
196 | |||
197 | // throttle the collisions to the number of milliseconds specified in the subscription | ||
198 | if (force || (PhysicsScene.SimulationNowTime >= NextCollisionOkTime)) | ||
199 | { | ||
200 | NextCollisionOkTime = PhysicsScene.SimulationNowTime + SubscribedEventsMs; | ||
201 | |||
202 | // We are called if we previously had collisions. If there are no collisions | ||
203 | // this time, send up one last empty event so OpenSim can sense collision end. | ||
204 | if (CollisionCollection.Count == 0) | ||
205 | { | ||
206 | // If I have no collisions this time, remove me from the list of objects with collisions. | ||
207 | ret = false; | ||
208 | } | ||
209 | |||
210 | // DetailLog("{0},{1}.SendCollisionUpdate,call,numCollisions={2}", LocalID, TypeName, CollisionCollection.Count); | ||
211 | base.SendCollisionUpdate(CollisionCollection); | ||
212 | |||
213 | // The collisionCollection structure is passed around in the simulator. | ||
214 | // Make sure we don't have a handle to that one and that a new one is used for next time. | ||
215 | CollisionCollection = new CollisionEventUpdate(); | ||
216 | } | ||
217 | return ret; | ||
218 | } | ||
219 | |||
220 | // Subscribe for collision events. | ||
221 | // Parameter is the millisecond rate the caller wishes collision events to occur. | ||
222 | public override void SubscribeEvents(int ms) { | ||
223 | // DetailLog("{0},{1}.SubscribeEvents,subscribing,ms={2}", LocalID, TypeName, ms); | ||
224 | SubscribedEventsMs = ms; | ||
225 | if (ms > 0) | ||
226 | { | ||
227 | // make sure first collision happens | ||
228 | NextCollisionOkTime = Util.EnvironmentTickCountSubtract(SubscribedEventsMs); | ||
229 | |||
230 | PhysicsScene.TaintedObject(TypeName+".SubscribeEvents", delegate() | ||
231 | { | ||
232 | CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); | ||
233 | }); | ||
234 | } | ||
235 | else | ||
236 | { | ||
237 | // Subscribing for zero or less is the same as unsubscribing | ||
238 | UnSubscribeEvents(); | ||
239 | } | ||
240 | } | ||
241 | public override void UnSubscribeEvents() { | ||
242 | // DetailLog("{0},{1}.UnSubscribeEvents,unsubscribing", LocalID, TypeName); | ||
243 | SubscribedEventsMs = 0; | ||
244 | PhysicsScene.TaintedObject(TypeName+".UnSubscribeEvents", delegate() | ||
245 | { | ||
246 | CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); | ||
247 | }); | ||
248 | } | ||
249 | // Return 'true' if the simulator wants collision events | ||
250 | public override bool SubscribedEvents() { | ||
251 | return (SubscribedEventsMs > 0); | ||
252 | } | ||
253 | |||
254 | #endregion // Collisions | ||
255 | |||
256 | // High performance detailed logging routine used by the physical objects. | ||
257 | protected void DetailLog(string msg, params Object[] args) | ||
258 | { | ||
259 | if (PhysicsScene.PhysicsLogging.Enabled) | ||
260 | PhysicsScene.DetailLog(msg, args); | ||
261 | } | ||
262 | } | ||
263 | } | ||