Example #1
0
        public void Normalize()
        {
            float m = normal.Normalize();

            if (m < SBSMath.Epsilon)
            {
                return;
            }
            distance /= m;
        }
Example #2
0
        public static SBSMatrix4x4 LookAt(SBSVector3 eye, SBSVector3 target, SBSVector3 up)
        {
            SBSVector3 zAxis = target - eye;

            zAxis.Normalize();
            SBSVector3 xAxis = SBSVector3.Cross(up, zAxis);

            xAxis.Normalize();
            SBSVector3 yAxis = SBSVector3.Cross(zAxis, xAxis);

            return(new SBSMatrix4x4(
                       xAxis.x, xAxis.y, xAxis.z, 0.0f,
                       yAxis.x, yAxis.y, yAxis.z, 0.0f,
                       zAxis.x, zAxis.y, zAxis.z, 0.0f,
                       eye.x, eye.y, eye.z, 1.0f));
        }
Example #3
0
        public static SBSQuaternion LookRotation(SBSVector3 forward, SBSVector3 up)
        {
#if UNITY_FLASH
            forward = forward.normalized;
#else
            forward.Normalize();
#endif
            SBSVector3 right = SBSVector3.Cross(up.normalized, forward).normalized;
            up = SBSVector3.Cross(forward, right);

            float w    = SBSMath.Sqrt(1.0f + right.x + up.y + forward.z) * 0.5f,
                  oo4w = 1.0f / (4.0f * w),
                  x    = (forward.y - up.z) * oo4w,
                  y    = (right.z - forward.x) * oo4w,
                  z    = (up.x - right.y) * oo4w;

            return(new SBSQuaternion(x, y, z, w));
        }
        public static SBSQuaternion LookRotation(SBSVector3 forward, SBSVector3 up)
        {
#if UNITY_FLASH
            forward = forward.normalized;
#else
            forward.Normalize();
#endif
            SBSVector3 right = SBSVector3.Cross(up.normalized, forward).normalized;
            up = SBSVector3.Cross(forward, right);

            float w    = SBSMath.Sqrt(1.0f + right.x + up.y + forward.z) * 0.5f,
                  oo4w = 1.0f / (4.0f * w),
                  x    = (forward.y - up.z) * oo4w,
                  y    = (right.z - forward.x) * oo4w,
                  z    = (up.x - right.y) * oo4w;

            return new SBSQuaternion(x, y, z, w);
        }
Example #5
0
    public void CreateBoxColliders(int numSegments, float trasversalOffset, float wallsHeight, float depth)
    {
        if (numSegments < 1)
        {
            return;
        }

        BoxCollider[] colliders = gameObject.GetComponentsInChildren <BoxCollider>();
        foreach (BoxCollider coll in colliders)
        {
            GameObject.DestroyImmediate(coll.gameObject);
        }
        colliders = null;

        float l = 0.0f,
              s = 1.0f / (float)numSegments;

        for (int i = 0; i < numSegments; ++i)
        {
#if UNITY_FLASH
            SBSVector3 p0 = new SBSVector3(), p1 = new SBSVector3(), q0 = new SBSVector3(), q1 = new SBSVector3(), t = new SBSVector3();

            this.TokenToWorld(l, -1.0f - trasversalOffset, p0, t);
            this.TokenToWorld(l + s, -1.0f - trasversalOffset, p1, t);
            this.TokenToWorld(l, 1.0f + trasversalOffset, q0, t);
            this.TokenToWorld(l + s, 1.0f + trasversalOffset, q1, t);
#else
            SBSVector3 p0, p1, q0, q1, t;

            this.TokenToWorld(l, -1.0f - trasversalOffset, out p0, out t);
            this.TokenToWorld(l + s, -1.0f - trasversalOffset, out p1, out t);
            this.TokenToWorld(l, 1.0f + trasversalOffset, out q0, out t);
            this.TokenToWorld(l + s, 1.0f + trasversalOffset, out q1, out t);
#endif

            SBSVector3 tanL  = p1 - p0,
                       tanR  = q1 - q0,
                       biTan = q0 - p0;
            float sizeX      = biTan.Normalize(),
                  sizeZL     = tanL.Normalize() + 0.2f,
                  sizeZR     = tanR.Normalize() + 0.2f;

            SBSQuaternion rotL = SBSQuaternion.AngleAxis(SBSVector3.Angle(SBSVector3.forward, tanL), SBSVector3.Cross(SBSVector3.forward, tanL)),
                          rotR = SBSQuaternion.AngleAxis(SBSVector3.Angle(SBSVector3.forward, tanR), SBSVector3.Cross(SBSVector3.forward, tanR));

            SBSVector3 groundCenter    = (p0 + p1 + q0 + q1) * 0.25f - SBSVector3.up * depth * 0.5f,
                       wallLeftCenter  = (p0 + p1 + SBSVector3.up * wallsHeight - biTan * depth) * 0.5f,
                       wallRightCenter = (q0 + q1 + SBSVector3.up * wallsHeight + biTan * depth) * 0.5f,
                       groundSize      = new SBSVector3(sizeX, depth, SBSMath.Max(sizeZL, sizeZR)),
                       wallLeftSize    = new SBSVector3(depth, wallsHeight, sizeZL),
                       wallRightSize   = new SBSVector3(depth, wallsHeight, sizeZR);

            GameObject ground = new GameObject("Ground", typeof(BoxCollider));
            ground.transform.parent   = transform;
            ground.transform.rotation = rotL;
            ground.transform.position = groundCenter;
            ground.GetComponent <BoxCollider>().size = groundSize;

            GameObject wallLeft  = new GameObject("WallLeft", typeof(BoxCollider)),
                       wallRight = new GameObject("WallRight", typeof(BoxCollider));
            wallLeft.transform.parent = wallRight.transform.parent = transform;

            wallLeft.transform.rotation = rotL;
            wallLeft.GetComponent <BoxCollider>().size = wallLeftSize;
            wallLeft.transform.position = wallLeftCenter;

            wallRight.transform.rotation = rotR;
            wallRight.GetComponent <BoxCollider>().size = wallRightSize;
            wallRight.transform.position = wallRightCenter;

            l += s;
        }
    }