Ball Physics

Hi guys,

i'm implementing ball physics, which includes ball bouncing off the walls and gravity.

I'm using a separate class for Ball.

Ball Class has

class Ball{

int ballx, bally, vx,vy;

int size = 10;

public Ball()

{

this.ballx = 0;

this.bally = 0;

this.vx = 2;

this.vy = 2;

}

void Move(int canvasWidth, canvasHeight)

{

ballx += vx;

bally += vy;

if ((ballx < 0) || (bally + size > canvasWidth ))

{

vx = -vx;

ballx += vx;

}

if ((bally < 0) || (bally + size > canvasHeight)))

{

vy = -vy;

bally += vy;

}

}

void paint(Graphics g)

{

g.fillArc(this.ballx, this.bally,size, size, 0, 360);

}

}

and the canvas class has

class can extends Canvas implements Runnable

{

int numBall;

Ball ball[]

public can()

{

ball = new Ball[3]; // max 3 balls

balls[0] = new Ball();

numBall = 1;

(new Thread(this)).start();

}

public void paint(Graphics g)

{

for(int i = 0; i < numBall; i++){

ball.Move (getWidth(), getHeight());

ball.paint(g);

}

}

public void run()

{

try{

Thread.sleep(20);

}

catch(Exception e){

}

repaint();

}

public void keyPressed(int keyCode) {

int action = getGameAction(keyCode);

switch (action) {

case LEFT:

if (numBalls > 1) {

// decrement the counter

numBall = numBall - 1;

ball[numBall] = null;

}

break;

case RIGHT:

// Increase the number of threads

if (numBall < ball.length) {

ball[numBall] = new Ball();

// increment the counter

numBall = numBall + 1;

}

break;

}

}

}

On pressing right key, number of balls needs to be increased. Its creating the new ball but its taking the same ballx , bally as the previous ball

its the problem with the thread right?

i'm starting the thread in constructor. i need to do it using single thread, please guide me.

Thanks

[2228 byte] By [drrpbsa] at [2007-10-3 3:10:51]
# 1

look at your paint code:

public void paint(Graphics g)

{

for(int i = 0; i < numBall; i++){

ball.Move (getWidth(), getHeight());

ball.paint(g);

}

}

There ther error somewhere ;)

And next time, use the [ code ] tags!

deepspacea at 2007-7-14 21:01:40 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

public void paint(Graphics g)

{

for(int i = 0; i < numBall; i++){

ball.Move (getWidth(), getHeight());

ball.paint(g);

}

}

thank u, i've corrected that. but stil the same issue exists

After pressing right key, i tried to print the x,y values for both the

balls it appears like second ball is following the first one. difference between the both balls x & y will be 2 so i'm able to make out that there are two balls. Similar issue with 3 balls.

Is there any logical error? I'm not able to make out at this moment.

Please help me

drrpbsa at 2007-7-14 21:01:40 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
> thank u, i've corrected that. but stil the same issue> existsHow did you correct that?
deepspacea at 2007-7-14 21:01:40 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4
i've updated it. i'm painting each ball in the for loop ie., i'm using ball.Move and ball.paint .May be some problem with this editor, its not taking the square brackets
drrpbsa at 2007-7-14 21:01:40 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5
I don't seen anything getting better....
deepspacea at 2007-7-14 21:01:40 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
shudnt that be repaint() instead of paint()?
yusraa at 2007-7-14 21:01:40 > top of Java-index,Java Mobility Forums,Java ME Technologies...