This site requires JavaScript, please enable it in your browser!
Greenfoot back
Pointifix
Pointifix wrote ...

2014/3/21

Ball Intersecting Detection

Pointifix Pointifix

2014/3/21

#
Same Problem as before, but another way: this code changes the color of two intersecting balls, but only works fine if there are 2 balls, at 3 or more the colors are not always changed, just sometimes at someone of the two intersecting balls, anyone got any idea pls :/
List<Ball> checkball = getObjects(Ball.class);
        
for(Ball currentball : checkball)
{
     for(Ball compareball : checkball)
     {
          if(!currentball.equals(compareball))
          {
              double distance = Math.hypot(currentball.getExactX() - compareball.getExactX(),currentball.getExactY() - compareball.getExactY());
                    
              if(distance <= currentball.radius + compareball.radius)
              {
                   currentball.getImage().setColor(Color.red);
                   compareball.getImage().setColor(Color.red);
              }
              else
              {
                   currentball.getImage().setColor(Color.green);
                   compareball.getImage().setColor(Color.green);
              }
              currentball.getImage().fillOval(0,0,currentball.radius * 2,currentball.radius * 2);
              compareball.getImage().fillOval(0,0,compareball.radius * 2,compareball.radius * 2);
          }
     }
}
Pointifix Pointifix

2014/3/21

#
Fail cleared, found solution in another forum ;) ------------------------------------------------------Closed------------------------------------------------------------------
You need to login to post a reply.