Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / WPF

WPF Grand Prix

Rate me:
Please Sign up or sign in to vote.
4.97/5 (110 votes)
14 Dec 2010CPOL10 min read 386.5K   6.3K   176  
An article showing how to create a race game using the powerful WPF capabilities
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace F1.TrackSegments
{
    /// <summary>
    /// Interaction logic for TrackSWCorner.xaml
    /// </summary>
    public partial class TrackSWCorner : UserControl, ITrackSegment
    {
        public const double SEGMENT_HEIGHT = 200;
        public const double SEGMENT_WIDTH = 200;

        public TrackSWCorner()
        {
            InitializeComponent();
        }

        public Point GetMidTrackPoint(double x, double y)
        {
            var dX = this.Position.X + SEGMENT_WIDTH - x;
            var dY = y - this.Position.Y;
            var h = Math.Sqrt(dX * dX + dY * dY);
            var cos = dX / h;
            var sin = dY / h;
            var aCos = Math.Acos(cos);

            var trackX = this.Position.X + SEGMENT_WIDTH - (SEGMENT_WIDTH / 2) * cos;
            var trackY = this.Position.Y + (SEGMENT_HEIGHT / 2) * sin;
            return new Point(trackX, trackY);
        }

        public double GetTrackAngle(double x, double y, double lastAngle, double lastCarAngle)
        {
            var dX = this.Position.X + SEGMENT_WIDTH - x;
            var dY = y - this.Position.Y;
            var h = Math.Sqrt(dX * dX + dY * dY);
            var cos = dX / h;
            var aCos = Math.Acos(cos);
            var angle = 0.0;

            var modLastCarAngle = lastCarAngle % 360;
            if (modLastCarAngle < 0)
                modLastCarAngle = 360.0 + modLastCarAngle;

            if (modLastCarAngle >= 0 && modLastCarAngle <= 180)
            {
                angle = 180.0 + 90.0 * (aCos / (Math.PI / 2));
            }
            else
            {
                angle = 90.0 * (aCos / (Math.PI / 2));
            }

            return angle;
        }

        public Point Position { get; set; }
        public int Row { get; set; }
        public int Column { get; set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions