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 › collision detection
Page Index Toggle Pages: 1
collision detection (Read 2309 times)
collision detection
Aug 6th, 2007, 3:37pm
 
Hi,

 I would like to implement collision detection in a 3D world within processing.  I know that Java3d has collision methods built in (WakeupOnCollisionEntry), but not knowing java very well, collision seems to be difficult to implement.  Can someone suggest how to access the shape properties of a 3D object in processing so that the parameters can be used in WakeupOnCollisionEntry?
http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_2_API/j3dapi/javax/media/j3d/WakeupOnCollisionEntry.html


More generally, I think there might be some interest in a simple way to do collision detection within processing.  Would it be possible to have some special mode, where objects would automatically have boundaries, and two objects would not be able to occupy the same space. In this mode whenever there is a collision between two objects, then some collision parameters would be set to mark the location (collisionX, collisionY, collisionZ) or some collisionDetected function would be called.

thanks.


Re: collision detection
Reply #1 - Aug 8th, 2007, 1:27am
 
I suspect you're going to have to look deeper into how Java3d handles this stuff - Processing doesn't really store objects as separate entities, so it doesn't make much sense to have object collision internally.

I agree with you though, this would be fantastic functionality to have.  But it's extremely difficult in general.  If you figure anything out, definitely let us know what you've got, because this stuff is a nightmare to program by hand.
Re: collision detection
Reply #2 - Aug 8th, 2007, 4:45am
 
This book is a really good resource for 2D and 3D collision detection;

http://www.charlesriver.com/books/BookDetail.aspx?productID=84482

I've been meaning to create a series of tutorials myself, as part of an expansion I'm planning (at some point) for my "http://www.shiffman.net/teaching/nature/ stuff.  So someday I hope!


Re: collision detection
Reply #3 - Aug 8th, 2007, 8:32am
 
Thanks for the suggestions.  Maybe I'm underestimating how difficult this will be, but there seems to be several tutorials on the web that seem to have the approximate behavior that I want.

http://fivedots.coe.psu.ac.th/~ad/jg2/ch04/index.html
http://www.java-tips.org/other-api-tips/java3d/collision-detection-with-java3d-2-2.html

When you say that processing doesn't really store objects as separate entities, I assume you mean in the processing script.  But internally, in the java code, I assume that the processing command "box(15,20,10)"
is something internal like this
com.sun.j3d.utils.geometry.Box mybox =
new com.sun.j3d.utils.geometry.Box(15, 20, 10,
                      Primitive.GENERATE_NORMALS |
                      Primitive.GENERATE_TEXTURE_COORDS);

I'm wondering how one accesses this mybox variable.

thanks
Re: collision detection
Reply #4 - Aug 8th, 2007, 9:33am
 
Alas, it's not done like that internally.  Processing doesn't use Java3d for 3D, for one, instead it uses JOGL bindings (or its own internal software renderer if you're using P3D).  What happens when you make a box in Processing is essentially that it creates, transforms, and immediately draws 12 triangles (6 faces * 2 triangles/face) to the screen without storing them anywhere (in fact, if you look in the source code, you'll see that P3D reuses the same PTriangle object over and over; with OpenGL it sends a separate drawing command for each triangle right as they are created).  So when you draw another box, all record of the first box is gone (except whichever pixels were drawn to the screen), so it's not possible to do collision detection that way.  The only way is either by hand or via a library - something else has to sit between you and the drawing functions to handle this stuff.  Otherwise, you probably want to just dig directly into Java3d programming rather than trying to make Processing interoperate appropriately.

Out of curiosity, what do you need this for?  Are we talking about a few simple shapes on screen and just making sure that they don't interpenetrate, or are you trying to set up a physics engine to run thousands of rigid bodies in real time?  I ask because there are easy ways to implement this stuff, and hard ways.  The easy ways will do the job, but they won't do it fast enough to handle massive amounts of geometry (like for a physics-based FPS or anything like that).  If you need major speed, you're in for a large amount of research and work - people have literally written entire textbooks about how to do this stuff in real time, and entire PhD theses exist on fairly small pieces of this problem.  But if it's for something fairly simple, you might be able to get away with pairwise triangle-intersection tests, which are really easy to code.
Re: collision detection
Reply #5 - Aug 9th, 2007, 12:45am
 
Thanks for the explanation.  I didn't realize that processing doesn't use Java3d.  I wouldn't mind using java3d directly, but I'm also using external controller that uses gainer (http://gainer.cc/) and so i would like to be able to avoid having to code a connection between gainer and java.

I'm just trying to code up a simple game, where there are two boxes and I want them to stop moving whenever they intersect.    If you have any suggestions about how to do that, please let me know.
Re: collision detection
Reply #6 - Aug 9th, 2007, 3:55am
 
One thing to keep in mind is that if something works with Processing, you can probably make it work in pure Java as well, since that's what it is ultimately written in, though it might require some work.

But if you've just got two boxes, the problem should be simple to solve - it's really only once you get many things of complex shapes that these types of problems become difficult.  Are the boxes able to rotate, or are they always flat-on?  Also, do they need to bounce, or just stop?  Either way, the detection and response for these cases should be pretty simple, so I can probably at least point you in the direction of something that will cover what you need.
Re: collision detection
Reply #7 - Aug 11th, 2007, 2:51am
 
Doing the collision stuff might be easier in pure java3d, but then I would have to create the code to link up to my external hardware, so either way it might be a lot of work.

I'm just using boxes and they should be able to do all of the processing transformations (rotate, translate, scale,...).  I don't need any bouncing.  I just want to be able to recognize a collision before it happens, and then have that change how the scene is updated in the draw function.
Re: collision detection
Reply #8 - Aug 12th, 2007, 4:12am
 
i think you should look into AABB to an easy box-to-box collision detection test..
basically you would just have to test each box's vertices to each box's bounding box.
try google, you should find some very useful information at the first web pages
Page Index Toggle Pages: 1