help with pong collision

I am working on a pong-like game for a project... and i am having some trouble with collision checks... again.

Here is our code so far, the problem is that the ball seems to be stopping completely when it is aligned with a paddle on the Y axis (even if it is at the center of the screen)

import java.applet.*;

import java.awt.*;

import java.util.*;

import java.awt.event.*;

publicclass TestAppletextends Appletimplements Runnable

{

//Paddle dimensions

int PaddleHeight = 50;

int PaddleWidth = 10;

//Other variables

boolean isStop =true;

int[] theKeys =newint[256];//Array with one element representing each key

Random Rand =new Random();

int bounces = 0;

boolean isAI2 =false;

boolean isAI1 =false;

ArrayList <Ball> TheBalls =new ArrayList <Ball>();

Paddle[] ThePaddles =new Paddle[2];

//Player scores

int Score1 = 0;

int Score2 = 0;

private Image dblimage;

private Graphics dbg;

publicvoid init()

{

//Set Background color to black

setBackground(Color.black);

}

publicvoid start()

{

//Define new thread

Thread th =new Thread(this);

//Start the new thread

th.start();

for(int z=0; z<256; z++)

theKeys[z] = 0;//Set all keys to default position (released)

enableEvents(AWTEvent.KEY_EVENT_MASK);

ThePaddles[0]=(new Paddle(true, PaddleHeight, PaddleWidth, 10, this.getWidth(), this.getHeight(),'w','s'));

ThePaddles[1]=(new Paddle(false, PaddleHeight, PaddleWidth, 10, this.getWidth(), this.getHeight(),'8','5'));

TheBalls.add(new Ball(this.getHeight(), this.getWidth()));

TheBalls.get(0).AddPaddle(ThePaddles[0]);

TheBalls.get(0).AddPaddle(ThePaddles[1]);

}

publicvoid run ()

{

//Minimize thread priority

Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

while (true)

{

for(int i=0; i<ThePaddles.length; i++)

{

if(isKeyPress(87))

{

ThePaddles[i].Move(1);

}

elseif(isKeyPress(83))

{

ThePaddles[i].Move(-1);

}

}

if(isKeyPress(32))

{

for(int x=0; x >< TheBalls.size(); x++)

TheBalls.get(x).Start();

isStop =false;

bounces = 0;

}

for(int i=0; i<TheBalls.size(); i++)

{

TheBalls.get(i).Move();

String scored = TheBalls.get(i).IsScore();

if(scored =="Player 1")

{

Score1++;

break;

}

elseif(scored =="Player 2")

{

Score2++;

break;

}

}

repaint();

try

{

Thread.sleep (20);

}

catch (InterruptedException ex)

{

// do nothing

}

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}

}

publicvoid paint(Graphics g)

{

//set color

g.setColor(Color.green);

for(Ball temp : TheBalls)

{

int[] tempfo = temp.BallInfo();

g.fillOval(tempfo[0], tempfo[1], tempfo[2], tempfo[3]);

}

for(Paddle temp: ThePaddles)

{

int[] PadTemp = temp.PaddleData();

g.fillRect(PadTemp[0], PadTemp[1], PadTemp[2], PadTemp[3]);

}

//paint player 1 score

g.drawString("Player 1 score: "+Score1, 10, 50);

//paint player 2 score

g.drawString("Player 2 score: "+Score2, 400, 50);

//paint current bounce count

g.drawString("Bounces: "+bounces, 250, 50);

g.drawString(""+ThePaddles[0].GetUp(), 100, 100);

g.drawString(""+ThePaddles[0].GetDown(), 100, 400);

g.drawString(""+ThePaddles[1].GetUp(), 200, 100);

g.drawString(""+ThePaddles[1].GetDown(), 200, 400);

g.drawString(""+TheBalls.get(0).Collide(), 250, 250);

g.drawLine(ThePaddles[0].getX(), ThePaddles[0].GetTop(), ThePaddles[0].getX()-500, ThePaddles[0].GetTop());

}

publicvoid update (Graphics g)

{

// initialize buffer

if (dblimage ==null)

{

dblimage = createImage (this.getSize().width, this.getSize().height);

dbg = dblimage.getGraphics ();

}

// clear screen in background

dbg.setColor (getBackground ());

dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

// draw elements in background

dbg.setColor (getForeground());

paint (dbg);

// draw image on the screen

g.drawImage (dblimage, 0, 0,this);

}

publicvoid processKeyEvent(KeyEvent ev)

{

int keycode = (ev.getKeyCode()&0xff);

if (ev.getID() == KeyEvent.KEY_PRESSED)

{

theKeys[keycode] = 1;//1 Represents key is pressed

}

elseif (ev.getID() == KeyEvent.KEY_RELEASED)

{

theKeys[keycode] = 0;//0 Represents key is not pressed

}

repaint();

}

//Checks if a given key is pressed

publicboolean isKeyPress(int theKey)

{

return (theKeys[theKey] != 0);

}

//Checks if a given key is released

publicboolean isKeyRel(int theKey)

{

return(theKeys[theKey] == 0);

}

}

class Paddle

