views:

34

answers:

2

Hi folks,

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

A: 

Are your controls of a squarish shape? Or can you generate bounding boxes? If so, you could create a System.Windows.Rect struct for each one, and use the Rect.Insersect method to test intersection.

Nikhil
I use Rect structures for collision detection. The problem isn't the detection, the problem is to properly shove them to avoid overlapping.
Aaginor
So do you never want them to collide at all? Do you want them to bounce? Or just stop moving on collision?
Nikhil
I want them to bounce. When they collide because of the movement caused by the force vectors, they should bounce so that they try to "uncollide"
Aaginor
A: 

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.

Hightechrider
the problem with very large force vectors is that they shot the object to elsewhere, making the system very unstable.
Aaginor
Maybe you need a greater damping force: at each step multiply velocity by 0.8, say, or add drag proportional to velocity squared. Without damping or drag it's tough to create a stable system.
Hightechrider
I will probably introduce a cooldown constant, which lowers the force with each step. Additionally, I'll reduce the size of the vectors by dividing it through the length of the biggest vector. So I move the objects in small steps. The objects that collide simply doesn't move.
Aaginor