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