Beispiel #1
0
        public static SBSVector3 RandomDirection(SBSVector3 direction, float angle)
        {
            SBSVector3 d = direction.normalized;
            SBSVector3 t = SBSVector3.Cross(d + (SBSVector3)UnityEngine.Random.onUnitSphere, d).normalized;

            return(SBSQuaternion.AngleAxis(UnityEngine.Random.value * angle, t) * d);
        }
Beispiel #2
0
        public static SBSVector3 Rotate(SBSVector3 from, SBSVector3 to, float t)
        {
            SBSVector3 a = SBSVector3.Cross(from, to);

            if (a.sqrMagnitude > 1.0e-3f)
            {
                return(SBSQuaternion.AngleAxis(SBSVector3.Angle(from, to) * t, a) * from);
            }
            else
            {
                return(to);
            }
        }
Beispiel #3
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));
        }
Beispiel #4
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));
        }
Beispiel #5
0
    public void WorldToToken(SBSVector3 worldPos, out float longitudinal, out float trasversal)
    {
        switch (type)
        {
        case TokenType.Cross:
        case TokenType.Rect:
            SBSVector3 localPos = matInvWorld.MultiplyPoint3x4(worldPos);
            longitudinal = SBSVector3.Dot(localPos, SBSVector3.forward) / lengthOrRadius;
            trasversal   = SBSVector3.Dot(localPos, SBSVector3.right) * 2.0f / width;
            break;

        case TokenType.Curve:
            SBSVector3 c  = this.Center,
                       p0 = matWorld.position - c,
                       p1 = worldPos - c;

            p0.DecrementBy(SBSVector3.Dot(p0, SBSVector3.up) * SBSVector3.up);
            p1.DecrementBy(SBSVector3.Dot(p1, SBSVector3.up) * SBSVector3.up);

            float sign  = SBSMath.Sign(SBSVector3.Dot(SBSVector3.Cross(p0, p1), SBSVector3.up));
            float angle = SBSVector3.Angle(p0, p1);

            longitudinal = angle / arcAngle;
            if (sign == curveDir)
            {
                longitudinal *= -1.0f;
            }
            trasversal = (p1.magnitude - lengthOrRadius) * 2.0f * curveDir / width;
            break;

        default:
            longitudinal = 0.0f;
            trasversal   = 0.0f;
            break;
        }
    }
Beispiel #6
0
 public void Set3Points(SBSVector3 p0, SBSVector3 p1, SBSVector3 p2)
 {
     normal   = SBSVector3.Cross(p1 - p0, p2 - p0);
     distance = -SBSVector3.Dot(normal, p0);
 }
Beispiel #7
0
 public SBSPlane(SBSVector3 p0, SBSVector3 p1, SBSVector3 p2)
 {
     normal   = SBSVector3.Cross(p1 - p0, p2 - p0);
     distance = -SBSVector3.Dot(normal, p0);
 }
Beispiel #8
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;
        }
    }