protected virtual void OnDestroy() { if (_constraint != null) { BulletPhysicsWorldManager.Unregister(_constraint); Dispose(); } }
protected virtual void OnEnable() { if (_constraint == null) { InitializeConstraint(); InitializeConstraintProperties(); BulletPhysicsWorldManager.Register(_constraint); } else { _constraint.IsEnabled = true; } }
//Unity OnDisable protected override void OnDisable() { //Check if registered //TODO: See OnEnable() if (!Registered || !Initialized) { return; } //Unregister from the physics world if (!Initialized) { return; } BulletPhysicsWorldManager.Unregister(BodyInstance); Registered = false; }
/// <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(); }
/// <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; }
//Unity OnEnable protected override void OnEnable() { //Check if initialized and already registered //TODO: Physics world should probably implement a method to return registration status if (!Initialized) { InitializePhysicsBody(); } if (Registered) { return; } //Register with the physics world if (!Initialized) { return; } BulletPhysicsWorldManager.Register(BodyInstance); Registered = true; }
//Attempt to register with the physics world protected virtual void Awake() { BulletPhysicsWorldManager.Register(this); BRigidBody = GetComponent <BulletRigidBody>(); }
/// <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; }