Jump to content
  • Advertisement
Sign in to follow this  
Firetag

dont understand how to test 2d collision in java

This topic is 3052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been working on this simple pong game for a few days and I still cant seem to be able too test for collision acurately or to even get it to work ... heres what I have so far...(note that this is only for the right side of the player)
	private void doCollisionTest(ComPaddle com, Player player){
		int playerleft = player.getSide();
		int playerwidth = player.getWidth();
		if (playerleft>250){playerwidth=-playerwidth;}
		int playerright = playerleft+playerwidth; 
		int playertop = player.getYPos();
		int playerbottom = playertop + player.getheight();
		if(ballx_speed<0){//check if ball is traveling to left if it is
		if(ballleft<=playerright){//check if player and ball are on same y axis
			for(int playerheightseed=0; playerheightseed<player.getheight();playerheightseed++){//start seed to get all of player height on x axis
				for(int ballheightseed=0;ballheightseed<ball_radius*2;ballheightseed++){//start seed to get all of ball height on x axis
					if(balltop-ballheightseed==playertop-playerheightseed){//check to see of ball and player are overlapping at x axis
						this.ballx_speed = -ballx_speed;
					}
				}
			}
		}
		}

	}


[Edited by - jpetrie on February 11, 2010 5:33:05 PM]

Share this post


Link to post
Share on other sites
Advertisement
I think you might be over complicating things.


private final int leftCollision = 0, rightCollision = 640;

//returns true if a collision is detected
public boolean checkCollision() {
if ((ballX - ballRadius) <= leftCollision) { //check for collision on left
if ( ((ballY - ballRadius) <= (paddleY + paddleHeight)) && ((ballY + ballRadius) >= (paddleY - paddleHeight)) ) {
return(true);
}
} else if ((ballX + ballRadius) >= rightCollision) { //check for collision on right
//same thing as above but for the other paddle
}

return(false);
}



Share this post


Link to post
Share on other sites
yeah but when i do it like that doesnt it only check it for 2 points on the object...with the loop it checks each pixel or am i wrong..it seems that if a loop isnt used and the ball has a slop of 0 it is very unlikely a collision will be detected...

Share this post


Link to post
Share on other sites
The method I posted is 100% accurate when your working with a rectangle and a circle. Draw a rectangle in Paint and then draw a circle and move it around to visualize and confirm for yourself.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!