none
WPF Collsion detection

    Question

  • What I have is simple shooting game in WPF  where I will fire a bullet at a object, what I am having trouble is getting a Collision detection working, right now I’m trying to use a hittest to accomplish this, I’m not sure if this is even possible, cause I know right now it is not working.  Let me show you what I got:

    I have a timer set up to call this sub “CheckHit”, which will call a function passing on the bullet and the object I want to hit, with the result coming back as a True/False, like this

    I store the bullets in a List of Rectangles, right now I’m only passing on one bullet just trying to get it to work, the “myrect” is just a Rectangle that I have put on a Canvas to make things simple for now,  I have both objects set at a Z-order of 1

    Private Sub checkhit()

            Dim Is_HIt As Boolean = CollidsWith(bulletholder(0), myrect)

            If Is_HIt = True Then

                'DO what I need done

            End If

        End Sub

    Here is the Function:

    Public Function CollidsWith(Bullets As FrameworkElement, Object_To_Hit As FrameworkElement) As Boolean

            Dim pt As New Point

            For x = Canvas.GetLeft(Bullets) To Canvas.GetLeft(Bullets) + Bullets.Width

                For y = Canvas.GetTop(Bullets) To Canvas.GetTop(Bullets) + Bullets.Height

                    pt.X = x

                    pt.Y = y

                    Dim result As HitTestResult = VisualTreeHelper.HitTest(Object_To_Hit, pt)

                    If result IsNot Nothing Then

                        Return True

                    End If

                Next

            Next

    I just set up a <place w:st="on">Loop</place> to go through all the point along the Top and Sides, then test these points on a “Hittest” but it will never return true when I have it set against the “object_to_hit”.  I’ve discovered that if I change the hittest from “object_to_hit” to this

                    Dim result As HitTestResult = VisualTreeHelper.HitTest(my_canvas, pt)

    Where “my_canvas” is the name of my canvas, it will return true, so im not sure why it will work against the canvas but not against the Rectangle I want to hit.

    I have to Bullet Moving with a DoubleAnimation, and the Timer will fire every 100 MS

    Any help or suggestions I would appreciate it

    • Moved by Mark Liu-lxf Friday, October 12, 2012 3:18 AM (From:Visual Basic General)
    Thursday, October 11, 2012 4:55 PM

Answers

  • Refer to this open source : http://farseerphysics.codeplex.com/

    Farseer Physics Engine is a collision detection system with realistic physics responses. This means you can create a game or robotic simulation easily using the engine and the associated tools


    Stay hungry, stay foolish

    Tuesday, October 16, 2012 4:42 AM

All replies

  • Hi lilloc1552,

    Thanks for you post.

    There is a special forum to deal with WPF issue. For better support, I will move this thread to Windows Presentation Foundation (WPF) forum. It will cost a little time to involve the members in this forum. I appreciate your patience.

    Sorry for any inconvenience and have a nice day.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 12, 2012 3:17 AM
  • Refer to this open source : http://farseerphysics.codeplex.com/

    Farseer Physics Engine is a collision detection system with realistic physics responses. This means you can create a game or robotic simulation easily using the engine and the associated tools


    Stay hungry, stay foolish

    Tuesday, October 16, 2012 4:42 AM