What goes into Fixed TimeStep for correct Physics (Box2D)?

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby just4phil » Wed Jun 06, 2012 10:32 pm

yes, there are updates for player and monsters like:

if hit then give them linearImpulse and angularvelocity

etc...

i have now both in the fixed timestep and will see if there are still performance problems....
just4phil
 
Posts: 804
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby broken-e » Wed Jun 06, 2012 11:04 pm

well if you're modifying physics based on physics then maybe it is best to have that with each step in the while loop. I guess you're the only one who can say for sure. If performance is still an issue, and if you do more than just physics updating in that main logic update method, you might split it out and just do the physics with the step and the rest afterward.
broken-e
 
Posts: 600
Joined: Sun Feb 06, 2011 7:20 am

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby just4phil » Thu Jun 07, 2012 8:16 am

Ok, that makes sense.

So how do you use the 4 methods of the contact listener?
I use them as follows:

1./2. begin contact and end contact for sensor triggering, like:
- foot-sensor begins to touch ground -> player may jump
- foot-sensor ends to touch ground -> player may not jump anymore

3. Pre solve for game events like
- player has contact with monster -> set player.boolean.hit = true
- bullet has contact with another object -> set bullet.boolean.explode = true
- ...

4. Post solve:
I don't use that. Maybe useful for game logic after setting booleans in 3. pre solve?


After box2d.step and collision handling described above my game logic simulation kicks in and processes all objects like:

Code: Select all
- updatePlayers:
   - if player.dead == true -> eliminate from game
   - else: player.update()
      - if player.hit == true -> let player whirl around
        (player.body gets some physic impulses like setAngularVelocity, applyLinearImpulse)
         subtract 1 life, ....
         if lifes == 0 player.dead = true
         ...
      - if jump-button pressed == true -> applyLinearImpulse ...
                                - ...
   ...

- updateMonsters:
   - if monster.dead == true -> eliminate from game
   - else: monster.update()
      - if monster.hit == true -> whirl monster around
         - monster.dead = true
      ...
   - monster.counter == 0 -> set level end = true

- updateBullets:
   - if bullet.isExploded == true
      - eliminate bullet from game
      - send back to bulletPool
   - else: bullet.update()
      - if bullet.letBurst == true
         - start explode-animation
         - physics: setLinearVelocity = 0
      - if explode-animation_finished == true -> bullet.isExploded = true
   - ...

- updateCollectables:
   - if collectable.isActive == true -> collectable.update()
      - if collectableType == .... setFriction
      - if touches wall -> bounce back (applyLinearImpulse)
      - ...
   - else:
      - eliminate from game
      - send back to CollectablesPool
   - ...

- updateVisuals: move and fade points-visuals
- update ...
- update ...
- update ...
- update level End
   - if level end = true switch game_state ....


as you see, there are really lots of physics-commands in the game-logic. seems like it should be in the fixed-timestep.
to me this structure makes sense, but i am not very experienced! is this how i should organize it?

i think i cant seperate physics-commands from the rest of the game-logic, its really integrated.
maybe i could use the box2d-method "post-solve" for some parts.

how do you use the 4 methods of the contact listener and organize your game-logic?

bye
phil
just4phil
 
Posts: 804
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby billbo » Fri Jun 08, 2012 6:54 pm

I would like to know the best practice for this as well. I've seen a lot of generic examples using the debug renderer, but nothing very comprehensive in terms of games that demonstrate the "proper" organization that uses the methods.
billbo
 
Posts: 11
Joined: Wed Jun 06, 2012 8:47 pm

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby broken-e » Fri Jun 08, 2012 8:54 pm

One thing I've recently implemented is where I create interfaces for each ContactListener method. Each interface has one method that basically passes the contact info to whatever implements the interface. I haven't implemented preSolve and postSolve yet, just since I haven't been using it, but it should be the same basically. Your game objects can implement any of the contact interfaces if they have logic that requires it.
Code: Select all
public interface BeginContact {                                     
   public void beginContact(Contact contact, Fixture otherFixture);
}                                                                       
public interface EndContact {                                       
   public void endContact(Contact contact, Fixture otherFixture); 
}                                                                     

Then each box2d Body that has a corresponding game object will have that game object set as its userData, and if the game object wants it can implement any of the contact interface methods, so i can pass that stuff on out to the game objects that want it in the world's ContactListener: (I also pass the "other fixture" in case that's useful)
Code: Select all
public class LevelContacts implements ContactListener {
@Override                                               
public void beginContact(Contact contact) {             
   Fixture fixA = contact.getFixtureA();               
   Fixture fixB = contact.getFixtureB();               
   Object obA = fixA.getBody().getUserData();           
   Object obB = fixB.getBody().getUserData();           
   if (obA instanceof BeginContact)                     
      ((BeginContact) obA).beginContact(contact, fixB);
   if (obB instanceof BeginContact)                     
      ((BeginContact) obB).beginContact(contact, fixA);
}                                                       
                                                         
@Override                                               
public void endContact(Contact contact) {               
   Fixture fixA = contact.getFixtureA();               
   Fixture fixB = contact.getFixtureB();               
   Object obA = fixA.getBody().getUserData();           
   Object obB = fixB.getBody().getUserData();           
   if (obA instanceof EndContact)                       
      ((EndContact) obA).endContact(contact, fixB);   
   if (obB instanceof EndContact)                       
      ((EndContact) obB).endContact(contact, fixA);   

...
}


sample game object:
Code: Select all
public class GameObject implements BeginContact {
   Body body;
   public GameObject setup(World world, OtherStuff otherStuff) {
      body = world.createBody(...stuff);
      body.setUserData(this);
      ...
   }
   @Override                                                       
   public void beginContact(Contact contact, Fixture otherFixture) {
      Gdx.app.log("beginContact", this.toString());               
   }
   ...   
}
broken-e
 
Posts: 600
Joined: Sun Feb 06, 2011 7:20 am

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby just4phil » Sat Jun 09, 2012 12:30 am

hi,

that sounds like a good architecture, maybe i will try that in my next game or in version 2.0.
i must say that my collision handling works really well and i wouldnt refactor evrything.
that would be a lot of work.

but i must also say that my architecture reaches the end of maintainable complexity, so i guess your architecture could be a solution for big projects.

right now i have implemented the fixed timestep and update the gameobjects and box2d within it.
that works good, but i think i have to implement the interpolation, too because sometimes i can see stutters with my cannonballs.
that sucks because i thought it could be ok without interpolation..... that seems to be a lot of work ...

do you use interpolation for the fixed timestep?? how did you implement that?

by the way:
for those who would like to take a look at bubblr..... i am very interested in feedback!!!:
http://freigabe.philweb.de/bubblr/bubblr-android.apk

bye
phil
just4phil
 
Posts: 804
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Previous

Return to General Development

Who is online

Users browsing this forum: No registered users and 4 guests