コード例 #1
0
        public void Awake()
        {
            initialPosition = transform.position;
            tmpX            = transform.position.x;
            tmpY            = transform.position.y;
            tmpZ            = transform.position.z;

            Collider[] otherThings = FindObjectsOfType <Collider>();
            otherColliders  = new List <SimpleRigidbody3D>();
            staticColliders = new List <BoxCollider>();

            foreach (SimpleRigidbody3D rigi in FindObjectsOfType <SimpleRigidbody3D>())
            {
                if (rigi == this)
                {
                    continue;
                }
                otherColliders.Add(rigi);
            }

            for (int i = 0; i < otherThings.Length; i++)
            {
                if (otherThings[i].GetComponent <BoxCollider>() != null)
                {
                    staticColliders.Add(otherThings[i].GetComponent <BoxCollider>());
                }
            }


            time   = FindObjectOfType <SimplePhysics>();
            radius = transform.localScale.x / 2.0f;
        }
コード例 #2
0
        public bool FixCollisions()
        {
            bool fixedSomething = false;

            for (int i = 0; i < otherColliders.Count; i++)
            {
                SimpleRigidbody3D other = otherColliders[i];
                float             colX, colY, colZ;
                float             normX, normY, normZ;

                if (SphereSphereCollision(this, other, out colX, out colY, out colZ, out normX, out normY, out normZ))
                {
                    fixedSomething = true;

                    float newVelX, newVelY, newVelZ;
                    SimplePhysics.VectorAfterNormalForce(velocityX, velocityY, velocityZ, normX, normY, normZ, out newVelX, out newVelY, out newVelZ);
                    float lostVelX = velocityX - newVelX;
                    float lostVelY = velocityY - newVelY;
                    float lostVelZ = velocityZ - newVelZ;

                    float otherNewVelX, otherNewVelY, otherNewVelZ;
                    SimplePhysics.VectorAfterNormalForce(other.velocityX, other.velocityY, other.velocityZ, -normX, -normY, -normZ, out otherNewVelX, out otherNewVelY, out otherNewVelZ);
                    float otherLostVelX = other.velocityX - otherNewVelX;
                    float otherLostVelY = other.velocityY - otherNewVelY;
                    float otherLostVelZ = other.velocityZ - otherNewVelZ;

                    float avgLostVelX = (lostVelX + otherLostVelX) / 2.0f;
                    float avgLostVelY = (lostVelY + otherLostVelY) / 2.0f;
                    float avgLostVelZ = (lostVelZ + otherLostVelZ) / 2.0f;

                    velocityX = newVelX * time.friction + avgLostVelX;
                    velocityY = newVelY * time.friction + avgLostVelY;
                    velocityZ = newVelZ * time.friction + avgLostVelZ;

                    other.velocityX = otherNewVelX * time.friction + avgLostVelX;
                    other.velocityY = otherNewVelY * time.friction + avgLostVelY;
                    other.velocityZ = otherNewVelZ * time.friction + avgLostVelZ;

                    float dx   = colX - tmpX;
                    float dy   = colY - tmpY;
                    float dz   = colZ - tmpZ;
                    float dist = Mathf.Sqrt(dx * dx + dy * dy + dz * dz);


                    float moveOffsetX = normX * (radius - dist);
                    float moveOffsetY = normY * (radius - dist);
                    float moveOffsetZ = normZ * (radius - dist);

                    tmpX += moveOffsetX;
                    tmpY += moveOffsetY;
                    tmpZ += moveOffsetZ;

                    other.tmpX -= moveOffsetX;
                    other.tmpY -= moveOffsetY;
                    other.tmpZ -= moveOffsetZ;
                }
            }

            for (int i = 0; i < staticColliders.Count; i++)
            {
                BoxCollider other = staticColliders[i];
                float       colX, colY, colZ;
                float       normX, normY, normZ;

                if (SphereBoxCollision(this, other, out colX, out colY, out colZ, out normX, out normY, out normZ))
                {
                    fixedSomething = true;

                    float newVelX, newVelY, newVelZ;
                    SimplePhysics.VectorAfterNormalForce(velocityX, velocityY, velocityZ, normX, normY, normZ, out newVelX, out newVelY, out newVelZ);
                    velocityX = newVelX * time.friction;
                    velocityY = newVelY * time.friction;
                    velocityZ = newVelZ * time.friction;


                    float dx   = colX - tmpX;
                    float dy   = colY - tmpY;
                    float dz   = colZ - tmpZ;
                    float dist = Mathf.Sqrt(dx * dx + dy * dy + dz * dz);

                    float moveOffsetX = normX * (radius - dist);
                    float moveOffsetY = normY * (radius - dist);
                    float moveOffsetZ = normZ * (radius - dist);

                    tmpX += moveOffsetX;
                    tmpY += moveOffsetY;
                    tmpZ += moveOffsetZ;
                }
            }

            return(fixedSomething);
        }