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.
IndexProgramming Questions & HelpSyntax Questions › I am new at programing. This is about conditionals
Page Index Toggle Pages: 1
I am new at programing. This is about conditionals (Read 537 times)
I am new at programing. This is about conditionals
Aug 17th, 2009, 12:22am
 
I created code of a ball bouncing off 4 walls. When I mousePress; the ball accelerates.

The problem is, the ball doesn't continue and accelerate in the same direction. It seems to slow down and change direction.

I think it is because the variable "acceleration" adds a positive number to the negative variable "speed", leading to some undesirable movements.

Can anyone explain to me what is going on and how to straighten the trajectory? Thanks.

Here is the code.

boolean on = false;
float x = 16;
float y = 16;
float xspeed = 3;
float yspeed = 2;
float acceleration = 0.2;

void setup() {
 size(200,200);
}

void draw() {
background(255);

fill(150);
stroke(0);
ellipse(x,y,32,32);

x = x + xspeed;
y = y + yspeed;

if (on) {
 xspeed = xspeed + acceleration; yspeed = yspeed + acceleration;
}

if (mousePressed){
on = !on;
}

if ((x > width - 16) || (x < 16)) {
 xspeed = xspeed * -1;
}

if ((y > height - 16) || (y < 16)) {
 yspeed = yspeed * -1;
}
}
Re: I am new at programing. This is about conditionals
Reply #1 - Aug 17th, 2009, 1:57am
 
your acceleration is always positive (.02), your velocity might not be (after hitting a wall). adding a +ve acceleration to a -ve velocity explains what you're seeing.

it might be better to have an acceleration slightly > 1 and multiple by it rather than add it.

(and it's a velocity rather than a speed as speed is a scalar but velocity is signed)
Re: I am new at programing. This is about conditionals
Reply #2 - Aug 17th, 2009, 11:25am
 
Thanks. That improved the result.

Also I will use velocity instead of speed when defining direction.
Page Index Toggle Pages: 1