Search code examples
c#wpfcollision-detection

Ellipse Bounce using IntersectsWith


I have a application where is ball (ellipse) and 4 rectangles (each for edge of window). I try to write a app where ball is moving and bouncing from the edges. I don't know how to make a method where ball changes a direction when collision is true.

XAML:

<Window x:Class="naszybkodlatestu.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="320" Width="640">
<Canvas>
    <Rectangle Name="boardTop"
               Height="2"
               Fill="Black"
               Width="609" Canvas.Left="7" Canvas.Top="10"/>
    <Rectangle Name="boardLeft"
               Height="270"
               Fill="Black"
               Width="2" Canvas.Left="614" Canvas.Top="10"/>
    <Rectangle Name="boardRight"
               Height="269"
               Fill="Black"
               Width="2" Canvas.Left="7" Canvas.Top="12"/>
    <Rectangle Name="boardBot"
               Height="2"
               Fill="Black"
               Width="609" Canvas.Left="7" Canvas.Top="278" />
    <Ellipse Name="ball"
             Width="15"
             Height="15"
             Fill="Red" 
             Canvas.Left="303" 
             Canvas.Top="107"/>
</Canvas>
</Window>

C#:

DispatcherTimer dTimer;
private void InitTimer()
{
    dTimer = new DispatcherTimer();
    dTimer.Tick += new EventHandler(TickdTimer);
    dTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
    dTimer.Start();
}
public MainWindow()
{
    InitializeComponent();
    InitTimer();
    InitMove();

}

private bool isMovingUp = false;
private bool isMovingDown = false;
private bool isMovingLeft = true;
private bool isMovingRight = false;

private double ballDirectionX = 0;
private double ballDirectionY = 0;

private double ballSpeed = 1;

private bool CollisionDetection()
{
    if (CheckCollision(ball, boardBot) || CheckCollision(ball, boardRight) ||
        CheckCollision(ball, boardTop) || CheckCollision(ball, boardLeft))

    {
        return true;
    }
    else return false;
}

public static bool CheckCollision(FrameworkElement a, FrameworkElement b)
{
    Rect rect1 = new Rect((double)a.GetValue(Canvas.LeftProperty), (double)a.GetValue(Canvas.TopProperty), a.Width, a.Height);
    Rect rect2 = new Rect((double)b.GetValue(Canvas.LeftProperty), (double)b.GetValue(Canvas.TopProperty), b.Width, b.Height);

    if (rect1.IntersectsWith(rect2))
    {
        return true;
    }
        else return false;
}

private void InitMove()
{

    if (isMovingDown)
    {
        ballDirectionY += ballSpeed;
    }
    if (isMovingUp)
    {
        ballDirectionY -= ballSpeed;
    }
    if (isMovingLeft)
    {
        ballDirectionX -= ballSpeed;
    }
    if (isMovingRight)
    {
        ballDirectionX += ballSpeed;
    }
    ball.SetValue(Canvas.TopProperty, ballDirectionY);
    ball.SetValue(Canvas.LeftProperty, ballDirectionX);
}

So i tried smth like that in InitMove(). It's just a test code to show you what is a problem.

private void InitMove()
{
    .......
    if (CollisionDetection())
    {
        if (isMovingLeft)
        {
            isMovingLeft = false;
            isMovingRight = true;
        }
    }

So ball changed direction from left to right. Ok for now.

Problem starts now when i want to put next "if".

private void InitMove()
{
    .......
    if (CollisionDetection())
    {
        if (isMovingLeft)
        {
            isMovingLeft = false;
            isMovingRight = true;
        }
        if (isMovingRight)
        {
            isMovingRight = false;
            isMovingLeft = true;
        }
    }

Now ball is passing by edge of rectangle and there is no bounce. So here is my problem.

How to make it right?

//EDIT

When i added else

if (isMovingLeft)
{
    isMovingLeft = false;
    isMovingRight = true;
}
else if (isMovingRight)
{
    isMovingRight = false;
    isMovingLeft = true;
}

ball just stopped at edge of a window.

//EDIT2

ok so i think i know how to do this thx to you

if (isMovingLeft)
{
    isMovingLeft = false;
    isMovingRight = true;
    ballDirectionX += 2;
}
else if (isMovingRight)
{
    isMovingRight = false;
    isMovingLeft = true;
    ballDirectionX -= 2;
}

So i added some move to push this ball out of wall collision. I dont see another way for now.


Solution

  • You should add a else statement :

        if (isMovingLeft)
        {
            isMovingLeft = false;
            isMovingRight = true;
        }
        else if (isMovingRight)
        {
            isMovingRight = false;
            isMovingLeft = true;
        }