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 & HelpPrograms › segment and circle don't intersect....
Page Index Toggle Pages: 1
segment and circle don't intersect.... (Read 1692 times)
segment and circle don't intersect....
Apr 19th, 2010, 6:34pm
 
I've created a snake game, and trying to get the snake to grow every time it "hits" or intersects with the flashing circles (food) that pop up, but the intersection isn't working.

you can view the game and download the code here: http://www.imagearts.ryerson.ca/lbonsell/snake/snakegame.html

THANKS!
Re: segment and circle don't intersect....
Reply #1 - Apr 20th, 2010, 5:57am
 
bo wrote on Apr 19th, 2010, 6:34pm:
I've created a snake game, and trying to get the snake to grow every time it "hits" or intersects with the flashing circles (food) that pop up, but the intersection isn't working.

you can view the game and download the code here: http://www.imagearts.ryerson.ca/lbonsell/snake/snakegame.html

THANKS!


Funny - someone posted with a very similar problem the other day.
Question: What do you pass to your intersect() function
Answer:

Quote:
if (intersect(snake.headx, snake.heady, snake.headsize,
   food.x, food.y, food.diameter) == true) {
     food.activate();      
   }


Question: Where are these properties defined
Answer: In the Snake constructor which gets called only once - when the snake is first created:

Code:
Snake() {
   headx = mouseX;
   heady = mouseY;
   headsize = 20;
 }


Solution: assign mouseX/Y to headx/y properties on each frame - perhaps in the Snake display method...
Re: segment and circle don't intersect....
Reply #2 - Apr 20th, 2010, 9:58am
 
okay, I tried moving
Code:
 headx = mouseX;
heady = mouseY;
headsize = 20;


into the snake display code and the intersection seemed to work, but instead of following the append to increase the array (snake) by one segment it made the snake get off the screen huge.  Shocked It added A LOT more segments then just one like I wanted
Re: segment and circle don't intersect....
Reply #3 - Apr 20th, 2010, 12:30pm
 
Okay, I fixed the intersection, now my problem is the append.
When the snake collects the food (flashing circle) I want it to just increase the segment size by 1, but for some reason it gets extremely large (off the screen large) when it hits the food and I have no idea why!!
Please help!
I've updated the code here: http://www.imagearts.ryerson.ca/lbonsell/snake/snakegame.html
Re: segment and circle don't intersect....
Reply #4 - Apr 20th, 2010, 2:39pm
 
Perhaps you should set a flag to test head/food collision only once (per food lifetime): I bet you check this collision on each frame and increase the size each time, thus the quick growth.
Re: segment and circle don't intersect....
Reply #5 - Apr 20th, 2010, 8:53pm
 
what is a flag and how do I set it??
Re: segment and circle don't intersect....
Reply #6 - Apr 20th, 2010, 11:09pm
 
A flag is just a boolean (true/false, flag raised or lowered...). A signaling variable...
Re: segment and circle don't intersect....
Reply #7 - Apr 20th, 2010, 11:11pm
 
I tried using a boolean but it still didn't work.... maybe I coded the boolean incorrectly?? please help!!
Re: segment and circle don't intersect....
Reply #8 - Apr 20th, 2010, 11:31pm
 
Show the logic you used to manage that.

The idea is: set the variable bHeadHit to false (name is just an example...)
Check the hit on each frame. If hitting and bHeadHit is false, set it to true and do the related action (grow snake). If hitting and bHeadHit is true, just ignore the event, it is already processed. If not hitting and bHeadHit is true, set it to false to manage a new hit.
Re: segment and circle don't intersect....
Reply #9 - Apr 21st, 2010, 8:25am
 
http://www.imagearts.ryerson.ca/lbonsell/snake/boolean/

that is the new link to what I have attempted. I have the keyPressed as a tester for if the growing work and it does, but still, for some reason, when the snake intersects with the food the snake grows constantly.
Shocked
Re: segment and circle don't intersect....
Reply #10 - Apr 21st, 2010, 10:08am
 
I fail to see where is your boolean (probably scanning code too fast).
I suggest to try my algorithm.
Page Index Toggle Pages: 1