We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How do I get objects from two classes to collide
Page Index Toggle Pages: 1
How do I get objects from two classes to collide? (Read 639 times)
How do I get objects from two classes to collide?
Jun 15th, 2008, 3:00pm
 
Hi I'm a newbie to Processing.

So I'm trying to make a simple side scrolling shooter.

I have two classes;
Bullets - Creates Ellipses when a key is pressed.

Enemies - Spawns Rectangles that spawn on the top of the screen and moves them down the screen

It all works fine, but I'm having trouble with getting the
bullets to collide with the enemies. I am aware of to how check for collision. But the problem is, I have no idea how to extract the coordinates for the Bullets or the Enemies.

Any help would be greatly appreciated.
Re: How do I get objects from two classes to colli
Reply #1 - Jun 15th, 2008, 4:12pm
 
One option is to add a "isHitBy(Bullet[] b)" method to your Enemies class, and then each frame pass all the bullets into each enemy, and have their method decide if they've been hit by a bullet, and then do whatever is necessary when that happens.
Re: How do I get objects from two classes to colli
Reply #2 - Jun 16th, 2008, 3:48am
 
Sorry if this is a stupid question. But how I would do that? I still have no idea on how to check the x and y positions.
Re: How do I get objects from two classes to colli
Reply #3 - Jun 16th, 2008, 10:37am
 
since a bullet is moving, your Bullet class should store its x and y position (and maybe have a move() method or something like that, it's up to you). Example :
Code:
class Bullet {
float x;
float y;
...
void move() {
y -= speed;
}
...
}


If you have a method called isHitBy(Bullet b) in your Ennemy class, you can easily retrieve the bullet's position. Example :
Code:
class Ennemy {
...
boolean isHitBy(Bullet b) {
// make some collision test using b.x and b.y;
}
...
}
Re: How do I get objects from two classes to colli
Reply #4 - Jun 16th, 2008, 2:17pm
 
OMG!! It works!! Thanks alot guys!!
Re: How do I get objects from two classes to colli
Reply #5 - Jun 16th, 2008, 9:38pm
 
One of the things I really love about Processing is this sort of reaction, and that people share so freely here on the forum.  I remember having the same "it works!" feeling when I was programming an obstacle-course type game for my HP calculator.  (All in RPL on an HP-48S.  Yeah, I was one of *those* geeks in the back of the class.)  Back then, the HP calculator "community" lived in a BBS (which I think was run by HP in Corvallis, OR) that had a forum not unlike this one where people traded tips and hacks for programming the calculator to do many things it wasn't supposed to be able to do.

If I remember correctly, the solution for collision detection was that the vehicle position was compared with each obstacle on the then current "bottom" row of the screen (the road scrolled by from top to bottom).  The bullet detection described above is an extension of the same thing into 2D.

I guess I don't have much of a point here except to say thank you to everyone for sharing -- your expertise, in some cases, and your enthusiasm in all cases!
Page Index Toggle Pages: 1