Box2D Forums

It is currently Tue May 21, 2013 8:25 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: b2Contact Addresses
PostPosted: Wed May 09, 2012 1:29 am 
Offline

Joined: Tue May 08, 2012 5:48 pm
Posts: 3
Hello! First of all thanks for the awesome physics engine!

I was wondering if b2Contacts survive from BeginContact to EndContact and if there addresses in memory stay the same throughout that process.

The reason I ask is in my game I am using a "state" system where I change the states of my characters and run game logic depending on what state they are in or have been changed to.

For example:

In presolve a character's state is changed to stateCollision because it collided with an object

The game then plays a collision sound.

(here's where it goes wrong)

In pre solve the same contact with the same object is being handled and it changes the characters state to stateCollision again.

The game then plays the collision sound again.

So I tried this:

Code:
 -(void) newState:(States)state withContact:(b2Contact *)contact {
   
    if (contact != _prevContact) {
        [self newState:state];
    }
   
    _prevContact = contact;
   
}


The contact listener sends this message with the b2Contact involved to the character then the character checks the see if the address of the new contact is the same as the old contact, which I thought would prevent the same collision from changing the characters state multiple times. But it isn't working so far and I was wondering if b2Contacts have unique memory address and if they survive from beginContact to endContact?

Thank you!
Marc


Top
 Profile  
 
 Post subject: Re: b2Contact Addresses
PostPosted: Wed May 09, 2012 2:18 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
Yes, they have a unique memory address, and it actually lasts longer than from BeginContact to EndContact. As soon as the AABBs of two fixtures begin to overlap, a contact is created to manage the interaction between them. The same contact will be used while the AABBs continue to overlap, which could span multiple BeginContact/EndContact events.

But usually there is no reason to care about the address of the contact. Seems to me that what you are trying to do could be done by just playing a sound in BeginContact... maybe I'm missing something. Likewise, changing the stateCollision of your character would usually be done in BeginContact/EndContact. Or, if you keep a list in each game object of what other objects it is touching, you can update this efficiently in BeginContact/EndContact. This list could then be checked any time you're doing game logic decisions and would have more info than just a 'state'. If the sound should get louder or change somehow depending on how forcefully the bodies are currently pressing into each other, you could look at the impulse values in PostSolve.


Top
 Profile  
 
 Post subject: Re: b2Contact Addresses
PostPosted: Fri May 18, 2012 3:18 pm 
Offline

Joined: Tue May 08, 2012 5:48 pm
Posts: 3
Thank you! changing states in begin contact fixed my problem


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 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