Ball Physics

Hi guys,I need to implement simple ball(2D) physics which handles gravity, collision detection with other balls, and bouncing with the walls.Number of balls will be 3.Can anyone give some tips how to do it?Cheers,:-)
[265 byte] By [drrpbsa] at [2007-10-3 2:53:37]
# 1

Yea read up on the physics of projectiles.

http://www.glenbrook.k12.il.us/GBSSCI/PHYS/CLASS/vectors/vectoc.html

- Get the equations

- Youll need the weight of the balls

- A value for your virtual gravity

- Make the time scalable

(so events can take more/less time without changing values)

- Make the number of balls variable to start with.

- Make the ball widths variable to start with

- Essentially make everything variable

And for the love of god dont use a different thread for every sprite.

Do a search of the forums for the many other people who have

implemented collision detection and bouncing for balls.

here is a thread from last week:

http://forum.java.sun.com/thread.jspa?threadID=759410

TuringPesta at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 2
You could probably have a thread per sprite if there are only going to be those three sprites.It sure wouldn't scale well though.
paulcwa at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks a lot
drrpbsa at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 4
I'm not using sprites, i'm filling the rect to form a circle. i have to handle it using a single thread.
drrpbsa at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 5
You ARE using sprites. http://en.wikipedia.org/wiki/Sprite_(computer_graphics)In computer graphics, a sprite is a two-dimensional image or animation that is integrated into a larger scene.
TuringPesta at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 6
Hey i dont want to use sprites,can i not do it using filloval
drrpbsa at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 7
Haha, how you draw the sprite has nothing to do with whether its a sprite or not. Sprite just means that its a 2D animation or an object moving around in a scene - it doesnt mean anything about how you implement it.To clear up the confusion, what do you associate with a
TuringPesta at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 8
i thought you r speaking about bitmap sprites, i'm sorry
drrpbsa at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 9
No problem. In this case for example you can create a Ball Sprite class and it would store position, direction (vector), color, size and it would be repsonsible for returning values like current location, momentum, etc.
TuringPesta at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 10
actually i have to give the user an option to toggle gravity,increase or decrease speed of the ball, increase or decrease number of balls(min 1, max 3).How can i toggle gravity?
drrpbsa at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 11
do a handstand?(worry about the rest first and youll realize how to do it)
TuringPesta at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 12
> To clear up the confusion, what do you associate with a sprite?Soda, usually.
doremifasollatidoa at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 13
> How can i toggle gravity?At some point you're going to have a bit of code that calculates the acceleration due to gravity on the ball. After you write that, make it toggle-able by putting it in an if block.
paulcwa at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 14

> At some point you're going to have a bit of code that calculates the acceleration due to gravity on the ball. After you write that, make it toggle-able by putting it in an if block.

Ive never coded this type of app before but from the equations:

Net Force = sum of forces

Gravity will just be a value being subtracted from the velocity.

To toggle gravity, i think you might just be able to make the gravity

value (or A acceleration) 0 instead of introducing a control flow.

Gravity would be negative (acting down)

If g = 0 > 1/2* G * T^2 = 0

y = viy * t + 0.5*g*t2

TuringPesta at 2007-7-14 20:42:39 > top of Java-index,Java Essentials,New To Java...
# 15

That's true; if you allow the user to adjust the accelleration due to gravity then toggling becomes just setting it to zero.

I suppose you could toggle that way even if you're not adjusting gravity on the fly. I tend to avoid things like that because it's mixing semantics in a way, but there's nothing wrong with it.

paulcwa at 2007-7-21 10:01:51 > top of Java-index,Java Essentials,New To Java...
# 16
Thank a lot for suggestions from all of u.I have started implementing it. I'l get back to u guys if i've any problem again.
drrpbsa at 2007-7-21 10:01:51 > top of Java-index,Java Essentials,New To Java...
# 17

I'm using the same logic but with 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, 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

drrpbsa at 2007-7-21 10:01:51 > top of Java-index,Java Essentials,New To Java...