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