10 Replies - 790 Views - Last Post: 21 June 2012 - 04:54 AM Rate Topic: -----

#1 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Pong help.

Posted 19 June 2012 - 07:09 PM

I'm working on some pong game, and I'm having trouble with the player pong collision with the ball.... For some reason, it is only detecting the collision if the paddle if as the top corner.....

Here is the code for this method plus the rest of the class for reference:

package TheGame;

import java.util.Random;

import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.*;

public class Methods {
	
	Random rand = new Random();
	static int oppX = 490;
	static int oppY = 0;
	static int oppW = 50;
	static int oppH = 360;
	
	static float ballX = 455;
	static float ballY = 0;
	static float ballW = 25;
	static float ballH = 25;
	static int vx; //x velocity
	static int vy; //y velocity
	
	static int py; //top of paddle 
	static int opy = py + 55; //bottom of paddle
	static int px = 20; //right side of paddle
	static int opx = 55; //left side of paddle
	
	public void drawField(Graphics g){ //draw background and center line
		g.setBackground(Color.black);
		g.setColor(Color.yellow);
		g.fillRect(307, 0, 26, 360);
	}
	
	public void drawOpposition(Graphics g){ //draw wall
		g.setColor(Color.cyan);
		g.fillRect(oppX, oppY, oppW, oppH);
	}
	
	public void drawBall(Graphics g){ //draw ball
		g.setColor(Color.red);
		g.fillRect(ballX, ballY, ballW, ballH);
	}
	
	public void drawPlayer(Graphics g){ //draw player paddle 
		g.setColor(Color.cyan);
		g.fillRect(px, py, 25, 50);
	}
	
	public void setVelocitys(){
		if(ballX > px && ballX < opx && (ballY-12) > (py-5) && (ballY-12) < opy ){ //collides with paddle
			vx = rand.nextInt(5);
		}else if(ballX >= 455){//exit off right side
			vx = -(rand.nextInt(5));
		}else if(ballX < 0){ // exit off left side
			ballX = 455;
		}
		
		if(ballY <= 0){ //exit off top
			vy = rand.nextInt(5);
		}else if(ballY >= 335){ //exit off bottom 
			vy = -(rand.nextInt(5));
		}
	}
	
	public void moveBall(GameContainer gc){ //move the ball
		setVelocitys();
		ballX += vx;
		ballY += vy;
		checkPlayer(gc);
	}
	
	public void checkPlayer(GameContainer gc){ //check player movement
		Input input = gc.getInput();
		
		if(input.isKeyDown(Input.KEY_DOWN)){
			py = py + 5;
		}
		if(input.isKeyDown(Input.KEY_UP)){
			py = py - 5;
		}
		if(py < 0){
			py = 0;
		}
		if(py > 310){
			py = 310;
		}
	}
}



(Also, I am using the slick and lwjgl libraries.)

Thanks for any suggestions :D

