java - Bouncing Balls in ArrayList Need to Detect Collisions -
hi making bouncingball program balls bounce off walls , off each other. walls fine, collisions problem. storing balls in arraylist can move given number using each loop.
i have been unable implement collisions , have looked lot. closest can there lots of random collisions when there shouldn't be.
latest attempt: physics class detect collisions , balls run off screen..
public class physics {
public static boolean collision(bouncingball b, arraylist<bouncingball> list){ //boolean collide = false; (int = 0; < list.size(); i++){ if(b.getxposition()== list.get(i).getxposition()){ return true; } } return false; }
the bounce method itself(in class balldemo):
ball.draw(); //above here code make balls , add arraylist } // make them bounce boolean finished = false; while (!finished) { mycanvas.wait(50); (bouncingball ball: balls){ if(physics.collision(ball, balls)){ collisioncount(ball); } //if ball out of x bounds bounce else if(ball.getxposition()>=850 || ball.getxposition()<0){ ball.collision(); } //if ball out of y bounds bounce else if(ball.getyposition()>=500 || ball.getyposition()<0){ ball.ycollision(); } ball.move(); } } }
just note:i aware of loop having conflicts due comparing first ball have tried starting @ 1 , still doesn't work.
since comparing each ball whole list, collide itself. need add check in collision see if it's being compared itself. like
if (b == list.get(i)) { continue; }
Comments
Post a Comment