Collision Detection
<p>How do I stop my object going through another object once the collision has been detected in c#? I have image 1, image 2 and image 3 in which when image 1 collides with image 2 it dissapears but when it collides with image 3 it doesnt dissapear but it does collide. Now the problem i'm having is that i dont know what c# code to put in so that once image 1 collides with image 3, it doesnt go through the image. I want image 1 to not go any further but take an alternative route....</p>

<p>Any ideas guyS???</p>

<p>(BELOW IS THE CODE...IM TRYING TO NOT GO PASS MYCHEESE1 WHICH IS AN IMAGE....IT COLLIDES WITH A IMAGE CALLED JAHED1 BUT IT GOES THROUGH THE IMAGE....HOW DO I STOP THAT FROM HAPPENING?</p>

<hr>

<p>using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;</p>

<p>namespace Practice
{
    public partial class MainPage : UserControl
  {</p>

<pre><code>    Pig jahed1;
    Apple mycheese1;
    Apple2 mycheese2;

    int i = 0;


    public MainPage()
    {
        InitializeComponent();


        jahed1 = new Pig();
        mycheese1 = new Apple();
        mycheese2 = new Apple2();

        Image myImg = new Image();
        myImg.Source = new BitmapImage(new Uri("mouse.png", UriKind.Relative));
        jahed1.Content = myImg;
        LayoutRoot.Children.Add(jahed1);
        Canvas.SetLeft(jahed1, 100);
        Canvas.SetTop(jahed1, 50);

        LayoutRoot.Children.Add(mycheese2);
        LayoutRoot.Children.Add(mycheese1);


        CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
    }

    public void CompositionTarget_Rendering(object sender, EventArgs e)
    {

        mycheese1.Update(LayoutRoot);


        if (DetectCollision(jahed1, mycheese1))
        {


            LayoutRoot.Children.Remove(mycheese1);

            // label2.Content = "Score: + " i.ToString();


        }
        if (DetectCollision(jahed1, mycheese2))
        {
            //i = i + 1;
            //label2.Content = "Score: + " i.ToString();

            label2.Visibility = Visibility.Visible;

            label3.Visibility = Visibility.Visible;
            label2.Content = "What is 20 + 20";
            submit.Visibility = Visibility.Visible;



            // LayoutRoot.Visibility = Visibility.Visible;
            textBox1.Visibility = Visibility.Visible;



            LayoutRoot.Children.Remove(mycheese2);


        }

    }

    private void UserControl_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.Key == Key.Right)
            this.jahed1.Move(Direction.Right);
        else if (e.Key == Key.Left)
            this.jahed1.Move(Direction.Left);
        else if (e.Key == Key.Down)
            this.jahed1.Move(Direction.Down);
        else if (e.Key == Key.Up)
            this.jahed1.Move(Direction.Up);


    }


    public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2)
    {

        Rect ctrl1Rect = new Rect(
                new Point(Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)),
                                     Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty))),
                             new Point((Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)) + ctrl1.ActualWidth),
                                     (Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty)) + ctrl1.ActualHeight))
                     );

        Rect ctrl2Rect = new Rect(
    new Point(Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)),
                                    Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty))),
                            new Point((Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)) + ctrl2.ActualWidth),
                                    (Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty)) + ctrl2.ActualHeight))
                    );

        ctrl1Rect.Intersect(ctrl2Rect);
        return !(ctrl1Rect == Rect.Empty);
    }

    private void submit_Click_1(object sender, RoutedEventArgs e)
    {
        if (textBox1.Text == "40")
        {
            label3.Content = ("Well Done");
            button1.Visibility = Visibility.Visible;

        }
        else
        {
            label3.Content = ("Wrong Anwser");
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        App app = (App)Application.Current;



        App.Navigate(new Maths2());
    }
</code></pre>

<p>}
}</p>
