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 › Collision detection
Page Index Toggle Pages: 1
Collision detection (Read 1240 times)
Collision detection
Apr 24th, 2010, 12:51am
 
Hi guys,

I'm new to this board, tried searching for possible threads that could be of help, but even implementing the ideas from the first thread in search results didn't help. I know it's more of a coding issue, the program runs, but collisions are not detected. And well, that's quite necessary :) Can anyone help?

I've posted only an excerpt of the code. Should more be necessary, I'll definitely post more!

FYI, the draw-function resides in a class, and is called from the applet's main draw-function.

Code:
    void draw() {
       order();
       NewPie pie;
       for (int i = 0; i < this._pies.length; i++) {
           pie = (NewPie) this._pies[i];
           pie.setDiameter(this._maxDiam - i * (this._maxDiam / this._pies.length));
           if (!pie.isDrawn()) {
               checkCollision(pie);
           }
           pie.draw(this._items);
       }
       this.evaluateMouseMove();
   }
   
   void checkCollision(NewPie pie) {
       NewPie temp;
       float tempX = random(pie.getDiameter()/2, width - pie.getDiameter()/2);
       float tempY = random(pie.getDiameter()/2, height - pie.getDiameter()/2);
       boolean isColliding = false;
       for (int i = 0; i < this._pies.length; i++) {
           temp = (NewPie) this._pies[i];
           if (temp.isDrawn() &&  !temp.equals(pie)) {
               if (dist(temp.getPosX(), temp.getPosY(), tempX, tempY) < (temp.getDiameter() / 2 + pie.getDiameter() / 2)) {
                   checkCollision(pie);
                   isColliding = true;
               }
           }
       }
       if (isColliding) {
           pie.setPosition(tempX, tempY);
       }
   }


Any help greatly appreciated, I've been breaking my head over this for hours!
Re: Collision detection
Reply #1 - Apr 25th, 2010, 1:36am
 
This may help you
http://processing.org/discourse/yabb2/num_1254601139.html

this thread was about the first game I made on the computer. Its not the best thing ever but if you get anything out of it Ill be happy.
http://www.openprocessing.org/visuals/?visualID=5136
Re: Collision detection
Reply #2 - Apr 25th, 2010, 1:53am
 
Two remarks:
- this.stuff is not necessary in Java, just use stuff.
Well, some people like to use this form to be explicit/symmetrical, but I see it more as "visual noise" distracting code reading. To each their own.
- in checkCollision(), you call checkCollision(), which is a recursive call. Not sure if it is intended... Probably not, as you already computed the dist and set the isColliding boolean.
Re: Collision detection
Reply #3 - Apr 25th, 2010, 2:03am
 
Might or might not be of help, but some of my sketches on open processing use collision detection

http://openprocessing.org/visuals/?visualID=8607
demonstrates (again) some ellastic/inelastic collisions of balls with each other

http://openprocessing.org/visuals/?visualID=8637
is a game where I need collision detecton from flat surfaces
(But the code might be too messy for your purpose)

http://openprocessing.org/visuals/?visualID=8918
uses an untypical collision check based on checking pixel-colors in a buffer image rather than geometric considerations. It works surprisingly well although not perfect.

Re: Collision detection
Reply #4 - Apr 27th, 2010, 1:40am
 
@PhiLho, I know, but I got trained as a software engineer and the use of this is like a religion Smiley
The recursive call to checkCollision is indeed necessary, since there are multiple elements being drawn, and collision can occur with others + on different positions (each time they are given a new random position). However, you are correct that the loop should stop as soon as a collision is detected. I did that now.

@stopfocus and @Bejoscha, your sketches and code helped me through all of this. Elastic collisions aren't necessary, but the way your code browses through all the objects, is much more efficient than mine. Thanks for that!

In general, the collision detection works like a charm now. The only issue occurs when dragging the elements around, and recursively checking for collisions again. Got some nasty StackOverflowException... I'll try to fix it first myself, though!
Re: Collision detection
Reply #5 - Apr 27th, 2010, 1:53am
 
Niels wrote on Apr 27th, 2010, 1:40am:
The recursive call to checkCollision is indeed necessary, since there are multiple elements being drawn, and collision can occur with others + on different positions (each time they are given a new random position).

Isn't a simple loop enough

Quote:
and recursively checking for collisions again. Got some nasty StackOverflowException...

Yes, stack overflow is typical of uncontrolled (or too deep) recursion...
Page Index Toggle Pages: 1