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?