Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ball Collision: Balls Stick Together

I followed the example at http://www.emanueleferonato.com/2007/08/19/managing-ball-vs-ball-collision-with-flash/ to create ball collision on Android,
my code was organized as follows:

int N = 6; // number of balls
for (int i=0; i<N; i++) {
    // move ball
    // as in line 10~39

    // collision detection
    for(int j=i+1; j<N; j++) {
        // as in line 66~77
    } // end for
} // end for

However, sometimes my balls may stick together (overlap).
The same situation didn't happen in the example.
Why?
Do I need to start a thread for each ball?

like image 682
user538565 Avatar asked Jun 20 '11 01:06

user538565


People also ask

What collision sticks together?

Hit and Stick: Inelastic Collision A common event is the collision of a moving object with another object in which both of them stick together after the collision. Such a collision is called inelastic because there is no bounce. Any collision in which kinetic energy is lost is inelastic.

What happens when two balls with same mass collide?

Since the two balls have equal masses, they will also experience equal accelerations. In a collision, there is a force on both objects that causes an acceleration of both objects; the forces are equal in magnitude and opposite in direction.

What type of collision is two balls colliding?

Elastic collisions occur when the colliding objects bounce off of each other. This typically occurs when you have colliding objects which are very hard or bouncy.

Which type of collision has objects that start off together but move apart?

An elastic collision occurs when the two objects "bounce" apart when they collide. Two rubber balls are a good example. In an elastic collision, both momentum and kinetic energy are conserved. Almost no energy is lost to sound, heat, or deformation.


2 Answers

This problem arises when two balls collide with sufficient speed that they cannot fully separate on the subsequent action loop. I've found the best way to handle this is to normalize the distance between them to (r1 + r2) where r is the radius of a ball whenever a collision is detected. This ensures that they will separate on the subsequent action loop, but does have some potential for causing extra collisions if there are a lot of balls in a very tight space.

like image 122
Joshua Sullivan Avatar answered Sep 23 '22 17:09

Joshua Sullivan


It is probably the

ball.xspeed = Math.random()*8-4;
ball.yspeed = Math.random()*8-4;

These generate floating point xspeed and yspeeds. The rounded/float/ceil values of which are used to draw in the screen. I think when the collision detection routine run and checks for a collision it cannot find one when the balls are only a very small distances apart in calculation, but when the speed is much higher, the next step will push the balls into their boundaries. Once they are inside each other the function manage_bounce(ball, ball2) will be invoked on every increment, and try to adjust the bounce direction, but only to find another collision.

I think the main problem is the big increment in the coordinated due to big value of speed, but a small distance between the two balls, which push them inside each other, mode than the floating point calculations.

During calculation when you have detected collision you should make sure at that point that they are not inside each other, and if yes then take them out and reposition. OR you might consider a speed increment IF the two balls are more pixels apart than the added distance, OR something like this

if the balls are `dn` pixels apart 
  if the speed in direction `n` is `n_speed`
     then
       if `dn` < `n_speed`
         `n = n + dn`
       else
          `n = n + n_speed`
       endif
  endif
endif
like image 31
phoxis Avatar answered Sep 23 '22 17:09

phoxis