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 › need help with intersection!
Page Index Toggle Pages: 1
need help with intersection! (Read 746 times)
need help with intersection!
Dec 3rd, 2009, 1:50pm
 
Can anyone help?  I am working on a basic game for an engineering class, but am running into difficulties.  I am trying to use the intersect function between two classes, one of which is an array for cats, another is a class for beer.  when the two objects (cats and beer) get too close, a spilled  beer image will be displayed. Any tips?  I've tried changing around the variables, but nothing seems to work.

boolean intersect (Cat b) {
    float distance = dist(cx,cy,mouseX,mouseY);
   
  if (distance < r/2) {
    return true;
 } else {
     return false;}
 }

boolean intersecting = intersect(cats,beer);
if (intersecting) {
  image(spilledbeer,beerx,beery);}
 

 }
Re: need help with intersection!
Reply #1 - Dec 4th, 2009, 1:13am
 
You make an intersect function with one parameter, and call it with two...
You don't use the parameter of the function but instead use mysterious variables and the mouse coordinates (reusing some code? good, but you should first understand it!).
You can replace your test by:
return distance < r/2;

I can hardly provide correct code as I don't know what is in your classes.
But the base idea is to have something looking like:
Code:
boolean intersect(Cat c, Beer b)
{
return dist(c.x, c.y, b.x, by) <= (c.dim + b.dim)/2;
}

where I assume your classes have the x and y variables holding the coordinates of the center of your objects, and the dim variable which is the diameter of the object (supposing they are approximatively round).
Re: need help with intersection!
Reply #2 - Dec 4th, 2009, 1:23am
 
Cats and Beer, i would love to know what kind of game that is going to be Smiley
Re: need help with intersection!
Reply #3 - Dec 4th, 2009, 2:27am
 
Probably need some pizzas and a TV set too...  Grin
Re: need help with intersection!
Reply #4 - Dec 4th, 2009, 10:40am
 
thanks for the help, i'll give it a shot.  And because there is some interest, the game is very simple.  An array of cats randomly float around the screen, while you control an image of a Sierra Nevada Pale Ale bottle with the mouse to avoid hitting them.  Every ten seconds, another cat is drawn in.  If a cat hits the beer image, a spilled bottle will be displayed, and game over.  The title is "Save the beer from the stupid cats".  It's an homage to my wife's cats.  Anyways, having trouble with the intersection of the beer and the cats.  Hopefully i can get this working soon.
Re: need help with intersection!
Reply #5 - Dec 4th, 2009, 11:01am
 
Based on your description of the game you might find this library useful.

http://www.lagers.org.uk/s4p/index.html
Page Index Toggle Pages: 1