clockwerk-opensim-stable – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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.Linq;
30 using System.Text;
31  
32 using OpenSim.Framework;
33 using OpenSim.Region.OptionalModules.Scripting;
34  
35 using OMV = OpenMetaverse;
36  
37 namespace OpenSim.Region.Physics.BulletSPlugin
38 {
39 public class BSPrimLinkable : BSPrimDisplaced
40 {
41 // The purpose of this subclass is to add linkset functionality to the prim. This overrides
42 // operations necessary for keeping the linkset created and, additionally, this
43 // calls the linkset implementation for its creation and management.
44  
45 private static readonly string LogHeader = "[BULLETS PRIMLINKABLE]";
46  
47 // This adds the overrides for link() and delink() so the prim is linkable.
48  
49 public BSLinkset Linkset { get; set; }
50 // The index of this child prim.
51 public int LinksetChildIndex { get; set; }
52  
53 public BSLinkset.LinksetImplementation LinksetType { get; set; }
54  
55 public BSPrimLinkable(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size,
56 OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical)
57 : base(localID, primName, parent_scene, pos, size, rotation, pbs, pisPhysical)
58 {
59 // Default linkset implementation for this prim
60 LinksetType = (BSLinkset.LinksetImplementation)BSParam.LinksetImplementation;
61  
62 Linkset = BSLinkset.Factory(PhysScene, this);
63  
64 Linkset.Refresh(this);
65 }
66  
67 public override void Destroy()
68 {
69 Linkset = Linkset.RemoveMeFromLinkset(this, false /* inTaintTime */);
70 base.Destroy();
71 }
72  
73 public override void link(Manager.PhysicsActor obj)
74 {
75 BSPrimLinkable parent = obj as BSPrimLinkable;
76 if (parent != null)
77 {
78 BSPhysObject parentBefore = Linkset.LinksetRoot; // DEBUG
79 int childrenBefore = Linkset.NumberOfChildren; // DEBUG
80  
81 Linkset = parent.Linkset.AddMeToLinkset(this);
82  
83 DetailLog("{0},BSPrimLinkable.link,call,parentBefore={1}, childrenBefore=={2}, parentAfter={3}, childrenAfter={4}",
84 LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
85 }
86 return;
87 }
88  
89 public override void delink()
90 {
91 // TODO: decide if this parent checking needs to happen at taint time
92 // Race condition here: if link() and delink() in same simulation tick, the delink will not happen
93  
94 BSPhysObject parentBefore = Linkset.LinksetRoot; // DEBUG
95 int childrenBefore = Linkset.NumberOfChildren; // DEBUG
96  
97 Linkset = Linkset.RemoveMeFromLinkset(this, false /* inTaintTime*/);
98  
99 DetailLog("{0},BSPrimLinkable.delink,parentBefore={1},childrenBefore={2},parentAfter={3},childrenAfter={4}, ",
100 LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
101 return;
102 }
103  
104 // When simulator changes position, this might be moving a child of the linkset.
105 public override OMV.Vector3 Position
106 {
107 get { return base.Position; }
108 set
109 {
110 base.Position = value;
111 PhysScene.TaintedObject(LocalID, "BSPrimLinkable.setPosition", delegate()
112 {
113 Linkset.UpdateProperties(UpdatedProperties.Position, this);
114 });
115 }
116 }
117  
118 // When simulator changes orientation, this might be moving a child of the linkset.
119 public override OMV.Quaternion Orientation
120 {
121 get { return base.Orientation; }
122 set
123 {
124 base.Orientation = value;
125 PhysScene.TaintedObject(LocalID, "BSPrimLinkable.setOrientation", delegate()
126 {
127 Linkset.UpdateProperties(UpdatedProperties.Orientation, this);
128 });
129 }
130 }
131  
132 public override float TotalMass
133 {
134 get { return Linkset.LinksetMass; }
135 }
136  
137 public override OMV.Vector3 CenterOfMass
138 {
139 get { return Linkset.CenterOfMass; }
140 }
141  
142 public override OMV.Vector3 GeometricCenter
143 {
144 get { return Linkset.GeometricCenter; }
145 }
146  
147 // Refresh the linkset structure and parameters when the prim's physical parameters are changed.
148 public override void UpdatePhysicalParameters()
149 {
150 base.UpdatePhysicalParameters();
151 // Recompute any linkset parameters.
152 // When going from non-physical to physical, this re-enables the constraints that
153 // had been automatically disabled when the mass was set to zero.
154 // For compound based linksets, this enables and disables interactions of the children.
155 if (Linkset != null) // null can happen during initialization
156 Linkset.Refresh(this);
157 }
158  
159 // When the prim is made dynamic or static, the linkset needs to change.
160 protected override void MakeDynamic(bool makeStatic)
161 {
162 base.MakeDynamic(makeStatic);
163 if (Linkset != null) // null can happen during initialization
164 {
165 if (makeStatic)
166 Linkset.MakeStatic(this);
167 else
168 Linkset.MakeDynamic(this);
169 }
170 }
171  
172 // Body is being taken apart. Remove physical dependencies and schedule a rebuild.
173 protected override void RemoveDependencies()
174 {
175 Linkset.RemoveDependencies(this);
176 base.RemoveDependencies();
177 }
178  
179 // Called after a simulation step for the changes in physical object properties.
180 // Do any filtering/modification needed for linksets.
181 public override void UpdateProperties(EntityProperties entprop)
182 {
183 if (Linkset.IsRoot(this) || Linkset.ShouldReportPropertyUpdates(this))
184 {
185 // Properties are only updated for the roots of a linkset.
186 // TODO: this will have to change when linksets are articulated.
187 base.UpdateProperties(entprop);
188 }
189 /*
190 else
191 {
192 // For debugging, report the movement of children
193 DetailLog("{0},BSPrim.UpdateProperties,child,pos={1},orient={2},vel={3},accel={4},rotVel={5}",
194 LocalID, entprop.Position, entprop.Rotation, entprop.Velocity,
195 entprop.Acceleration, entprop.RotationalVelocity);
196 }
197 */
198 // The linkset might like to know about changing locations
199 Linkset.UpdateProperties(UpdatedProperties.EntPropUpdates, this);
200 }
201  
202 // Called after a simulation step to post a collision with this object.
203 // This returns 'true' if the collision has been queued and the SendCollisions call must
204 // be made at the end of the simulation step.
205 public override bool Collide(uint collidingWith, BSPhysObject collidee,
206 OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth)
207 {
208 bool ret = false;
209 // Ask the linkset if it wants to handle the collision
210 if (!Linkset.HandleCollide(collidingWith, collidee, contactPoint, contactNormal, pentrationDepth))
211 {
212 // The linkset didn't handle it so pass the collision through normal processing
213 ret = base.Collide(collidingWith, collidee, contactPoint, contactNormal, pentrationDepth);
214 }
215 return ret;
216 }
217  
218 // A linkset reports any collision on any part of the linkset.
219 public long SomeCollisionSimulationStep = 0;
220 public override bool HasSomeCollision
221 {
222 get
223 {
224 return (SomeCollisionSimulationStep == PhysScene.SimulationStep) || base.IsColliding;
225 }
226 set
227 {
228 if (value)
229 SomeCollisionSimulationStep = PhysScene.SimulationStep;
230 else
231 SomeCollisionSimulationStep = 0;
232  
233 base.HasSomeCollision = value;
234 }
235 }
236  
237 // Convert the existing linkset of this prim into a new type.
238 public bool ConvertLinkset(BSLinkset.LinksetImplementation newType)
239 {
240 bool ret = false;
241 if (LinksetType != newType)
242 {
243 DetailLog("{0},BSPrimLinkable.ConvertLinkset,oldT={1},newT={2}", LocalID, LinksetType, newType);
244  
245 // Set the implementation type first so the call to BSLinkset.Factory gets the new type.
246 this.LinksetType = newType;
247  
248 BSLinkset oldLinkset = this.Linkset;
249 BSLinkset newLinkset = BSLinkset.Factory(PhysScene, this);
250  
251 this.Linkset = newLinkset;
252  
253 // Pick up any physical dependencies this linkset might have in the physics engine.
254 oldLinkset.RemoveDependencies(this);
255  
256 // Create a list of the children (mainly because can't interate through a list that's changing)
257 List<BSPrimLinkable> children = new List<BSPrimLinkable>();
258 oldLinkset.ForEachMember((child) =>
259 {
260 if (!oldLinkset.IsRoot(child))
261 children.Add(child);
262 return false; // 'false' says to continue to next member
263 });
264  
265 // Remove the children from the old linkset and add to the new (will be a new instance from the factory)
266 foreach (BSPrimLinkable child in children)
267 {
268 oldLinkset.RemoveMeFromLinkset(child, true /*inTaintTime*/);
269 }
270 foreach (BSPrimLinkable child in children)
271 {
272 newLinkset.AddMeToLinkset(child);
273 child.Linkset = newLinkset;
274 }
275  
276 // Force the shape and linkset to get reconstructed
277 newLinkset.Refresh(this);
278 this.ForceBodyShapeRebuild(true /* inTaintTime */);
279 }
280 return ret;
281 }
282  
283 #region Extension
284 public override object Extension(string pFunct, params object[] pParams)
285 {
286 DetailLog("{0} BSPrimLinkable.Extension,op={1},nParam={2}", LocalID, pFunct, pParams.Length);
287 object ret = null;
288 switch (pFunct)
289 {
290 // physGetLinksetType();
291 // pParams = [ BSPhysObject root, null ]
292 case ExtendedPhysics.PhysFunctGetLinksetType:
293 {
294 ret = (object)LinksetType;
295 DetailLog("{0},BSPrimLinkable.Extension.physGetLinksetType,type={1}", LocalID, ret);
296 break;
297 }
298 // physSetLinksetType(type);
299 // pParams = [ BSPhysObject root, null, integer type ]
300 case ExtendedPhysics.PhysFunctSetLinksetType:
301 {
302 if (pParams.Length > 2)
303 {
304 BSLinkset.LinksetImplementation linksetType = (BSLinkset.LinksetImplementation)pParams[2];
305 if (Linkset.IsRoot(this))
306 {
307 PhysScene.TaintedObject(LocalID, "BSPrim.PhysFunctSetLinksetType", delegate()
308 {
309 // Cause the linkset type to change
310 DetailLog("{0},BSPrimLinkable.Extension.physSetLinksetType, oldType={1},newType={2}",
311 LocalID, Linkset.LinksetImpl, linksetType);
312 ConvertLinkset(linksetType);
313 });
314 }
315 ret = (object)(int)linksetType;
316 }
317 break;
318 }
319 // physChangeLinkType(linknum, typeCode);
320 // pParams = [ BSPhysObject root, BSPhysObject child, integer linkType ]
321 case ExtendedPhysics.PhysFunctChangeLinkType:
322 {
323 ret = Linkset.Extension(pFunct, pParams);
324 break;
325 }
326 // physGetLinkType(linknum);
327 // pParams = [ BSPhysObject root, BSPhysObject child ]
328 case ExtendedPhysics.PhysFunctGetLinkType:
329 {
330 ret = Linkset.Extension(pFunct, pParams);
331 break;
332 }
333 // physChangeLinkParams(linknum, [code, value, code, value, ...]);
334 // pParams = [ BSPhysObject root, BSPhysObject child, object[] [ string op, object opParam, string op, object opParam, ... ] ]
335 case ExtendedPhysics.PhysFunctChangeLinkParams:
336 {
337 ret = Linkset.Extension(pFunct, pParams);
338 break;
339 }
340 default:
341 ret = base.Extension(pFunct, pParams);
342 break;
343 }
344 return ret;
345 }
346 #endregion // Extension
347 }
348 }