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.cs213
1 files changed, 213 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..1ac8c59
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs
@@ -0,0 +1,213 @@
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// 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.
40public 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 = new BSLinkset(PhysicsScene, this);
50
51 CollisionCollection = new CollisionEventUpdate();
52 SubscribedEventsMs = 0;
53 CollidingStep = 0;
54 CollidingGroundStep = 0;
55 }
56
57 public BSScene PhysicsScene { get; protected set; }
58 // public override uint LocalID { get; set; } // Use the LocalID definition in PhysicsActor
59 public string PhysObjectName { get; protected set; }
60 public string TypeName { get; protected set; }
61
62 public BSLinkset Linkset { get; set; }
63
64 // Return the object mass without calculating it or having side effects
65 public abstract float MassRaw { get; }
66
67 // Reference to the physical body (btCollisionObject) of this object
68 public BulletBody BSBody;
69 // Reference to the physical shape (btCollisionShape) of this object
70 public BulletShape BSShape;
71
72 // Stop all physical motion.
73 public abstract void ZeroMotion();
74
75 // Step the vehicle simulation for this object. A NOOP if the vehicle was not configured.
76 public virtual void StepVehicle(float timeStep) { }
77
78 // Update the physical location and motion of the object. Called with data from Bullet.
79 public abstract void UpdateProperties(EntityProperties entprop);
80
81 // Tell the object to clean up.
82 public abstract void Destroy();
83
84 public abstract OMV.Vector3 ForcePosition { get; set; }
85
86 public abstract OMV.Quaternion ForceOrientation { get; set; }
87
88 #region Collisions
89
90 // Requested number of milliseconds between collision events. Zero means disabled.
91 protected int SubscribedEventsMs { get; set; }
92 // Given subscription, the time that a collision may be passed up
93 protected int NextCollisionOkTime { get; set; }
94 // The simulation step that last had a collision
95 protected long CollidingStep { get; set; }
96 // The simulation step that last had a collision with the ground
97 protected long CollidingGroundStep { get; set; }
98 // The collision flags we think are set in Bullet
99 protected CollisionFlags CurrentCollisionFlags { get; set; }
100
101 // The collisions that have been collected this tick
102 protected CollisionEventUpdate CollisionCollection;
103
104 // The simulation step is telling this object about a collision.
105 // Return 'true' if a collision was processed and should be sent up.
106 // Called at taint time from within the Step() function
107 public virtual bool Collide(uint collidingWith, BSPhysObject collidee,
108 OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth)
109 {
110 bool ret = false;
111
112 // The following lines make IsColliding() and IsCollidingGround() work
113 CollidingStep = PhysicsScene.SimulationStep;
114 if (collidingWith <= PhysicsScene.TerrainManager.HighestTerrainID)
115 {
116 CollidingGroundStep = PhysicsScene.SimulationStep;
117 }
118
119 // prims in the same linkset cannot collide with each other
120 if (collidee != null && (this.Linkset.LinksetID == collidee.Linkset.LinksetID))
121 {
122 return ret;
123 }
124
125 // if someone has subscribed for collision events....
126 if (SubscribedEvents()) {
127 CollisionCollection.AddCollider(collidingWith, new ContactPoint(contactPoint, contactNormal, pentrationDepth));
128 // DetailLog("{0},{1}.Collison.AddCollider,call,with={2},point={3},normal={4},depth={5}",
129 // LocalID, TypeName, collidingWith, contactPoint, contactNormal, pentrationDepth);
130
131 ret = true;
132 }
133 return ret;
134 }
135
136 // Routine to send the collected collisions into the simulator.
137 // Also handles removal of this from the collection of objects with collisions if
138 // there are no collisions from this object. Mechanism is create one last
139 // collision event to make collision_end work.
140 // Called at taint time from within the Step() function thus no locking problems
141 // with CollisionCollection and ObjectsWithNoMoreCollisions.
142 // Return 'true' if there were some actual collisions passed up
143 public virtual bool SendCollisions()
144 {
145 bool ret = true;
146
147 // throttle the collisions to the number of milliseconds specified in the subscription
148 int nowTime = PhysicsScene.SimulationNowTime;
149 if (nowTime >= NextCollisionOkTime)
150 {
151 NextCollisionOkTime = nowTime + SubscribedEventsMs;
152
153 // We are called if we previously had collisions. If there are no collisions
154 // this time, send up one last empty event so OpenSim can sense collision end.
155 if (CollisionCollection.Count == 0)
156 {
157 // If I have no collisions this time, remove me from the list of objects with collisions.
158 ret = false;
159 }
160
161 // DetailLog("{0},{1}.SendCollisionUpdate,call,numCollisions={2}", LocalID, TypeName, CollisionCollection.Count);
162 base.SendCollisionUpdate(CollisionCollection);
163
164 // The collisionCollection structure is passed around in the simulator.
165 // Make sure we don't have a handle to that one and that a new one is used for next time.
166 CollisionCollection = new CollisionEventUpdate();
167 }
168 return ret;
169 }
170
171 // Subscribe for collision events.
172 // Parameter is the millisecond rate the caller wishes collision events to occur.
173 public override void SubscribeEvents(int ms) {
174 // DetailLog("{0},{1}.SubscribeEvents,subscribing,ms={2}", LocalID, TypeName, ms);
175 SubscribedEventsMs = ms;
176 if (ms > 0)
177 {
178 // make sure first collision happens
179 NextCollisionOkTime = Util.EnvironmentTickCountSubtract(SubscribedEventsMs);
180
181 PhysicsScene.TaintedObject(TypeName+".SubscribeEvents", delegate()
182 {
183 CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(BSBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
184 });
185 }
186 else
187 {
188 // Subscribing for zero or less is the same as unsubscribing
189 UnSubscribeEvents();
190 }
191 }
192 public override void UnSubscribeEvents() {
193 // DetailLog("{0},{1}.UnSubscribeEvents,unsubscribing", LocalID, TypeName);
194 SubscribedEventsMs = 0;
195 PhysicsScene.TaintedObject(TypeName+".UnSubscribeEvents", delegate()
196 {
197 CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(BSBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
198 });
199 }
200 // Return 'true' if the simulator wants collision events
201 public override bool SubscribedEvents() {
202 return (SubscribedEventsMs > 0);
203 }
204
205 #endregion // Collisions
206
207 // High performance detailed logging routine used by the physical objects.
208 protected void DetailLog(string msg, params Object[] args)
209 {
210 PhysicsScene.PhysicsLogging.Write(msg, args);
211 }
212}
213}