Box2D Forums

It is currently Thu May 23, 2013 3:51 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Mon Dec 06, 2010 4:10 pm 
Offline

Joined: Sat Dec 04, 2010 8:00 pm
Posts: 7
Is there a way to determine how much force is being applied to a body? In my game, I'd like a brick to shatter if it is hit with enough force or crumble if it is under enough weight.

Thanks,

Mike


Top
 Profile  
 
PostPosted: Tue Dec 21, 2010 4:07 pm 
Offline

Joined: Sat Dec 04, 2010 8:00 pm
Posts: 7
I tried to remove a body after it had a certain amount of force applied to it. The idea is that if that amount of force is applied, then the object is destroyed and needs to be removed from the scenes. Here is the code I used.

@Override
public void result(ContactResult point) {
if (point.normalImpulse > 800.0f){
//The brick has been hit hard enough to be destroyed

Body body = point.shape2.getBody();
World world = body.getWorld();
body.destroyShape(point.shape2);
world.destroyBody(body);

}
}

I traced the code and made sure this is executing. And it is. But, the bodies are not being removed from the scene.

Am I doing something wrong here?

Thanks,

Mike


Top
 Profile  
 
PostPosted: Tue Dec 21, 2010 8:37 pm 
Offline

Joined: Tue May 26, 2009 7:39 pm
Posts: 35
Mike,

You cannot destroy bodies directly when a collision is detected by the ContactListener. You have to mark the body for destruction and handle it before the next time step.

Nathan


Top
 Profile  
 
PostPosted: Fri Dec 24, 2010 12:17 pm 
Offline

Joined: Sat Dec 04, 2010 8:00 pm
Posts: 7
Awesome! That did the trick!

Thanks,

Mike


Top
 Profile  
 
PostPosted: Thu Aug 11, 2011 12:29 pm 
Offline

Joined: Thu Aug 11, 2011 12:14 pm
Posts: 1
I ran exactly into the same problem, and I found nothing in the Box2D docs about this (http://www.box2d.org/manual.html).
Would it make sense to mention this fact somewhere there?
Maybe I missed it, sorry, if so...

Kind regards,
Stefan


Top
 Profile  
 
PostPosted: Thu Aug 11, 2011 7:34 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
The c++ version asserts if you try to do this so you can tell right away you're doing something wrong. I guess the java equivalent to that be throwing an exception?


Top
 Profile  
 
PostPosted: Tue Aug 16, 2011 6:10 pm 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
java uses assertions as well, you just have to enable them with -ea


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: psbot [Picsearch] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group