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.
Page Index Toggle Pages: 1
Collisions? (Read 1010 times)
Collisions?
May 29th, 2005, 12:08pm
 
I am writing a simple game in Processing in order to try to learn a bit more about it. I have an enemy class and a polygon which is drawn at the mouse coordinates to represent the player, but I was wondering how I would work out collisions, or if there are any commands in processing to deal with this sort of thing. Can anyone help? Here's the code:

//Simple game test
//Last updated 28.05.05


int amtFoes = 10;
Enemy[] foes;
int maintimer;

void setup()
{

 noCursor();
 smooth();

 size(300,200);
 background(20,20,45);
 framerate(60);
 
 foes = new Enemy[amtFoes];
 
 for(int i=0; i<amtFoes; i++)
 {
   int index = i;
   foes[index] = new Enemy((i*25)+35, 10, 0, false);
 }
 
}

void draw()
{

 background(20,20,45);
 
 for(int i=0; i<amtFoes; i++)
 {
 foes[i].update();
 foes[i].drawtoscreen();
 }
 
 fill(140,150,210,180);
 beginShape(POLYGON);
   vertex(mouseX,mouseY);
   vertex(mouseX+10,mouseY+15);
   vertex(mouseX,mouseY+10);
   vertex(mouseX-10,mouseY+15);
 endShape();
 
 if(maintimer<millis()) {
 for(int i=0; i<amtFoes; i++)
 {
   int index = i;
   foes[index] = new Enemy((i*25)+35, random(-10,-50), random(9.25,9.55), false);
 }
 maintimer = maintimer + 5000;
 }

}

class Enemy
{

 float x,y;
 float dir;
 boolean dead;
 int shottmr;
 
 Enemy(float cx, float cy, float cdir, boolean cdead)
 {
   x = cx;
   y = cy;
   dir = cdir;
   dead = cdead;  
 }
 
 void update()
 {
     
   x = x + sin(dir)*1.8;
   y = y - cos(dir)*1.8;
 
 }
 
 void drawtoscreen()
 {
 
   fill(200,180,120,150);
   stroke(255,255,255);
   ellipse(x,y,20,20);
 
 }

}

class EnemyBullet
{

 float x,y;
 int speed;
 
 EnemyBullet(float bx, float by, int bspeed)
 {
   x = bx;
   y = by;
   speed = bspeed;
 }

}

Ps. Have I posted this in the right section?
Re: Collisions?
Reply #1 - May 29th, 2005, 12:27pm
 
Should probably be posted under programs, since it's pointers you are after, not directly syntax.  
but that doesn't stop me from helping, so here goes.

the player and enemy class must each have an object to check intersection, as a quick hack i use rectangles.
Code:

// so give both enemy and player and object
Rectangle playerc; // in the top, global
Rectangle c; // in the top of the enemy class
// remember to initialize the rects

// then check for each enemy
boolean checkIntersection(){
boolean ret = false;
for(int i = 0; i < foes.length; i++)
if(foes[i].c.intersects(playerc)) ret = true;
return ret;
}


Think this should work, implemented properly..
And this is the quickest hack i found so far, and it's the one i use in the games i've made.. you might be able to use something else than a rectangle, but i havn't checked that out..
give us a shout if you find something better :)

-seltar
Page Index Toggle Pages: 1