aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs
blob: 6f8430c8cc22a5801ed4ddc82e14cae560a2a0ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyrightD
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSimulator Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.Collections.Generic;
using System.Text;

using OMV = OpenMetaverse;

namespace OpenSim.Region.Physics.BulletSPlugin
{
public class BSLinkset
{
    private static string LogHeader = "[BULLETSIM LINKSET]";

    private BSPrim m_linksetRoot;
    public BSPrim Root { get { return m_linksetRoot; } }

    private BSScene m_scene;

    private List<BSPrim> m_children;

    // We lock the diddling of linkset classes to prevent any badness.
    // This locks the modification of the instances of this class. Changes
    //    to the physical representation is done via the tainting mechenism.
    private object m_linksetActivityLock = new Object();

    // We keep the prim's mass in the linkset structure since it could be dependent on other prims
    private float m_mass;
    public float LinksetMass 
    { 
        get 
        {
            m_mass = ComputeLinksetMass();
            return m_mass;
        }
    }

    public OMV.Vector3 CenterOfMass
    {
        get { return ComputeLinksetCenterOfMass(); }
    }

    public OMV.Vector3 GeometricCenter
    {
        get { return ComputeLinksetGeometricCenter(); }
    }

    public BSLinkset(BSScene scene, BSPrim parent)
    {
        // A simple linkset of one (no children)
        m_scene = scene;
        m_linksetRoot = parent;
        m_children = new List<BSPrim>();
        m_mass = parent.MassRaw;
    }

    // Link to a linkset where the child knows the parent.
    // Parent changing should not happen so do some sanity checking.
    // We return the parent's linkset so the child can track it's membership.
    public BSLinkset AddMeToLinkset(BSPrim child, BSPrim parent)
    {
        lock (m_linksetActivityLock)
        {
            parent.Linkset.AddChildToLinkset(child);
        }
        return parent.Linkset;
    }

    public BSLinkset RemoveMeFromLinkset(BSPrim child)
    {
        lock (m_linksetActivityLock)
        {
            if (IsRoot(child))
            {
                // if root of linkset, take the linkset apart
                while (m_children.Count > 0)
                {
                    // Note that we don't do a foreach because the remove routine
                    //    takes it out of the list.
                    RemoveChildFromLinkset(m_children[0]);
                }
                m_children.Clear(); // just to make sure
            }
            else
            {
                // Just removing a child from an existing linkset
                RemoveChildFromLinkset(child);
            }
        }

        // The child is down to a linkset of just itself
        return new BSLinkset(m_scene, child);
    }

    // An existing linkset had one of its members rebuilt or something.
    // Go through the linkset and rebuild the pointers to the bodies of the linkset members.
    public BSLinkset RefreshLinkset(BSPrim requestor)
    {
        BSLinkset ret = requestor.Linkset;

        lock (m_linksetActivityLock)
        {
            System.IntPtr aPtr = BulletSimAPI.GetBodyHandle2(m_scene.World.Ptr, m_linksetRoot.LocalID);
            if (aPtr == System.IntPtr.Zero)
            {
                // That's odd. We can't find the root of the linkset.
                // The linkset is somehow dead.  The requestor is now a member of a linkset of one.
                DetailLog("{0},RefreshLinkset.RemoveRoot,child={1}", m_linksetRoot.LocalID, m_linksetRoot.LocalID);
                ret = RemoveMeFromLinkset(m_linksetRoot);
            }
            else
            {
                // Reconstruct the pointer to the body of the linkset root.
                DetailLog("{0},RefreshLinkset.RebuildRoot,rootID={1},ptr={2}", m_linksetRoot.LocalID, m_linksetRoot.LocalID, aPtr);
                m_linksetRoot.Body = new BulletBody(m_linksetRoot.LocalID, aPtr);

                List<BSPrim> toRemove = new List<BSPrim>();
                foreach (BSPrim bsp in m_children)
                {
                    aPtr = BulletSimAPI.GetBodyHandle2(m_scene.World.Ptr, bsp.LocalID);
                    if (aPtr == System.IntPtr.Zero)
                    {
                        toRemove.Add(bsp);
                    }
                    else
                    {
                        // Reconstruct the pointer to the body of the linkset root.
                        DetailLog("{0},RefreshLinkset.RebuildChild,rootID={1},ptr={2}", bsp.LocalID, m_linksetRoot.LocalID, aPtr);
                        bsp.Body = new BulletBody(bsp.LocalID, aPtr);
                    }
                }
                foreach (BSPrim bsp in toRemove)
                {
                    RemoveChildFromLinkset(bsp);
                }
            }
        }

        return ret;
    }


