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 › bad random bad code
Page Index Toggle Pages: 1
bad random? bad code? (Read 573 times)
bad random? bad code?
Feb 18th, 2009, 1:08am
 
hi guys

i'm trying to perform a very simple easing code:
pushing a ball easing around screen.
easing is calculated with targetX and targetY and each frame update with velocity propotional to the distance from the target.. all from manuals : p

but.. there is a problem.
ball stay only in bottom half part of screen, compile to belive!!

this is my code:
Code:

Ball[] balls;

void setup() {

size(600,600);
smooth();
noStroke();
frameRate(12);

balls = new Ball[1];
for(int i=0;i<balls.length;i++) {
balls[i] = new Ball( );
}
}

void draw() {

background(255);

for (int i=0; i<balls.length; i++) {
balls[i].update();
balls[i].draw();
}

}

class Ball {

private float easing = 0.1;
private float targetX, targetY;
private int radius = 5;

public float x, y;


Ball() {
x = random(width);
y = random(height);
targetX = random(width);
targetY = random(height);
}

public void update() {
ease();
}

private void ease() {
float dx,dy;
float vx, vy;

dx = targetX - x;
dy = targetY - y;

vx = dx*easing;
vy = dy*easing;

x += vx;
y += vy;

if ( (targetX - x < 0.1 )) {
targetX = random(width);
println( "new target x: "+ targetX);
}
if (targetY - y < 0.1) {
targetY = random(height);
println("new target y:"+ targetY);
}
}


public void draw() {
fill(204, 102, 0);
ellipse(x,y, radius, radius);
}

}



why? is random bad written?
Re: bad random? bad code?
Reply #1 - Feb 18th, 2009, 1:40am
 
at the end of your Ball.ease() method, add abs() within the if statements to make it properly gauge distance from pos or neg direction:


if ( (abs(targetX - x) < 0.1 ))  {
targetX = random(width);
println( "new target x: "+ targetX);
}
if (abs(targetY - y) < 0.1) {
targetY = random(height);
println("new target y:"+ targetY);
}

Re: bad random? bad code?
Reply #2 - Feb 18th, 2009, 1:46am
 
random() is supposed to take two parameters, the lower and upper bounds of the ranges you want to generate numbers in.

If you replace random(height) with random(0, height) and the same for width then it starts working just fine.
Re: bad random? bad code?
Reply #3 - Feb 18th, 2009, 9:53am
 
cloister wrote on Feb 18th, 2009, 1:46am:
random() is supposed to take two parameters[...]If you replace random(height) with random(0, height)[...]

If that was the case, there would be a compilation error!
The reference manual says: "The function call random(5) returns values between 0 and 5 (starting at zero, up to but not including 5)."
So random(height) is the same as random(0, height).
Re: bad random? bad code?
Reply #4 - Feb 18th, 2009, 6:53pm
 
DanI's explanation is a better root-cause analysis than mine, but for whatever reason, changing the random() calls in the way I suggested also works.  Try it out if you don't believe me, I did...
Re: bad random? bad code?
Reply #5 - Feb 18th, 2009, 9:35pm
 
@cloiste: well, maybe it's true, a bit better, but with my code if you get framerate bigger for move ball faster and you do a lot of test it's move more or less in half bottom-right part of screen, mainly. also with random(0,width)..


but i made a discovery!
if i write only one if and no 2 separate if statement
the random result increase a lot:

Code:

if ( (targetX - x < 0.1 ) && (targetY - y < 0.1) ) {
targetX = random(width);
println( "new target x: "+ targetX);

targetY = random(height);
println("new target y:"+ targetY);
}


why?????
easing target is separate in two direction, calculating in separate step x and y motion component..

Re: bad random? bad code?
Reply #6 - Feb 18th, 2009, 9:38pm
 
ohu ohu ohu!!
resolved, i just tried abs() in if statement and it work properly.
but.. why??

sorry for stupid thing but i'm not a good programmer and with motion and geometry i have to learn soo much!
Re: bad random? bad code?
Reply #7 - Feb 18th, 2009, 11:54pm
 
Abs() helps because the _difference_ between two values is not the same thing as the _distance_ between two locations represented by those values.

Differences--one thing minus something else--can be positive or negative.  Distance, as a concept, is never negative.

Your original code was confusing the two concepts.

Consider: if your ball is at position x=10 and the target is at x=100, then targetX - ballX = 90.  But if the target is at 10 and the ball is at 100, then targetX - ballX is -90.

But the _distance_ between the two is +90 in both cases.  Abs() always gets you a positive result, so it is useful for cases like this.

Put another way: you can live 5 kilometers away from your girlfriend's house, but you cannot live -5 kilometers away from her house.  Distance is always positive.
Re: bad random? bad code?
Reply #8 - Feb 19th, 2009, 12:17am
 
uhh oki.
and so my if statement was < 0.1

in case of difference +90 no problem
in case of -90 the ease was broken _before_ achive target..
so the target in top of screen was overtaken by bottom ones.

ok difference and distance are now very clear concept
thanks a lot : D
Page Index Toggle Pages: 1