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 & HelpOther Libraries › Jbox2d, ContactListener in processing
Page Index Toggle Pages: 1
Jbox2d, ContactListener in processing (Read 4124 times)
Jbox2d, ContactListener in processing
Sep 25th, 2009, 1:00am
 
Hello,
this is a cross post, but I try to start a new topic, maybe it will be more visible…
I'm trying to detect the collisions, the contact points in Jbox2d/BoxWrap2d,
to destroy some bodies with another (trying to make a Breakout clone for an installation).
I've read Box2d docs, watched some tutorials so I understood the way the ContactListener is working… But I'm not able to script this in Processing, because I can't understand how to override a method, and even to declare correctly the ContactListener.
All the code I found is in JAVA, and, well, I'm really not and advanced coder. If someone could give me some clues, it would really help me very much.
Thanks,

Gui-aum
Re: Jbox2d, ContactListener in processing
Reply #1 - Sep 25th, 2009, 3:31am
 
Sorry for not answering earlier in the other thread, but I was busy finished a JavaFX program for a contest, so I had no time to experiment... But I kept open your question to come back to it.

Anyway, it was a good idea to open a new thread, no only for visibility but also to avoid "polluting" threads with questions unrelated to original topic... :-D

I haven't tried yet contact listener, but of course it is on my (virtual) todo list. I will do as I did for the mouse joint, I will experiment and integrate latter in the tutorials... Meanwhile, if you have a handy link to existing code, it might save me some researches.
Re: Jbox2d, ContactListener in processing
Reply #2 - Sep 25th, 2009, 1:01pm
 
Thanks, PhiLho, for your answer…  I hope you're kidding, when you're sorry for not answering earlier.

I'm trying to do some detection between bodies with the ContactEdge and the Manifolds, I hope  I could make it work very soon, but I'm really not sure… It can't be a recommended method, but for the moment, it's the only way I found… i will post the code if it works.

I found two sources in Java from ewjordan (who else), with some custom contactlistener, overriding "add", "remove", etc… methods.
http://code.google.com/p/rgbgame/source/browse/trunk/RGBBox2d/src/com/rgbgame/ph...
http://code.google.com/p/pulpfizz/source/browse/trunk/PulpFizz/src/pulpfizz/phys...

and here, exactly what I would like to do in Processing:
http://www.kerp.net/box2d/displaylesson.php?chapter=2&lesson=15
http://www.kerp.net/box2d/displaylesson.php?chapter=2&lesson=16

I opended the sources of Jbox2d testbed and found this kind of method, too (AbstractExample.java).

Thanks again for sharing and helping…

Re: Jbox2d, ContactListener in processing
Reply #3 - Sep 25th, 2009, 2:03pm
 
Hiya,

Im developing a game which you can see here:
http://blissfulthinking.nl/gamezero

It uses processing as well as jbox2d just like yours.

I would like to share my solutions but I think they are hard to use if you stick with the processing .pde editor.  Its really easy to use both processing and jbox2d from the Eclipse programming environment.
[EDIT: Excuse me if im wrong, but I am under the assuption you use the .pde editor, ok byebey]

http://www.eclipse.org/
If you want I can send you links to my project files.

My solution is one where ContactPoints are collected each frame and than reviewed in between. My CustomWorld class looks at them to see if both bodies have custom data (i.e. the getUserData thing) attached. If they both do object A is sent to object B and vice versa.

All attached userdata shares the same parent class called WorldObject. But the children of this class (which extend this class) all inherit different Interfaces that show which contacts are relevant to it.