    // Return 'true' if the passed object is the root object of this linkset
    public bool IsRoot(BSPrim requestor)
    {
        return (requestor.LocalID == m_linksetRoot.LocalID);
    }

    // Return 'true' if this linkset has any children (more than the root member)
    public bool HasAnyChildren { get { return (m_children.Count > 0); } }

    // Return 'true' if this child is in this linkset
    public bool HasChild(BSPrim child)
    {
        bool ret = false;
        foreach (BSPrim bp in m_children)
        {
            if (child.LocalID == bp.LocalID)
            {
                ret = true;
                break;
            }
        }
        return ret;
    }

    private float ComputeLinksetMass()
    {
        float mass = m_linksetRoot.MassRaw;
        foreach (BSPrim bp in m_children)
        {
            mass += bp.MassRaw;
        }
        return mass;
    }

    private OMV.Vector3 ComputeLinksetCenterOfMass()
    {
        OMV.Vector3 com = m_linksetRoot.Position * m_linksetRoot.MassRaw;
        float totalMass = m_linksetRoot.MassRaw;

        foreach (BSPrim bp in m_children)
        {
            com += bp.Position * bp.MassRaw;
            totalMass += bp.MassRaw;
        }
        com /= totalMass;

        return com;
    }

    private OMV.Vector3 ComputeLinksetGeometricCenter()
    {
        OMV.Vector3 com = m_linksetRoot.Position;

        foreach (BSPrim bp in m_children)
        {
            com += bp.Position * bp.MassRaw;
        }
        com /= m_children.Count + 1;

        return com;
    }

    // I am the root of a linkset and a new child is being added
    public void AddChildToLinkset(BSPrim pchild)
    {
        BSPrim child = pchild;
        if (!HasChild(child))
        {
            m_children.Add(child);

            m_scene.TaintedObject(delegate()
            {
                DebugLog("{0}: AddChildToLinkset: adding child {1} to {2}", LogHeader, child.LocalID, m_linksetRoot.LocalID);
                DetailLog("{0},AddChildToLinkset,child={1}", m_linksetRoot.LocalID, pchild.LocalID);
                PhysicallyLinkAChildToRoot(pchild);     // build the physical binding between me and the child
            });
        }
        return;
    }

    // I am the root of a linkset and one of my children is being removed.
    // Safe to call even if the child is not really in my linkset.
    public void RemoveChildFromLinkset(BSPrim pchild)
    {
        BSPrim child = pchild;

        if (m_children.Remove(child))
        {
            m_scene.TaintedObject(delegate()
            {
                DebugLog("{0}: RemoveChildFromLinkset: Removing constraint to {1}", LogHeader, child.LocalID);
                DetailLog("{0},RemoveChildFromLinkset,child={1}", m_linksetRoot.LocalID, pchild.LocalID);

                if (m_children.Count == 0)
                {
                    // if the linkset is empty, make sure all linkages have been removed
                    PhysicallyUnlinkAllChildrenFromRoot();
                }
                else
                {
                    PhysicallyUnlinkAChildFromRoot(pchild);
                }
            });
        }
        else
        {
            // This will happen if we remove the root of the linkset first. Non-fatal occurance.
            // m_scene.Logger.ErrorFormat("{0}: Asked to remove child from linkset that was not in linkset", LogHeader);
        }
        return;
    }

