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 › problem with int() and gravity
Page Index Toggle Pages: 1
problem with int() and gravity (Read 389 times)
problem with int() and gravity
May 10th, 2009, 4:31pm
 
Hello  im having a problem, ive made a program that creates a matrix of dots, it has 2  functions that are controled with keys: animation and gravity.  when i press the animation  key the dots become animated, the thing is that i need that when i press another key the dots return to its original position. For making this task im using gravity,  each dot has as center of gravity the original position. when i begin the program the gravity is turned off, then i turn on the animation key and the dots begin to move, when i want that my dots return to its original place i press the gravity key. The program is working but is not perfect:  when the gravity is turned on and each dot is at the same position of the its original position the gravity stops. The problem is the animation algorithm  uses a lot of floating point values, so i need to make an int() to the x and y values to compare with the original position of the same dot , in the process of converting the floating point to int there is a little change so the gravity doesnt stop when the dot is in the same position of its initial position but there is a diference of one pixel,  Do anybody have an idea of how can i fix this?


many thanks


pun

here is an example : try "q" key to begin animation and then "w" key to gravity.

Code:
lineas[][] rayas = new lineas[30][30];
float boton_uno = 0;
float boton_dos = 0;
float distancia_entero;


void setup( ) {

size(600, 600);

for (int i=0 ; i < 30 ; i ++) {
for (int j=0 ; j < 30 ; j ++) {
rayas[i][j] = new lineas( 42 * i , 22 * j, j);

}
}
}


void draw( ) {
background(255,255,255);
ellipse( 240, 240 , 12, 12);
for (int i=0 ; i < 30 ; i ++) {
for (int j=0 ; j < 30 ; j ++) {

rayas[i][j].dibuja();

if ( i == 1 && j == 1 ){
distancia_entero = int(rayas[i][j].ex) + int(rayas[i][j].x);

}

}
}

if (boton_uno == 1){

mueve ();
}

if (boton_dos == 1){

gravedad ();
}

if (boton_dos == 0){

resetea_gravedad ();
}

}





void mueve() {

for (int i=0 ; i < 30 ; i ++) {
for (int j=0 ; j < 30 ; j ++) {

if (boton_dos == 1) {
if (int(rayas[i][j].ex) + int(rayas[i][j].x) == (rayas[i][j].valor_inicial_x ) && int(rayas[i][j].yz + rayas[i][j].y )== int(rayas[i][j].valor_inicial_y)){

rayas[i][j].switcher = 1;

}
else {
rayas[i][j].switcher = 0;


}}

rayas[i][j].mover();

}
}
}


void gravedad() {

for (int i=0 ; i < 30 ; i ++) {
for (int j=0 ; j < 30 ; j ++) {

rayas[i][j].calcular_distancia_gravedad();

}
}
}

void resetea_gravedad() {

for (int i=0 ; i < 30 ; i ++) {
for (int j=0 ; j < 30 ; j ++) {

rayas[i][j].delta_x = 0;
rayas[i][j].delta_y = 0;


}
}
}


void keyPressed() {

if (key == 'q' || key == 'Q') {
boton_uno = 1;

}
if (key == 'a' || key == 'A') {
boton_uno = 0;

}

if (key == 'w' || key == 'W') {
boton_dos = 1;

}
if (key == 's' || key == 'S') {
boton_dos = 0;

}

}





class lineas {
float x;
float y;
float jot;
float ex = 0;
float yz = 0;

float dx;
float dy;

float direccion = random(TWO_PI);
float velocidad = 0.6;
float d ;
float k = 1.3;
float delta_x;
float delta_y;

float valor_inicial_x;
float valor_inicial_y;

float absoluto_x;
float absoluto_y;
float switcher = 0;


lineas (float equis, float ygriega , float jota ) {

valor_inicial_x = equis;
valor_inicial_y = ygriega;
x = equis;
y = ygriega;
jot = jota ;

}

void calcular_distancia_gravedad() {
// d = dist( 240, 240, x - ex, y - yz );
d = dist( valor_inicial_x, valor_inicial_y, x - ex, y - yz );
delta_x = k * ( valor_inicial_x - x - ex)/ d * d ;
delta_y = k * ( valor_inicial_y - y - yz )/ d * d ;
delta_x = delta_x;
delta_y = delta_y;

}


void mover(){
cambiaangulo(20);
dx = velocidad * cos(direccion);
// print(" " + dx);
dy = velocidad * sin(direccion );

if (switcher == 0 ){
ex = ex + dx + delta_x;
yz = yz + dy + delta_y;
}
else
{
ex = ex;
yz = yz ;

}
}

void cambiavariable(float moco){
ex = ex + moco;
yz = yz + moco;

}

void dibuja() {
//translate(ex, yz);
pushMatrix ();


translate(x, y );
pushMatrix();
translate(ex, yz);

line(3, 3 , 58, 3);
popMatrix();
popMatrix();
}

void cambia(){
ex = ex + 1;
yz = yz + 22;

}

void cambiaangulo(float amplitud) {

float radianes = radians(amplitud);
direccion = direccion + random ( - radianes , radianes);

}
}


Page Index Toggle Pages: 1