Trouble with basic 2d collision detection
in
Programming Questions
•
1 year ago
SOLVED - See my last reply for details on how i got the below problem to work.
This is what happens below:
-when circles 0 & 1 overlap, they both do not change colour (
fail)
-when circles 0 & 2 overlap, only circle 0 changes colour (
fail)
-when circles 1 & 2 overlap, they both change colour (
pass)
could any one give me a hand?
thanks :)
Main Class:
- CompBall[] cBalls;
- PlayerBall player;
- //---------------------------------------------------------------------------
- void setup() {
- size(800, 800);
- cBalls = new CompBall[3];
- for (int i = 0; i < cBalls.length; i++) {
- cBalls[i] = new CompBall(random(500), random(500), 100, 100, i, cBalls);
- }
- player = new PlayerBall(random(500), random(500), 100, 100);
- smooth();
- }
- //---------------------------------------------------------------------------
- void draw() {
- stroke(0);
- println(frameRate);
- background(0);
- for (int i = 0; i < cBalls.length; i++) {
- cBalls[i].collide();
- cBalls[i].move(player.x, player.y, player.repel);
- cBalls[i].display();
- }
- player.move(mouseX, mouseY);
- for (int i = 0; i < cBalls.length; i++) {
- if (dist(cBalls[i].x, cBalls[i].y, mouseX, mouseY) < 4) {
- player.shrink();
- }
- }
- player.display();
- if (player.w < 20) {
- noLoop();
- }
- }
CompBall Class:
- class CompBall {
- float x, y, w, h, speed, gravity;
- color c;
- int id;
- CompBall[] cba;
- //---------------------------------------------------------------------------
- CompBall(float tx, float ty, float tw, float th, int tempID, CompBall[] tempCBA) {
- x = tx;
- y = ty;
- w = tw;
- h = th;
- c = color(0, 200, 0);
- speed = 2;
- id = tempID;
- cba = tempCBA;
- }
- //---------------------------------------------------------------------------
- void move(float mx, float my, boolean repel) {
- // Distance between X & Y co-ordinates
- float diffX = mx - x;
- float diffY = my - y;
- // Length between the 2 points (pythag)
- float len = sqrt((diffX * diffX) + (diffY * diffY));
- // Catch any divide by zero situations
- if (len != 0)
- {
- // Work out 1 unit of travel for X & Y
- diffX /= len;
- diffY /= len;
- }
- // Move the 1 unit of length by speed
- diffX *= speed;
- diffY *= speed;
- if (!repel) {
- // Change the X & Y of the object
- x += diffX;
- y += diffY;
- }
- else {
- // Change the X & Y of the object
- x -= diffX;
- y -= diffY;
- }
- // Ensure the balls don't disappear off the edge
- x = constrain(x, w/2, width-w/2);
- y = constrain(y, w/2, height-w/2);
- }
- //---------------------------------------------------------------------------
- void collide() {
- for (int i = id + 1; i < cba.length; i++) {
- float dx = cba[i].x - x;
- float dy = cba[i].y - y;
- float distance = sqrt(dx*dx + dy*dy);
- if ( distance < cba[i].w ) {
- c = color(0, 0, 200);
- cba[i].c = color(0, 0, 200);
- }
- else {
- c = color(0, 200, 0);
- cba[i].c = color(0, 200, 0);
- }
- }
- }
- //---------------------------------------------------------------------------
- void display() {
- ellipseMode(CENTER);
- textMode(CENTER);
- noStroke();
- for (float i = w; i > 0; i-=4) {
- fill(c, i);
- ellipse(x, y, i, i);
- fill(0);
- text(id, x, y);
- }
- }
- }
PlayerBall Class:
- class PlayerBall {
- float x, y, w, h, speed;
- color c;
- boolean repel;
- //---------------------------------------------------------------------------
- PlayerBall(float tx, float ty, float tw, float th) {
- x = tx;
- y = ty;
- w = tw;
- h = th;
- c = color(100, 0, 0);
- speed = 1;
- }
- //---------------------------------------------------------------------------
- void move(float mx, float my) {
- x = mx;
- y = my;
- x = constrain(x,w/2,width-w/2);
- y = constrain(y,w/2,height-w/2);
- }
- //---------------------------------------------------------------------------
- void shrink() {
- w -= 0.5;
- h -= 0.5;
- }
- //---------------------------------------------------------------------------
- void display() {
- ellipseMode(CENTER);
- noStroke();
- if (mousePressed) {
- for (float i = w; i > 0; i-=4) {
- fill(255-i, i);
- ellipse(x, y, i, i);
- }
- repel = !repel;
- }
- else {
- for (float i = w; i > 0; i-=4) {
- fill(255-i, 0, 0, i);
- ellipse(x, y, i, i);
- }
- }
- }
- }
1