    // Create a constraint between me (root of linkset) and the passed prim (the child).
    // Called at taint time!
    private void PhysicallyLinkAChildToRoot(BSPrim childPrim)
    {
        // Zero motion for children so they don't interpolate
        childPrim.ZeroMotion();

        // relative position normalized to the root prim
        OMV.Quaternion invThisOrientation = OMV.Quaternion.Inverse(m_linksetRoot.Orientation);
        OMV.Vector3 childRelativePosition = (childPrim.Position - m_linksetRoot.Position) * invThisOrientation;

        // relative rotation of the child to the parent
        OMV.Quaternion childRelativeRotation = invThisOrientation * childPrim.Orientation;

        // create a constraint that allows no freedom of movement between the two objects
        // http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=4818
        // DebugLog("{0}: CreateLinkset: Adding a constraint between root prim {1} and child prim {2}", LogHeader, LocalID, childPrim.LocalID);
        DetailLog("{0},LinkAChildToMe,taint,root={1},child={2}", m_linksetRoot.LocalID, m_linksetRoot.LocalID, childPrim.LocalID);
        BSConstraint constrain = m_scene.Constraints.CreateConstraint(
                        m_scene.World, m_linksetRoot.Body, childPrim.Body,
                        // childRelativePosition,
                        // childRelativeRotation,
                        OMV.Vector3.Zero,
                        OMV.Quaternion.Identity,
                        OMV.Vector3.Zero,
                        OMV.Quaternion.Identity
                        );
        constrain.SetLinearLimits(OMV.Vector3.Zero, OMV.Vector3.Zero);
        constrain.SetAngularLimits(OMV.Vector3.Zero, OMV.Vector3.Zero);

        // tweek the constraint to increase stability
        constrain.UseFrameOffset(m_scene.BoolNumeric(m_scene.Params.linkConstraintUseFrameOffset));
        constrain.TranslationalLimitMotor(m_scene.BoolNumeric(m_scene.Params.linkConstraintEnableTransMotor),
                        m_scene.Params.linkConstraintTransMotorMaxVel,
                        m_scene.Params.linkConstraintTransMotorMaxForce);
        constrain.SetCFMAndERP(m_scene.Params.linkConstraintCFM, m_scene.Params.linkConstraintERP);

    }

    // Remove linkage between myself and a particular child
    // Called at taint time!
    private void PhysicallyUnlinkAChildFromRoot(BSPrim childPrim)
    {
        DebugLog("{0}: PhysicallyUnlinkAChildFromRoot: RemoveConstraint between root prim {1} and child prim {2}", 
                    LogHeader, m_linksetRoot.LocalID, childPrim.LocalID);
        DetailLog("{0},PhysicallyUnlinkAChildFromRoot,taint,root={1},child={2}", m_linksetRoot.LocalID, m_linksetRoot.LocalID, childPrim.LocalID);
        // BulletSimAPI.RemoveConstraint(_scene.WorldID, LocalID, childPrim.LocalID);
        m_scene.Constraints.RemoveAndDestroyConstraint(m_linksetRoot.Body, childPrim.Body);
    }

    // Remove linkage between myself and any possible children I might have
    // Called at taint time!
    private void PhysicallyUnlinkAllChildrenFromRoot()
    {
        // DebugLog("{0}: PhysicallyUnlinkAllChildren:", LogHeader);
        DetailLog("{0},PhysicallyUnlinkAllChildren,taint", m_linksetRoot.LocalID);
        m_scene.Constraints.RemoveAndDestroyConstraint(m_linksetRoot.Body);
        // BulletSimAPI.RemoveConstraintByID(_scene.WorldID, LocalID);
    }

    // Invoke the detailed logger and output something if it's enabled.
    private void DebugLog(string msg, params Object[] args)
    {
        m_scene.Logger.DebugFormat(msg, args);
    }

    // Invoke the detailed logger and output something if it's enabled.
    private void DetailLog(string msg, params Object[] args)
    {
        m_scene.PhysicsLogging.Write(msg, args);
    }

}
}