For instance, the player object is interested in enemy bullets and the enemy ships are interested in player bullets etc. I call these Filters in my implementation but they should not be confused with the Filters that come with JBox2D (which I didn't use so far).

Let me know if you want to see the code!

N.B. I also maintain a thread with some of my algorithms here but those are not contact point related, they are about doing raycasts on polygons and stuff
http://www.box2d.org/forum/viewtopic.php?f=9&t=3411
Re: Jbox2d, ContactListener in processing
Reply #4 - Sep 26th, 2009, 5:06am
 
Hi,
unfortunately, as I'm working on OSX, I'm not able to test your game, Michiel… and the download link at the top of the page is broken…

For the moment, I thought that I wouldn't have any interest to work with Eclipse (because of the "parent." you have to add before P5 instructions),
but… if it could make the detection of contacts possible, let's go.

Obviously, it would be very helpful to have a look at your files. What you decribe is more or less what I need to do. And  I quickly looked at the topic on box2d.org, your code seems to be awesome.
Thanx Michiel for your help Wink
Re: Jbox2d, ContactListener in processing
Reply #5 - Sep 30th, 2009, 1:20am
 
Finally, I get it work! (pfuiii).
But, I had to learn a lot of new things. I moved everything into eclipse (not so easy, but the IDE is really great to work with).
Then I used exactly what is explained at http://www.kerp.net/box2d/ (see the 2 links above). With a few modifications, it's not too complicated to translate it from AS3 to Java.
I'm quite sure my code is the ugliest you could find, so, I'm not sure it would be interesting to post it.
We are far away from the beautiful simplicity of Processing and Boxwrap2d…
Thanks for your help, PhiLho and Michiel.
Gui-aum

Re: Jbox2d, ContactListener in processing
Reply #6 - Sep 30th, 2009, 2:31am
 
Good, I started to look at the issue, but was sidetracked so I didn't advance much. But I will continue to do my demo sketch anyway, and will release it here first.
I am happy you finally solved your problem yourself, it might be painful but that's a good learning experience, and learning to work with Eclipse is good too (I use it daily, although not with Processing...).
Re: Jbox2d, ContactListener in processing
Reply #7 - Sep 30th, 2009, 4:47pm
 
I summerized my method in a document at http://blissfulthinking.nl/jbox2dguide_collisions.pdf
Re: Jbox2d, ContactListener in processing
Reply #8 - Oct 14th, 2009, 6:17am
 
@michiel: oh sh*t man, you do amazing stuff..I saw your post on the box2d forum about raycast rendering.WOOOW  Cheesy alot of code to study  Grin

..but i gotta simple questions for you guys too:how i can change the color of the shape of an body
I looked a million times in the docs but with no succes Cry I've tried the custom rendering method but I get an error when setting it,like"make sure the method is spelled correct"blabla"and has a world arg"..i think it's cuz i use ruby,and in ruby you do not type args..anyway,thanks in advance for your help..->
I've done a new topic related to this
Re: Jbox2d, ContactListener in processing
Reply #9 - Jan 17th, 2010, 10:49am
 
For those of you who are realising that none of the other examples are in Processing, or wishing gui-aum had posted his solution, here is a working method:

Code:
public class CustomListener implements ContactListener {
 CustomListener(){
 };
 void add(ContactPoint cpoint)
 {//contacts collide for the first time
   println("new hit");
 }

 void persist(ContactPoint cpoint)
 {//contacts continue to collide - i.e. resting on each other
 }

 void remove(ContactPoint cpoint)
 {//objects stop touching each other
 }

 void result(ContactResult point)
 {//coltact point is resolved into an add, persist etc
 }
}


then you have to add the custom listener to your world
Code:
    m_world = physics.getWorld();
   m_world.setContactListener(new CustomListener());


Hope that's useful.
Re: Jbox2d, ContactListener in processing
Reply #10 - Feb 9th, 2010, 2:04pm
 
Findus1 wrote on Jan 17th, 2010, 10:49am:
For those of you who are realising that none of the other examples are in Processing, or wishing gui-aum had posted his solution, here is a working method:


Perhaps it's because I'm using the PDE, but I get a "setContactListener(sketch.CustomListener) does not exist" error.

I also cannot find ANYTHING in the boxWrap2D docs that mention listeners or destroying objects... am I missing something obvious

-cr0ybot
Re: Jbox2d, ContactListener in processing
Reply #11 - Feb 10th, 2010, 5:31am
 
You have to add also the given CustomListener class, with stuff within.

And BoxWrap2D doesn't wrap all JBox2D, so the latter must be checked for advanced stuff.
Re: Jbox2d, ContactListener in processing
Reply #12 - Feb 13th, 2010, 4:13pm
 
I found the issue... I'm kicking myself now.

The above code works perfectly as long as you define m_world.

Code:
World m_world;
Physics physics;

void setup(){
 physics = new Physics(this,width,height);
 m_world = physics.getWorld();
 m_world.setContactListener(new CustomListener());
}


Page Index Toggle Pages: 1