Slick Forums

Discuss the Slick 2D Library
It is currently Tue Jun 18, 2013 6:57 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Jul 31, 2012 10:41 pm 
Offline

Joined: Tue Jul 31, 2012 10:31 pm
Posts: 9
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 :)


Top
 Profile  
 
PostPosted: Wed Aug 08, 2012 1:28 am 
Offline

Joined: Tue Jul 31, 2012 10:31 pm
Posts: 9
Some further information to this: I've realised the problem only arises when the bat is in motion, specifically when the bat and ball move into the same place at the same time, so that the two of them overlap. Does anyone have any suggestions how I might solve this? Thanks!


Top
 Profile  
 
PostPosted: Mon Aug 13, 2012 7:08 pm 
Offline

Joined: Thu Aug 09, 2012 8:42 pm
Posts: 3
I'm not sure what exactly would cause the behavior you describe, but you may be able to fix the problem by setting the ball's position such that it no longer collides with the bat after you detect a collision.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group