Exemplo n.º 1
0
        public bool AddMultiBody(BMultiBody mb)
        {
            if (!_isDisposed)
            {
                if (m_worldType < WorldType.MultiBodyWorld)
                {
                    Debug.LogError("World type must be be multibody");
                }
                if (debugType >= BDebug.DebugType.Debug)
                {
                    Debug.LogFormat("Adding multibody {0} to world", mb);
                }
                // This is complicated because the native parts for multiple components are created by bullet when the MultiBody is created. We
                // need to let Bullet create these then get the references to the native parts and assign them to the components.
                if (mb._BuildMultiBody())
                {
                    ((MultiBodyDynamicsWorld)m_world).AddMultiBody(mb.GetMultiBody(), (int)mb.groupsIBelongTo, (int)mb.collisionMask);
                    mb.CreateColliders();
                    if (debugType >= BDebug.DebugType.Debug)
                    {
                        Debug.LogFormat("Adding MultiBodyBaseCollider {0} to world", mb);
                    }
                    m_world.AddCollisionObject(mb.GetBaseCollider(), mb.groupsIBelongTo, mb.collisionMask);
                    List <BMultiBodyLink> links = mb.GetLinks();
                    for (int i = 0; i < links.Count; i++)
                    {
                        BMultiBodyLink link = links[i];
                        if (debugType >= BDebug.DebugType.Debug)
                        {
                            Debug.LogFormat("Adding MultiBodyLinkCollider {0} to world", link);
                        }
                        m_world.AddCollisionObject(link.GetLinkCollider(), link.groupsIBelongTo, link.collisionMask);
                        link.isInWorld = true;
                        BMultiBodyConstraint bmbc = link.GetComponent <BMultiBodyConstraint>();
                        if (bmbc != null)
                        {
                            MultiBodyConstraint mbc = bmbc.CreateMultiBodyConstraint(mb.GetMultiBody());
                            mbc.FinalizeMultiDof();
                            if (mbc != null)
                            {
                                if (debugType >= BDebug.DebugType.Debug)
                                {
                                    Debug.LogFormat("Adding MultiBodyLinkConstraint {0} to world", mbc);
                                }
                                ((MultiBodyDynamicsWorld)m_world).AddMultiBodyConstraint(mbc);
                                bmbc.isInWorld = true;
                            }
                        }
                    }

                    MultiBody mbb = mb.GetMultiBody();
                    for (int i = 0; i < links.Count; i++)
                    {
                        MultiBodyLink mbLink = mbb.GetLink(i);
                        links[i].AssignMultiBodyLinkOnCreation(mb, mbLink);
                    }

                    mb.isInWorld = true;
                }
                else
                {
                    if (debugType >= BDebug.DebugType.Debug)
                    {
                        Debug.LogWarningFormat("Failed To Add MultiBody {0} to world ", mb);
                    }
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        internal bool _BuildMultiBody()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (m_multibody != null && isInWorld && world != null)
            {
                isInWorld = false;
                world.RemoveMultiBody(this);
            }

            if (transform.localScale != UnityEngine.Vector3.one)
            {
                Debug.LogErrorFormat("The local scale on {0} rigid body is not one. Bullet physics does not support scaling on a rigid body world transform. Instead alter the dimensions of the CollisionShape.", name);
            }

            if (!fixedBase && baseMass <= 0f)
            {
                Debug.LogErrorFormat("If not fixed base then baseMass must be greater than zero.");
                return(false);
            }

            m_baseCollisionShape = GetComponent <BCollisionShape>();
            if (m_baseCollisionShape == null)
            {
                Debug.LogErrorFormat("There was no collision shape component attached to this BMultiBody. {0}", name);
                return(false);
            }
            if (GetComponent <BMultiBodyLink>() != null)
            {
                Debug.LogErrorFormat("There must not be a BMultiBodyLink component attached to the gameObject with a BMultiBody component. {0}", name);
                return(false);
            }

            m_links = new List <BMultiBodyLink>();
            if (!GetLinksInChildrenAndNumber(transform, m_links, -1))
            {
                Debug.LogError("Error building multibody");
                return(false);
            }

            if (m_links.Count == 0)
            {
                Debug.LogError("Could not find any links");
                return(false);
            }

            if (!ValidateMultiBodyHierarchy(m_links))
            {
                return(false);
            }

            BCollisionShape[]      shapes      = new BCollisionShape[m_links.Count];
            BMultiBodyConstraint[] constraints = new BMultiBodyConstraint[m_links.Count];
            for (int i = 0; i < m_links.Count; i++)
            {
                shapes[i] = m_links[i].GetComponent <BCollisionShape>();
                if (shapes[i] == null && shapes[i].GetComponent <RigidBody>() != null)
                {
                    Debug.LogErrorFormat("BMultiBodyLink must not have a RigidBody component. {0}", m_links[i]);
                    return(false);
                }
                constraints[i] = m_links[i].GetComponent <BMultiBodyConstraint>();
            }

            BulletSharp.MultiBody mb = m_multibody;
            CreateOrConfigureMultiBody(ref mb, baseMass, shapes, constraints);
            m_multibody = mb;

            //TODO is this allowed
            //m_multibody.UserObject = this;

            return(true);
        }