Right, for my first Java game I've been making a Pong clone (inspired, right?) and it's working absolutely fine except for one niggling issue I can't work out how to solve: When the bat moves into the ball the collision detection goes haywire, bouncing the ball around inside/on the edge of the bat, and eventually breaking free somewhere near the other end of the bat going in a pretty much random direction.
I'm not really sure how to solve this. Here's what I think are the relevant part s of the code:
In main Play class:
Code:
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
//detecting collisions on bat 1 (left)
if(bat1.intersects(ball)){
// Bouncing off of top face OR bouncing off of bottom face
if( (ball.getMaxY() < bat1.getMinY() ) || (ball.getMinY() > bat1.getMaxY() ) ){
ball.setyVelocity(-ball.getyVelocity());
} else if(bat1.isDown()){
ball.setxVelocity(-(ball.getxVelocity() -1));
ball.setyVelocity(ball.getyVelocity() +1);
} else if(bat1.isUp()){
ball.setxVelocity(-(ball.getxVelocity() -1));
ball.setyVelocity(ball.getyVelocity() -1);
} else ball.setxVelocity(-(ball.getxVelocity() -1));
Code:
if(input.isKeyDown(Input.KEY_W) ){
bat1.setUp(true);
} else bat1.setUp(false);
if(input.isKeyDown(Input.KEY_S)){
bat1.setDown(true);
} else bat1.setDown(false);
The bat class is an extension of Rectangle, and ball is extending Circle. The collision is all based on the intersects() method.
Thanks for any help you can provide
