We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
stupid bounce (Read 459 times)
stupid bounce
Apr 8th, 2009, 2:35pm
 
hei guys
i'm trying a simple (too simple and stupid) code, bouncing a ball around screen


bounce founction runs correctly.
problem is if i write ball.update() BEFORE gravity and friction ball no moves.
if i write AFTER.. let's ok, runs correctly

WHY?!?!?!?
in update i add.. so if it's zero for first frame no moveing, but second frame i add += gravity.. so i don't understand


i have this in draw:
Code:

// ball.update(); here desn't move.

ball.vy += gravity;

ball.vx *= friction;
ball.vy *= friction;

ball.update(); //here runs ok
checkBounds(ball);

// .....

private void checkBounds(Ball n) {
if(n.x + n.radius > WIDTH) {
n.x = WIDTH - n.radius;
n.vx *= - n.bounce;
}
if(n.x - n.radius < 0) {
n.x = n.radius;
n.vx *= - n.bounce;
}
if(n.y + n.radius > HEIGHT) {
n.y = HEIGHT - n.radius;
n.vy *= - n.bounce;
}
if(n.y - n.radius < 0) {
n.y = n.radius;
n.vy *= - n.bounce;
}
}



and this is Ball class:
Code:
public class Ball {

public int radius;

public float x,
y,
vx = 0,
vy = 0,
ax = 0,
ay = 0;

public int color;
public double bounce = 0.6;

public void update() {
vx += ax;
vy += ay;
x += vx;
y += vy;
}


}

                 
Re: stupid bounce
Reply #1 - Apr 10th, 2009, 8:36am
 
Difficult to be sure why the problem occurs without seeing the full code, I tried recreating the problem and created the following sketch
Code:

Ball ball = new Ball();;
float gravity = 0.06;
float friction = 0.995;

void setup(){
size(400,400);
ball.x = width / 5;
ball.y = height / 2;
ball.vx = 4;
ball.vy = -6;
ball.radius = 30;
ball.col = color(20,255,20);
ball.bounce = 0.8;

}

void draw(){

background(250);

//ball.update(); //here runs ok as well

ball.vy += gravity;

ball.vx *= friction;
ball.vy *= friction;

ball.update(); //here runs ok
checkBounds(ball);

ball.display();

}

private void checkBounds(Ball n) {
if(n.x + n.radius > width) {
n.x = width - n.radius;
n.vx *= - n.bounce;
}
if(n.x - n.radius < 0) {
n.x = n.radius;
n.vx *= - n.bounce;
}
if(n.y + n.radius > height) {
n.y = height - n.radius;
n.vy *= - n.bounce;
}
if(n.y - n.radius < 0) {
n.y = n.radius;
n.vy *= - n.bounce;
}
}

public class Ball {

public int radius;

public float x,
y,
vx = 0,
vy = 0,
ax = 0,
ay = 0;

public int col;
public double bounce = 0.6;

public void update() {
vx += ax;
vy += ay;
x += vx;
y += vy;
}

public void display(){
noStroke();
fill(col);
ellipse(x,y,radius,radius);
}
}


I did notice in your checkBounds method that you were using WIDTH and HEIGHT rather than width and height. The uppercase version are Processing constants and have the numeric value 1 and 2. I suspect that this was part of the problem because depending on the values you use for gravity, friction etc the ball.x and ball.y values will be different when checkBounds is called for the first time depending  on the position of the update call.

Hope this helps

Page Index Toggle Pages: 1