{

privateint

XPos,

YPos,

Width,

Height,

Speed,

AppletHeight,

AppletWidth,

Down,

Up;

boolean

LeftSide;

public Paddle(boolean LeftSide,int Height,int Width,int Speed,int AppletWidth,int AppletHeight,char Up,char Down)

{

this.AppletHeight = AppletHeight;

this.AppletWidth = AppletWidth;

YPos = AppletHeight/2;

this.Height = Height;

this.Width = Width;

this.Speed = Speed;

this.LeftSide = LeftSide;

if(LeftSide)

this.XPos = 15;

else

this.XPos = AppletWidth-25;

Character UpChar =new Character(Up);

Character DownChar =new Character(Down);

this.Up = UpChar.charValue();

this.Down = DownChar.charValue();

}

publicvoid Move(int x)

{

if(x > 0)

{

YPos -= Speed;

}

else

YPos += Speed;

}

publicint GetTop()

{

return YPos - Height/2;

}

publicint GetBottom()

{

return YPos + Height/2;

}

publicint GetUp()

{

return Up;

}

publicint GetDown()

{

return Down;

}

publicint getX()

{

return XPos;

}

publicint getY()

{

return YPos;

}

publicint GetHeight()

{

return Height;

}

publicint GetWidth()

{

return Width;

}

publicint[] PaddleData()

{

if(LeftSide)

returnnewint[]{XPos+Width/2, YPos-Height/2, Width, Height};

returnnewint[]{XPos-Width/2, YPos-Height/2, Width, Height};

}

}

class Ball

{

privateint

XPos,

XSpeed,

YPos,

YSpeed,

Radius,

MaxSpeed,

Height,

Width;

privateboolean

IsStill;

private ArrayList <Paddle>

ThePaddles=new ArrayList <Paddle>();

private Random Rand=new Random();

public Ball(int Width,int Height)

{

this.ThePaddles=ThePaddles;

this.Height=Height;

this.Width=Width;

XPos=Width/2;

YPos=Height/2;

IsStill=true;

MaxSpeed=9;

Radius=10;

ChangeAngle();

}

publicvoid AddPaddle(Paddle temp)

{

ThePaddles.add(temp);

}

publicint[] BallInfo()

{

returnnewint[]{XPos-Radius, YPos-Radius, 2*Radius, 2*Radius};

}

publicvoid Start()

{

IsStill =false;

}

publicint getX()

{

return XPos;

}

publicint getY()

{

return YPos;

}

publicint getRad()

{

return Radius;

}

publicvoid RevX()

{

XSpeed *= -1;

}

publicint getXSp()

{

return XSpeed;

}

publicint getYSp()

{

return YSpeed;

}

publicboolean isNeg()

{

if(XSpeed < 0)

returntrue;

returnfalse;

}

//Generates a random trajectory for the ball

publicvoid ChangeAngle()

{

int Num, Opp;

Num= Rand.nextInt(MaxSpeed)+1;

Opp = MaxSpeed-Num;

XSpeed = ((XSpeed>0)?1:-1)*Num;

YSpeed = ((Rand.nextInt(2)==0)?1:-1)*Opp;

}

publicboolean Collide()

{

boolean collid=false;

for(Paddle i: ThePaddles)

{

if(YPos >= i.GetTop() && YPos <= i.GetBottom())

{

if(i.LeftSide)

collid = (((XPos - Radius) <= (i.getX() + i.GetWidth())));

if(!collid)

collid = (((XPos + Radius) >= (i.getX() - i.GetWidth())));

}

}

if(!collid)

collid = ((YPos<=0 && YSpeed < 0) || (YPos>=Height && YSpeed > 0));

return collid;

}

public String IsScore()

{

if(XPos >= Width)

{

XPos = Width/2;

YPos = Height/2;

IsStill =true;

return"Player 1";

}

if(XPos <= 0)

{

XPos = Width/2;

YPos = Height/2;

IsStill =true;

return"Player 2";

}

return"";

}

publicvoid Move()

{

boolean temp =false;

for(int i=0; i<=Math.abs(XSpeed); i++)

{

if((Collide()) && !temp)

{

RevX();

ChangeAngle();

temp =true;

break;

}

XPos = ((XSpeed>0)?XPos+1:XPos-1);

}

temp =false;

for(int i=0; i<=Math.abs(YSpeed); i++)

{

if((Collide()) && !temp)

{

YSpeed *= -1;

temp =true;

break;

}

YPos = ((YSpeed>0)?YPos+1:YPos-1);

}

}

}

[21732 byte] By [omgitsadama] at [2007-11-27 5:08:43]
# 1

public boolean Collide()

{

boolean collid=false;

for(Paddle i: ThePaddles)

{

if(YPos >= i.GetTop() && YPos <= i.GetBottom())

{

if(i.LeftSide)

collid = (((XPos - Radius) <= (i.getX() + i.GetWidth())));

//if(!collid)

else

collid = (((XPos + Radius) >= (i.getX()/* - i.GetWidth()*/)));

crwooda at 2007-7-12 10:28:15 > top of Java-index,Security,Cryptography...
# 2

Thanks alot for your help crwood, but it still does not seem to be working...

Now the collision works perfectly with the right side paddle, but it will not collide at all with the left side paddle. Obviously, this is probably a problem with our "LeftSide" checks, and we looked at the code and realised that the boolean we were trying to access was private... This was puzzling because you would think we would get an access error. Anyway, we changed it to implement an accessor method for the boolean LeftSide, but it still has the same problem.

Any more assistance would be greatly appreciated.

omgitsadama at 2007-7-12 10:28:15 > top of Java-index,Security,Cryptography...