Esempio n. 1
0
        /// <summary>
        /// Disposes of the RigidBody
        /// Should only be called internally or from a physics world instance.
        /// </summary>
        public override void Dispose()
        {
            //Dispose of all the components in reverse order
            if (Disposing)
            {
                return;
            }
            if (Registered)
            {
                BulletPhysicsWorldManager.Unregister(_staticBody);
            }

            Disposing = true;
            _staticBody?.Dispose();
            _constructionInfo?.Dispose();
            PhysicsCollisionShape?.Dispose();
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes the Bullet StaticBody and attempts to register it with the physics world.
        /// This should generally only be called internally or from a physics world instance.
        /// </summary>
        public override void InitializePhysicsBody()
        {
            //Check if already initialized
            if (Initialized)
            {
                return;
            }

            //Make sure a BulletCollisionShape is attached and reference it if possible
            PhysicsCollisionShape = GetComponent <BulletCollisionShape>();
            if (PhysicsCollisionShape == null)
            {
                Debug.LogError("No Bullet collision shape is attached!\n" +
                               "Please attach a Bullet collision shape to this object");
                return;
            }

            //Initialize the Bullet transform matrix and set the values based on Unity transform
            InitialTransform = Matrix.AffineTransformation(1f, transform.rotation.ToBullet(), transform.position.ToBullet());

            //Initialize the Bullet default motion state using the transform matrix
            if (PhysicsCollisionShape.GetShapeType() == CollisionShapeType.StaticPlane)
            {
                InitialTransform.Origin = transform.position.ToBullet() - transform.up.ToBullet();
            }

            //Initialize the Bullet rigidbody construction info and assign the relevant values
            _constructionInfo = new RigidBodyConstructionInfo(0f, null, PhysicsCollisionShape.GetCollisionShape())
            {
                StartWorldTransform = InitialTransform,
                Friction            = _friction,
                Restitution         = _restitution
            };

            //Create the Bullet RigidBody
            _staticBody = new RigidBody(_constructionInfo);

            //Register with the physics world
            BulletPhysicsWorldManager.Register(_staticBody);
            Registered = true;

            //Initialization complete
            Initialized = true;
        }
        /// <summary>
        /// Initializes the Bullet RigidBody and attempts to register it with the physics world.
        /// This should generally only be called internally or from a physics world instance.
        /// </summary>
        public override void InitializePhysicsBody()
        {
            //Check if already initialized
            if (Initialized)
            {
                return;
            }

            //Make sure a BulletCollisionShape is attached and reference it if possible
            PhysicsCollisionShape = GetComponent <BulletCollisionShape>();
            if (PhysicsCollisionShape == null)
            {
                Debug.LogError("No Bullet collision shape is attached!\n" +
                               "Please attach a Bullet collision shape to this object");
                return;
            }

            //Calculate the local intertial of the given shape
            PhysicsCollisionShape.GetCollisionShape().CalculateLocalInertia(_mass, out _localInternia);

            //Initialize the Bullet transform matrix and set the values based on Unity transform
            InitialTransform = Matrix.AffineTransformation(1f, transform.rotation.ToBullet(), transform.position.ToBullet());

            //Initialize the Bullet default motion state using the transform matrix
            PhysicsMotionState = new BulletMotionState(transform);

            //Initialize the Bullet rigidbody construction info and assign the relevant values
            _constructionInfo = new RigidBodyConstructionInfo(_mass, PhysicsMotionState, PhysicsCollisionShape.GetCollisionShape())
            {
                LocalInertia             = _localInternia,
                LinearDamping            = _linearDamping,
                AngularDamping           = _angularDamping,
                Friction                 = _friction,
                RollingFriction          = _rollingFriction,
                Restitution              = _restitution,
                LinearSleepingThreshold  = _linearSleepThreshold,
                AngularSleepingThreshold = _angularSleepThreshold
            };

            //Create the Bullet RigidBody
            BodyInstance = new RigidBody(_constructionInfo)
            {
                LinearFactor  = _linearFactor.ToBullet(),
                AngularFactor = _angularFactor.ToBullet()
            };
            BodyInstance.CollisionFlags = CollisionFlags.CustomMaterialCallback;
            //Set sleeping flag
            if (!_allowSleeping)
            {
                BodyInstance.ActivationState = ActivationState.DisableDeactivation;
            }
            //_rigidBody.CcdMotionThreshold = 0.5f;
            //_rigidBody.CcdSweptSphereRadius = 0.25f;

            //Register with the physics world
            BulletPhysicsWorldManager.Register(BodyInstance);
            Registered = true;

            //Initialization complete
            Initialized = true;
        }