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
> 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
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.
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