Thank you, I've made the changes and it works, I´ve been letting the ball bounce for a long time and it hasn't change its maximun height. But I dont understand wthe reason why my code doesn´t work and yours does.
Now I,ve tried to use the same code to make a simple videogame and the ball bounce lower and lower. I can´t see any difference between both codes. In fact I used copy and paste.
This is the code simplified of the videogame:
Code:Bola pelota;
Bola jug1;
Bola jug2;
float vel=2;
float dista;
float gravedad=1.5;
boolean der1;
boolean izq1;
boolean der2;
boolean izq2;
//principio
void setup(){
size(500,350);//tamaño
pelota=new Bola(0,10,10,0,20);
jug1=new Bola(width/4,height-100,0,4,70);
jug2=new Bola(width*3/4,height-100,0,4,70);
background(100);//fondo
stroke (255);//lápiz
fill(255);
smooth(); //dibuja con suavidad
frameRate(17);//velocidad
}
//dibujo
void draw(){
background(100); //borra
rect(width/2,height-200,10,200);
pelota.bota();
jug1.bota();
jug2.bota();
jug1.golpea();
jug2.golpea();
mueve();
}
class Bola{
float x;
float y;
float vx;
float vy;
float diametro;
Bola(float xin,float yin,float vxin,float vyin, float diame){
x=xin;
y=yin;
vx=vxin;
vy=vyin;
diametro=diame;
}
void bota(){
ellipse(x,y,diametro,diametro);
vy=vy+gravedad;
x=x+vx;//aumenta x
y=y+vy;
//rebote
if((abs(x-width/2)<diametro/2+5)&&(y>height-200)){
if(x<width/2){
x=width/2-(diametro/2+5);
}
else
{
x=width/2+(diametro/2+5);
}
vx=-vx;
}
if(y>height-diametro/2){
vy=-vy;
y=height-diametro/2;
}
if(y<diametro/2){
vy=-vy;
y=diametro/2;
}
if(x>width-diametro/2){
vx=-vx;
x=width-diametro/2;
}
if(x<diametro/2){
vx=-vx;
x=diametro/2;
}
}
void golpea(){
dista=sqrt((x-pelota.x)*(x-pelota.x)+(y-pelota.y)*(y-pelota.y));
if (dista<diametro/2+pelota.diametro/2){
do{
dista=sqrt((x-pelota.x)*(x-pelota.x)+(y-pelota.y)*(y-pelota.y));
pelota.y-=pelota.vy/sqrt(pelota.vy*pelota.vy);
}
while (dista<=diametro/2+pelota.diametro/2);
pelota.vy*=-1;
if(pelota.x<x){
pelota.vx=-abs(pelota.vx);
}
else{
pelota.vx=abs(pelota.vx);
}
}
}
}
void mueve(){
jug1.x+=jug1.vx;
jug2.x+=jug2.vx;
}