We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, im doing a project to university and i need some help , how i can collide the ellipses with the paddle that i draw and, how i can control an ellipse with mouse and she has to collide with the others and finally make a goal and their balls go out the goal and go back when she dont hit goal .
Their is the code, PLZ HELP ME
import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.effects.*; import ddf.minim.signals.*; import ddf.minim.spi.*; import ddf.minim.ugens.*;
AudioPlayer sound ; Minim minim; PImage background ; int totalBolas = 16; float contadorX = 10, y=10, passo=3; Bola[] Bolas = new Bola[totalBolas];
void setup() { size(800,400);
background = loadImage("pokemon1.jpg");
minim= new Minim(this); sound = minim.loadFile("106-the-road-to-viridian-city-from-palette.mp3"); sound.loop();
for (int i = 0; i < totalBolas; i++) { Bola bola = new Bola(); bola.x = random(width); bola.y = random(height); bola.vx = random(10) - 5; bola.vy = random(10) - 5; bola.raio = 14; Bolas[i] = bola; bola.cor = color(random(255), random(255), random(255)); } }
void draw() { background(background); fill(color(255, 0, 0)); for (int i = 0; i < totalBolas; i++) { Bolas[i].x += Bolas[i].vx; Bolas[i].y += Bolas[i].vy; checkwallcolison(Bolas[i]); Bolas[i].desenha(); } checkballcolision(); rectMode(CENTER); //rect(width/2,0,350,100); //rectMode(CENTER);
rectMode(CORNER); strokeWeight(1); fill (255,0,0); noStroke(); rect (contadorX,y+10,80,20); contadorX = contadorX+passo; if ((contadorX >= width-90) || (contadorX <=0)) passo = passo*(-1);
}
void checkballcolision() { for (int i = 0; i< totalBolas; i++) { for ( int j= i +1; j <totalBolas; j++) { float dx = Bolas[j].x - Bolas[i].x;//distancia de x float dy = Bolas[j].y - Bolas[i].y;//distancia de y float dist = sqrt(dxdx + dydy);//teorema de pitagoras if (dist<(Bolas[j].raio + Bolas[i].raio)) { //OS Circulos tocam-se float normalX = dx/dist; float normalY = dy/dist;
//encontrar ponto médio
float midpointX = (Bolas[i].x + Bolas[j].x)/2;
float midpointY = (Bolas[i].y + Bolas[j].y)/2;
//bolas tocam-se e mexem-se
Bolas[i].x = midpointX - Bolas[i].raio * normalX;
Bolas[i].y = midpointY - Bolas[i].raio* normalY;
Bolas[j].x = midpointX + Bolas[i].raio * normalX;
Bolas[j].y = midpointY + Bolas[i].raio* normalY;
float dVector = ( Bolas[i].vx-Bolas[j].vx) * normalX;
dVector += (Bolas[i].vy - Bolas[j].vy) * normalY;
float dvx = dVector * normalX;
float dvy = dVector * normalY;
Bolas[i].vx -= dvx;
Bolas[i].vy -= dvy ;
Bolas[j].vx += dvx;
Bolas[j].vy += dvy;
}
}
} } void checkwallcolison(Bola bola) {
if (bola.x < bola.raio) {
bola.x = bola.raio;
bola.vx *= -1 ;
}
if (bola.x > width - (bola.raio)) {
bola.x = width - (bola.raio);
bola.vx *= -1;
}
//if (bola.y < bola.raio) {
//bola.y = bola.raio;
//bola.vy *= -1;
//}
if (bola.y > height - (bola.raio)) {
bola.y = height - (bola.raio);
bola.vy *= -1;
}
}
class Bola {
float x = 0;
float y = 0;
float vx = 0;
float vy = 0;
float raio= 5;
color cor;
void desenha() {
fill(cor);
ellipse(x, y, raio * 2, raio * 2);
}
}
Answers
Go back
Edit your post please.
Format the code right:
Empty line before and after the sketch/ code in your post
Highlight the code
Click the small C with the mouse
Looks better:
you have a lot of different questions!!!
do you mean one ball with the other balls?
see here :
https://www.processing.org/examples/bouncybubbles.html
just remove the gravity from the example
check the ellipse against the paddle :
you need to check each ball against the paddle
so do it in the for-loop:
when ball hits paddle reflect ball
when ball misses paddle : ball dies
what!?
you want to control the paddle, not the mouse, right?
err.... wait....
they go out when they miss the paddle, right?
And then the score gets decreased (because it's a failure).
ok, and when balls hits goal, score gets increased (because it's a success).
The goal is similar to paddle with if (see above). But the goal does not move. Or it could move as well ;-)
your sketch