Rectangle with Ball Collision

edited December 2016 in Questions about Code

I am pretty new to processing and programming, so please bear with me. I want to create a program, where there are 7 rectangles and maximum 15 balls.

The rectangles should not have collision with others, and the balls with rectangles too.

I have created the rectangles, they come down, and when they reached the end of the page, they start over, and the Ball object will be created every time the user click the mouse, till the number of balls reach 15.

I know the code doesn't look good, still new and learning.

Thanks a lot.

There is the code I have written so far:

import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;

float e = 1.0;
ArrayList<Particle> particles;
float radious = random(10, 50);
PVector loc = new PVector(random(radious, width - radious), random(radious, height - radious));
PVector currentPos;
Brick brick_1 = new Brick(random(650), random(650), 2);
ArrayList<Brick> bricks = new ArrayList<Brick>();

void setup() {
size(800, 800);
frameRate(60);
particles = new ArrayList<Particle>();
while (bricks.size() < 7) {
bricks.add(new Brick(random(650), random(650), 2));
}
}

void draw() {
background(0);

// Ball Object START
for (Particle p : particles) {
p.display();
}
for (Particle p : particles) {
p.collide();
}
for (Particle p : particles) {
p.update();
}
for (int i = 0; i < 7; i++) {
Brick one = bricks.get(i);
one.display();
}
ArrayList<Float> list = new ArrayList<Float>();
Brick first_brick = bricks.get(0);
Brick second_brick = bricks.get(1);
Brick third_b = bricks.get(2);
Brick forth_b = bricks.get(3);
Brick fifth_b = bricks.get(4);
Brick sixth_b = bricks.get(5);
Brick seventh_b = bricks.get(6);
list.add(first_brick.getPosition());
list.add(second_brick.getPosition());
list.add(third_b.getPosition());
list.add(forth_b.getPosition());
list.add(fifth_b.getPosition());
list.add(sixth_b.getPosition());
list.add(seventh_b.getPosition());
float this_is_the_min = Collections.min(list);
if (this_is_the_min > height + 20) {
bricks.clear();
for (int i = 0; i <7; i++) {
bricks.add(new Brick(random(650), random(650), 2));
}
}
}
void mousePressed() {
if (particles.size() < 15) {
float radious = random(10, 50);
PVector loc = new PVector(random(radious, width - radious), random(radious, height - radious));
particles.add( new Particle(loc, radious));
}
}


Brick Class

class Brick {
float x;
float y;
float bx;
float by;
float vbx = -1;
float vby = -1; //vbx is fixed;
color c;
Brick (float bx, float by, float vby) {
this.bx=bx;
this.by=by;
this.vby=vby;
c=color(random(255), random(255), random(255));
}

void display() {
fill(c);
noStroke();
rect(bx, by, 100, 30);
by = by + vby;
}

float getPosition () {
PVector v1 = new PVector(0, 0, 0);
PVector buffer = new PVector (bx, by);
float d = buffer.dist(v1);
return d;
}
}

Ball Class (taken from open processing)

class Particle {

PVector loc, vel, nvel;
float radious;
float mass;
color c;
Particle(PVector _loc, float _radious) {
loc = _loc;
float velSize = random(0.5, 3);
float velAngle = random(TWO_PI);
vel = new PVector(velSize * cos(velAngle), velSize * sin(velAngle));
radious = _radious;
mass = sq(radious) * PI;
c=color(random(255), random(255), random(255));
}

void display() {
fill(c);
noStroke();
ellipse(loc.x, loc.y, radious * 2, radious * 2);
}

void collide() {
nvel = new PVector(vel.x, vel.y);
for (Particle p : particles) {
if (p != this && PVector.dist(loc, p.loc) < radious + p.radious) {
nvel = PVector.sub(loc, p.loc);
nvel.normalize();
nvel.mult(PVector.dot(PVector.sub(p.vel, vel), nvel));
nvel.mult(1 + e);
nvel.mult(p.mass / (mass + p.mass));
nvel.add(vel);
break;
}
}
}

void update() {
vel = nvel;
loc.add(vel);
if (loc.x < radious) {
vel.x *= -1;
loc.x += vel.x;
}
if (loc.x > width - radious) {
vel.x *= -1;
loc.x += vel.x;
}
if (loc.y < radious) {
vel.y *= -1;
loc.y += vel.y;
}
if (loc.y > height - radious) {
vel.y *= -1;
loc.y += vel.y;
}
}
}
Tagged:

Answers

  • I don't even see what your question is.
    Read here for more info on rectangle-circle collision detection.

  • sorry, maybe asked badly. If you run my program, you will see 7 bricks coming down the screen. Click anywhere on screen, and a Ball will appear on screen, click again, another ball will be added and so on ... . The Problem is, that the balls have collision with bricks, I don't want the balls have collision with bricks.

    Thanks

  • edited December 2016 Answer ✓

    @TheLoser -- Currently:

    1. your draw() method loops through all your Particles (Balls) and calls .collide()
    2. the .collide() of each Particle checks a list of "particles" (balls). It should probably be taking this list as an argument!

    So your Particles don't bump into each other. But your Particles do bump into your Bricks, because they aren't in the same list.

    You can approach this in a few ways. Here are two:

    1. Make collide take a list as an argument. In draw, loop through all the Particles and have each collide(particles) AND collide(bricks). This requires that Bricks work in the same way (loc, radious, etc.) That leads to:

    2. Make your Brick class a kind of Particle, using "extends." While you are at it, you could also make a Ball class that extends Particle! Then either keep one list of bricks and one list of balls as above, or just keep one big list of both.

  • @jeremydouglass Your solution looks simpler than it is - how will the Particle know if the other Particle is a ball or a brick? You need to check if it is a brick or if it is a ball inside collide, then use the required form of collision checking.
    Rather, I would recommend creating a new collide() method that takes a list of bricks as an argument, and preferably change the original collide() method to take in a list of balls as jeremydouglass suggests.
    Note that if you're going to have many more different shapes and not just two, you will be better off with a solution similar to jeremydouglass' solution.

  • You should not do this - any information on the forum is for sharing, and just removing it is being ungrateful to those who pout effort into answering.

This discussion has been closed.