Exemplo n.º 1
0
    /*
     * Sets a joint point as the new start position. Removes all saved virtual paths and intersections.
     * */
    public void setStartPosition(JointPoint joint)
    {
        startJoint    = joint;
        intersections = new List <VirtualIntersection>();
        paths         = new List <VirtualPath>();

        VirtualIntersection intersection = CreateInstance <VirtualIntersection>();

        intersection.init(joint.getPosition(), joint, "" + intersections.Count);
        intersections.Add(intersection);

        intersection.setWalkingStartPosition(0, joint.getWalkingStartPosition(0));
        intersection.setWalkingStartPosition(1, joint.getWalkingStartPosition(1));
        intersection.setWalkingStartPosition(2, joint.getWalkingStartPosition(2));
        intersection.setWalkingStartPosition(3, joint.getWalkingStartPosition(3));

#if UNITY_EDITOR
        SceneView.RepaintAll();
        AssetDatabase.AddObjectToAsset(intersection, this);
        EditorUtility.SetDirty(this);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
#endif
        Debug.Log("Set new start position");
    }
Exemplo n.º 2
0
    /*
     * Creates a new path and the corresponding intersection at the end.
     * */
    public void createPathAndIntersection(VirtualIntersection startIntersection, Curve curve, float gain)
    {
        // Calculate radius of virtual path
        float radius = curve.getRadius() * gain;
        // Calculate length of curve
        float length = Mathf.Deg2Rad * curve.getAngle() * curve.getRadius();
        // Calculate angle of virtual path
        float angle = Mathf.Rad2Deg * (length / radius);

        int newPathIndex = this.getPathIndex(startIntersection.getJoint(), curve);

        // Calculate end joint point
        JointPoint endJointPoint = getCorrespondingEndJointPoint(startIntersection, curve);

        // Calculate the circle center of the new path
        Vector3 circleCenter = calculateCircleCenterOfPath(startIntersection, curve, radius);

        // Calculate (nomralized) direction vector from circle center to start intersection
        Vector3 directionToStartPath = (startIntersection.getWalkingStartPosition(newPathIndex) - circleCenter).normalized;

        // Calculate direction vector from circle center to end intersection by:
        // 1. rotating the direction vector to start intersection (around angle of path and angle of walking zone)
        // 2. extending the vector by the virtual radius
        float   angleOfWalkingZone = Mathf.Rad2Deg * startIntersection.getJoint().getWalkingZoneRadius() / radius;
        Vector3 directionToEndPath = Quaternion.AngleAxis(this.getSignOfCurve(startIntersection.getJoint(), endJointPoint) * angle, Vector3.up) * directionToStartPath;

        directionToEndPath = directionToEndPath * radius;

        Vector3 directionToEndIntersection = Quaternion.AngleAxis(this.getSignOfCurve(startIntersection.getJoint(), endJointPoint) * angleOfWalkingZone, Vector3.up) * directionToEndPath;

        // Calculate position of new end intersection
        Vector3 positionEndIntersection = circleCenter + directionToEndIntersection;

        VirtualIntersection endIntersection = CreateInstance <VirtualIntersection>();

        endIntersection.init(positionEndIntersection, endJointPoint, "" + intersections.Count);

        List <VirtualIntersection> endPoints = new List <VirtualIntersection>();

        endPoints.Add(startIntersection);
        endPoints.Add(endIntersection);

        VirtualPath path = CreateInstance <VirtualPath>();

        path.init(circleCenter, gain, curve, endPoints, radius, angle);

        intersections.Add(endIntersection);
        paths.Add(path);

        startIntersection.addPath(path, this.getPathIndex(startIntersection.getJoint(), curve));
        endIntersection.addPath(path, this.getPathIndex(endJointPoint, curve));

        calculateWalkingStartPositions(this.getPathIndex(endJointPoint, curve), endJointPoint, endIntersection, circleCenter, directionToEndPath);

#if UNITY_EDITOR
        SceneView.RepaintAll();
        AssetDatabase.AddObjectToAsset(endIntersection, this);
        AssetDatabase.AddObjectToAsset(path, this);
        EditorUtility.SetDirty(this);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
#endif
        Debug.Log("Created new path and intersection");
    }