Tuesday, 14 June 2011

Collisions ( Part I )

Firstly, solid mechanics are very complex, and, depending on many elements such as shapes of elements, environmental factors, etc, differents laws are used to know what happen.
So, the code made for the platform limit mechanics to what is needed for croquet application : a ball which collides other balls and fixed elements.

In the code, each Element is now associated with a Shape object which Circle and Rectangle class heritate (some Elements like hoops may, however, be assiciated with more than one).
All the collisions aspects will be programmed into Shape objects.

Collisions between elements

This topic will be seperated into 3 part, which contain the needed elements to manage collisions :
  • Collision detection
  • Find the impact point and position the element on contact with the other
  • Change directions and speeds of the elements using mechanics

  • Collision detection

One of the model used by physic systems is to associate each element with a bounding box. The objective is to detect collisions between these bounding box. A bounding box can be a circle, a square rectangle, etc.. or even a more complex polygon. However, for each pair of shapes, it would be needed to program it.
Here again, I limited the platform to the needs of croquet : balls, peg, and extremities of hoops will be modelled as circle. To play the game, we'll also need to detect collision between ball's circle and hoop's rectangle.

Circle & Circle

You only need to compare the distance between centers with the sum of radius : if the distance is lower, there is collision.

Circle & Straight Rectangle

When rectangles are straight (this will be the case for hoops), the problem is not too complicated : two points have to be checked :

  1. If the circle's centre lies inside the rectangle
  2. If one of the edges of the rectangle intersects the circle

    • Find the impact point and position the element on contact with the other
     Here again, the platform is limited to collision between circles (we actually don't care about impact points when balls interesect hoop's rectangle, as it's not a "physic collision").

    Basically, the code is based on these geometric formulas :
    http://amrita.vlab.co.in/?sub=1&brch=68&sim=197&cnt=1

    As I use angles to model direction, instead of vectors, some adaptation was made.


    On this figure, you know the position of the source element before collision (St), the position of target (T).
    So, you can determine the angle a. Knowing the direction angle of source element gives you the angle b.
    Then, the difference of the angles and trigonemtry into XYZ triangles gives d value.
    To finish, you can find alpha into WYZ triangle, and deduce the value of f with the relation b-alpha .
    With f, you can then know the position of the element after collision.
    Rotating f of 90° also gives you the tangent angle I'll use in next part.

    Actually, many particular cases make the problem be more complicated.

    No comments:

    Post a Comment