Java Pong collision detection -
hello trying make pong game java , works great except collision part. firstly used bounds class , intersects method solve collision problem method works collision case shown on picture drew.
so wrote new code myself instead of using bounds class, , created method side collision detection again having hard time solve collision detection top , bottom of paddle.
public boolean collideright(ball ball){ if(ball.getlayoutx()+ball.getradius()>=player.getlayoutx()&&(ball.getlayouty()+ball.getradius()>=player.getlayouty()&&ball.getlayouty()-ball.getradius()<=player.getlayouty()+height)){ return true; } else{ return false; } }
and method made top , bottom collision detection.
public boolean colliderightupside(ball ball){ if((ball.getlayoutx()+ball.getradius()>=player.getlayoutx()&&ball.getlayoutx()-ball.getradius()<=player.getlayoutx()+width)&&(ball.getlayouty()+ball.getradius()>=player.getlayouty()&&ball.getlayouty()-ball.getradius()<=player.getlayouty()+height)){ return true; } else{ return false; } }
can please me.
try include both if statements in 1 function looks this:
public boolean collidefunction(ball ball){ if(ball.getlayoutx()+ball.getradius()>=player.getlayoutx()&&(ball.getlayouty()+ball.getradius()>=player.getlayouty()&&ball.getlayouty()-ball.getradius()<=player.getlayouty()+height)){ return true; } else if((ball.getlayoutx()+ball.getradius()>=player.getlayoutx()&&ball.getlayoutx()-ball.getradius()<=player.getlayoutx()+width)&&(ball.getlayouty()+ball.getradius()>=player.getlayouty()&&ball.getlayouty()-ball.getradius()<=player.getlayouty()+height)){ return true; } else{ return false; } }
Comments
Post a Comment