Search code examples
silverlightgeometrycollision

Collision Handling for moving rectangles


in my Silverlight 3 application, I display a tree. A self made user control is used for the treenodes, the LineArrow object for the connections. After initial displaying the tree, I want the nodes to move by the following "physical properties"

  • there is a gravitational force, that pulls the node down
  • there is a force vector to its parent
  • it's children draws it to their middle

Naturally, my User Controls will overlap soon. But I do not want them to overlap. In physics terms, I want them to be solid objects and enforce the physical rule that no 2 objects can inhabit the same space.

Any suggestions how to tackle this problem? I do not want to use a physics engine like farseer for this, because the described part is the only physics to be used within my project.

Thanks in advance,
Frank


Solution

  • You can model them as 'nearly solid' objects simply by adding a very large force between them whenever they overlap. Calculate the center to center vector, normalize it, maybe multiply by the overlap and then apply that as a force to each object.

    Even if you don't use the full Farseer library you may still find some useful classes in it like Vector2.