Example #1
0
    public void Initialise(WaveParams waveParams)
    {
        // Instantiate the wave segments, and set the initial wave rotation

        WaveSegments = new WaveSegment[waveParams.SegmentCount];
        var waveWidth = WaveSegment.SEGMENT_WIDTH * waveParams.SegmentCount;

        var leftOrigin = new Vector3(-waveWidth / 2, 0, 0);
        var leftControlPointInOrigin  = new Vector3(-0.2f, 0, 0).Rotated(new Vector3(0, 0, 1), waveParams.LeftControlRotationRad);
        var leftControlPointOutOrigin = new Vector3(0.2f, 0, 0).Rotated(new Vector3(0, 0, 1), waveParams.LeftControlRotationRad);

        var rightOrigin = new Vector3(waveWidth / 2, 0, 0);
        var rightControlPointInOrigin  = new Vector3(0.2f, 0, 0).Rotated(new Vector3(0, 0, 1), waveParams.RightControlRotationRad);
        var rightControlPointOutOrigin = new Vector3(-0.2f, 0, 0).Rotated(new Vector3(0, 0, 1), waveParams.RightControlRotationRad);

        var curve = new Curve3D();
        var gap   = rightOrigin - leftOrigin;

        // control point locations are relative to the curve point origin (i.e not global coordinates)
        curve.AddPoint(leftOrigin,
                       SetControlPoint(gap.Length(), leftControlPointInOrigin),
                       SetControlPoint(gap.Length(), leftControlPointOutOrigin));

        curve.AddPoint(rightOrigin,
                       SetControlPoint(gap.Length(), rightControlPointInOrigin),
                       SetControlPoint(gap.Length(), rightControlPointOutOrigin));

        // instantiate wave segments
        var segmentFraction = (float)1 / waveParams.SegmentCount;

        for (var i = 0; i < waveParams.SegmentCount; i++)
        {
            var waveSegment = WaveSegmentScene.Instance() as WaveSegment;

            // xOffset moves from leftOrigin to rightOrigin in a number of steps equal to the segment count
            var xOffset = leftOrigin.x + (rightOrigin.x - leftOrigin.x) * (i * segmentFraction);
            var yOffset = curve.Interpolate(0, i * segmentFraction + (segmentFraction / 2));

            waveSegment.Translation = new Vector3(xOffset, yOffset.y, 0);

            AddChild(waveSegment);
            WaveSegments[i] = waveSegment;
        }

        Rotation        = new Vector3(0, 0, waveParams.InitialRotationRad);
        RotationRateRad = waveParams.RotationRateRad;
    }