Having multiple moving objects collide detection issues


#1

Recently I have been playing with the Arduino and I have been using processing for the coding. Before I go any further the program does run without the specific need for an Arduino and the issue I am having also does not involve it.

Now onto the issue, so I am struggling to make two moving objects, multiple random falling ellipse and a bouncing rectangle. There are multiple issues, for one the speed of the falling ellipse is in a class and it isn’t a variable therefore it is unreachable. I am sure they might be a way to fix that that I am overlooking. The next thing is I know how to even do two moving rectangles colliding if the speed is available however I do not know hoe to do an ellipse and rectangle, one moving and stationary is easy but I am a bit confused to both moving.

I am trying to get a collision detection for the falling ellipse and bouncing rectangle just for clarification.

Here is my code:

int direction1 = 100;
int speedX = 12;
int heightWidth=40;
int bounceSize= 40;
int ellipseSizeone=40;
int ellipseSizetwo=50;

Meteor meteors;
int total = 10;
Meteor[] fall = new Meteor[total]; 

void setup() {
size (800, 500);
background(0);
smooth();
noStroke();

for (int i = 0; i < fall.length; i++) {

fall[i] = new Meteor(); 
meteors = new Meteor();
}

void draw() {
if (arduino.digitalRead(button) == 0) { // if the button is not pressed
background(0);

direction1+=speedX;
if (direction1 > width-bounceSize) {
direction1 = width-bounceSize;
speedX = -12;
}
if (direction1 < 0) {
direction1 = 0;
speedX = 12;
}
} 
rect(direction1, 450, heightWidth, heightWidth);
fill(0,20);
rect(0,0,800,500);

for (int i = 0; i < fall.length; i+=3) {
fall[i].fall();
}

fill(0,255,0);
}
class Meteor {
float x = random(800);
float y = random(-500);

void fall() {
y = y + 7;
fill(random(200),0,0);
ellipse(x, y, ellipseSizeone, ellipseSizetwo);

if(y>height){
x = random(800);
y = 0;
}

}
}

#2

You’ll have better luck if you post a simplified example that only includes the code necessary to reproduce your problem. More info here:

Also, can you be a little more specific about exactly what you want to happen? Which line of code are you having trouble with?


#3

So I have a bouncing rectangle on the bottom on the screen which code is in the void draw. I also have randomly falling ellipses which code is in the class meteor and under void fall(). I want to have a detection so when a falling ellipse hits the bouncing rectangle it does something I want to.

First I know how to do two rectangles but I do not know what the detection rules are for hitting the rectangle. I do know however that the equation should end up using their speeds somewhere for collision detection as it is a moving object. The issue is the ellipses speed is under void fall and part of the class meteor.

If I try to call the speed of the ellipse it will not recognize y (the speed) because it is under its own separate class. So basically this and I don’t know how to calculate the detection for a rectangle and ellipse hitting. I hope that makes sense.


#4

You just need to know the position and the dimension of the objects to know if they collide.

What do you want to do after the collision?


#5

But the positions are not set as both objects are moving the dimensions are clearly defined but neither of the objects are in one spot and the objects falling are defined under one thing but creates a random ten of the same falling on any side of the screen. And I will have it adding score to like a counter because later ill have falling blocks that end the game instead, I am creating a game. And isn’t the speed of the object required to find its position?


#6

The speed is definitely not needed for that. And you have necessarily an access to the position since your are drawing your shape at the proper position on each frame.

Now if you are planing on making a game with several shapes moving in different directions with different speed, I advise you to create some class to handle all the logic and keep your code neat!


#7

If you post your question to multiple sites, please link between the posts so we can see what help you’ve already received.

This question has also been asked here: