Windows Vista Forums

Intersection of two UIElements
  1. #1


    brian@sweetapp.com Guest

    Intersection of two UIElements

    Hi,

    I've been looking for the answer to this question for a while.

    I would like to know if two UIElements intersect on the screen. These
    UIElements are in the same container but they have their margins
    specified in different ways (i.e. one is center aligned and one is left
    aligned) and have had various transformations applied to them. So
    calculating if they intersect is reasonably complex.

    Does anyone know of a way to accomplish this. Any help would be hugely
    appreciated.

    Cheers,
    Brian




      My System SpecsSystem Spec

  2. #2


    Marcus Guest

    Re: Intersection of two UIElements

    I dont think I can provide 100% of the answer... but here I go...

    You can ask any visual for a GeneralTranform that tranforms coordinates
    or bounds to another visual's coordinate system...

    You could ask both your UIElements for a tranform to their common
    ancestor and intersects the 2 rects...

    This can be done using the methods:

    Visual.TransformToAncestor()
    Visual.TransformToDescendant()
    Visual.TransformToVisual()

    Unfortunatly... I'm not sure how this applies to UIElements with Layout
    or Render Transforms... You would have to try by yourself.

    You could always test for the 4 corners of your elements, to see if
    they are inside the other element?!?


    brian@sweetapp.com wrote:
    > Hi,
    >
    > I've been looking for the answer to this question for a while.
    >
    > I would like to know if two UIElements intersect on the screen. These
    > UIElements are in the same container but they have their margins
    > specified in different ways (i.e. one is center aligned and one is left
    > aligned) and have had various transformations applied to them. So
    > calculating if they intersect is reasonably complex.
    >
    > Does anyone know of a way to accomplish this. Any help would be hugely
    > appreciated.
    >
    > Cheers,
    > Brian



      My System SpecsSystem Spec

  3. #3


    brian@sweetapp.com Guest

    Re: Intersection of two UIElements

    Marcus wrote:
    > I dont think I can provide 100% of the answer... but here I go...
    >
    > You can ask any visual for a GeneralTranform that tranforms coordinates
    > or bounds to another visual's coordinate system...
    >
    > You could ask both your UIElements for a tranform to their common
    > ancestor and intersects the 2 rects...
    >
    > This can be done using the methods:
    >
    > Visual.TransformToAncestor()
    > Visual.TransformToDescendant()
    > Visual.TransformToVisual()
    >
    > Unfortunatly... I'm not sure how this applies to UIElements with Layout
    > or Render Transforms... You would have to try by yourself.


    These seem to be taken into account. My method to get the
    container-oriented bounds of the ui_element is (Python syntax):

    def get_play_rect(self):
    t = self.ui_element.TransformToVisual(self.play_area)
    return t.TransformBounds(
    Rect(
    0,
    0,
    self.ui_element.ActualWidth,
    self.ui_element.ActualHeight))

    My actual collision detection code (for my pong clone) looks like this:

    if ball.get_play_rect().IntersectsWith(left_paddle.get_play_rect()):
    if ball.dx < 0:
    ball.x_bounce()

    I'll make the complete code available when it is done.

    > You could always test for the 4 corners of your elements, to see if
    > they are inside the other element?!?


    Well, two elements could intersect but not have any corners inside the
    other.

    Thanks very much for your help!

    Cheers,
    Brian


      My System SpecsSystem Spec

Intersection of two UIElements

Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing intersection of overlaying elements Ranj Avalon 2 24 Feb 2006