Example #1
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);
            }
        }
Example #2
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;
        }
    }
Example #3
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;
        }
    }