Here is where the problem is I believe.
if(ballX > px && ballX < opx && (ballY-12) > (py-5) && (ballY-12) < opy ){ //collides with paddle


This post has been edited by Benzoate: 19 June 2012 - 07:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pong help.

#2 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: Pong help.

Posted 19 June 2012 - 09:42 PM

Have you considered using java's built-in shape classes to handle the collision detection for you instead?
Was This Post Helpful? 0
  • +
  • -

#3 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: Pong help.

Posted 19 June 2012 - 09:47 PM

I didn't even know that such a thing existed... How do I use it?
Was This Post Helpful? 0
  • +
  • -

#4 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: Pong help.

Posted 19 June 2012 - 09:52 PM

May find http://docs.oracle.c...ge-summary.html

Useful, though it may be quicker for you to figure out what is wrong exactly with your bounds there, I would check but I'm tired right now.

Worst comes to worst you can write a quick-and-dirty raycasting method to get collision with ball.


Or see here just noticed this in related topics.

http://www.dreaminco...t-game-of-pong/

Can probably take a peak at how collision was done there.

This post has been edited by Ghlavac: 19 June 2012 - 09:54 PM

Was This Post Helpful? 1
  • +
  • -

#5 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: Pong help.

Posted 19 June 2012 - 09:58 PM

all the first link did is blow my mind haha and the second I've looked over before but it wasn't much help... but I'll re-read it.

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#6 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: Pong help.

Posted 19 June 2012 - 10:14 PM

thanks, I think I got the problem fixed but I'm not positive...
Was This Post Helpful? 0
  • +
  • -

#7 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: Pong help.

Posted 20 June 2012 - 07:46 AM

nope... I tried to make it into a boolean but for some reason the code won't work...
	public void setVelocitys(boolean hitpaddle){
		if(hitpaddle = true){ //collides with paddle
			vx = .3f;
			hitpaddle = false;
		}else if(ballX >= 455){//exit off right side
			vx = -.3f;
		}else if(ballX < 0){ // exit off left side
			ballX = 455;
		}
		
		if(ballY <= 0){ //exit off top
			vy = .3f;
		}else if(ballY >= 335){ //exit off bottom 
			vy = -.3f;
		}
	}
	
	public boolean hitpaddle;
	public void checkCollision(boolean hitpaddle){
		hitpaddle = false;
		if(py <= ballY && (py + 50) >= ballY && ballX == (px + 25)){
			hitpaddle = true;
		}
	}



that's the code.
any suggestions?
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Pong help.

Posted 20 June 2012 - 09:49 AM

When you post code that you need help with, especially just a snippet that can't be run, please provide a better description of the problem than, "for some reason the code won't work..." What should it be doing that it's not, what's it doing that it shouldn't (provide input and output examples when possible), and include any error messages and stack traces exactly as they appear at your end, copied and pasted.

If you intended the snippet posted above to be an update to your previously posted code, then post the whole program or class.

This post has been edited by GregBrannon: 20 June 2012 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#9 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • View blog
  • Posts: 3,121
  • Joined: 05-April 11

Re: Pong help.

Posted 20 June 2012 - 10:00 AM

You made, what I would call, a beginner mistake. Instead of just checking the boolean value, you first assign it to true and then evalute it

if(hitpaddle = true){ //collides with paddle



You can just write the boolean value straight in there, and you will never make the same mistake again :)

if(hitpaddle){ //collides with paddle


Was This Post Helpful? 0
  • +
  • -

#10 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: Pong help.

Posted 20 June 2012 - 01:59 PM

@GregBrannon
Sorry I was trying to post this while my mother and brother were rushing me so I didn't have long

@CasiOo
Thank you, I fixed that so now it is
	public void setVelocitys(){
		if(hitpaddle){ //collides with paddle
			vx = .5f;
		}else if(ballX >= 455){//exit off right side
			vx = -.5f;
		}else if(ballX < 0){ // exit off left side
			ballX = 455;
		}
		
		if(ballY <= 0){ //exit off top
			vy = .5f;
		}else if(ballY >= 335){ //exit off bottom 
			vy = -.5f;
		}
	}
	
	public boolean hitpaddle;
	public void checkCollision(){
		if(py <= ballY && (py + 50) >= ballY && ballX == (px + 25)){
			hitpaddle = true;
		}
	}



But I am still having the same problem, the ball just goes straight through my paddle. :(

Here is the whole class:
package TheGame;

import java.util.Random;

import org.newdawn.slick.*;

public class Methods {
	
	Random rand = new Random();
	static int oppX = 490;
	static int oppY = 0;
	static int oppW = 50;
	static int oppH = 360;
	
	static float ballX = 455;
	static float ballY = 0;
	static float ballW = 25;
	static float ballH = 25;
	static float vx; //x velocity
	static float vy; //y velocity
	
	static int py; //top of paddle 
	static int opy = py + 55; //bottom of paddle
	static int px = 20; //right side of paddle
	static int opx = 55; //left side of paddle
	
	public void drawField(Graphics g){ //draw background and center line
		g.setBackground(Color.black);
		g.setColor(Color.yellow);
		g.fillRect(307, 0, 26, 360);
	}
	
	public void drawOpposition(Graphics g){ //draw wall
		g.setColor(Color.cyan);
		g.fillRect(oppX, oppY, oppW, oppH);
	}
	
	public void drawBall(Graphics g){ //draw ball
		g.setColor(Color.red);
		g.fillRect(ballX, ballY, ballW, ballH);
	}
	
	public void drawPlayer(Graphics g){ //draw player paddle 
		g.setColor(Color.cyan);
		g.fillRect(px, py, 25, 50);
	}
	
	public void setVelocitys(){
		if(hitpaddle){ //collides with paddle
			vx = .5f;
		}else if(ballX >= 455){//exit off right side
			vx = -.5f;
		}else if(ballX < 0){ // exit off left side
			ballX = 455;
		}
		
		if(ballY <= 0){ //exit off top
			vy = .5f;
		}else if(ballY >= 335){ //exit off bottom 
			vy = -.5f;
		}
	}
	
	public boolean hitpaddle;
	public void checkCollision(){
		if(py <= ballY && (py + 50) >= ballY && ballX == (px + 25)){
			hitpaddle = true;
		}
	}
	
	public void moveBall(GameContainer gc){ //move the ball
		setVelocitys();
		ballX += vx;
		ballY += vy;
		checkPlayer(gc);
	}
	
	public void checkPlayer(GameContainer gc){ //check player movement
		Input input = gc.getInput();
		
		if(input.isKeyDown(Input.KEY_DOWN)){
			py = py + 5;
		}
		if(input.isKeyDown(Input.KEY_UP)){
			py = py - 5;
		}
		if(py < 0){
			py = 0;
		}
		if(py > 310){
			py = 310;
		}
	}
}



I'm pretty sure that the problem is right here:
public void checkCollision(){
		if(py <= ballY && (py + 50) >= ballY && ballX == (px + 25)){
			hitpaddle = true;
		}
	}



Specifically:
if(py <= ballY && (py + 50) >= ballY && ballX == (px + 25))


I'm not sure what's wrong... what should I do?
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Pong help.

Posted 21 June 2012 - 04:54 AM

I'm showing errors earlier in the code you've posted and then in other places. Are you sure it's getting to the line you think is suspect? Are you getting error messages/stack traces when you compile and run or before that? Check your console output or view when you compile/run to see the errors.

One of the errors I see is that the g.fillRect() method does not accept float values as arguments. Aren't you getting an error there?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1