[QuickB2] Custom gravity

Hi there,

I would like to create a platformer with with QuickB2 and I need more info about it.
- one-sided platforms
- disable gravity for certain objects (bullets for example)
- moving platforms

Does anybody had these problems? Source, info, advices are appreciated.

Thanks,
Plamen
Hi Plamen,

Thanks for your questions.

- One sided platforms you have to handle yourself at this point. Here's an example:
Code:
myPlatform.addEventListener(qb2ContactEvent.PRE_SOLVE, contactHandler);

function contactHandler(evt:qb2ContactEvent):void
{
     if( <contact between player and platform is on the underside> )
     {
          evt.disable();
     }
}


- Gravity can be fine controlled by using qb2GravityField's. These are less efficient than using qb2World::gravity, but are a great way to have really dynamic gravity layouts. You can add shapes to a qb2GravityField to define its boundaries, or make it ubiquitous so that it affects all objects in the same branch of the world hierarchy.

- Moving platforms are best done by setting "myPlatform.isKinematic = true", and then manipulating the platform's linear velocity directly.

I hope that's kind of clear. I definitely suggest trying out the above stuff by messing around with the examples provided with the library.

- Doug
Hi Doug,

I tried to test the qb2GravityField, but not sure what to do. The class is empty in the source and it extends another empty class qbEffect which extends Object. Also, should I add the field to the world? What I'm trying to do is to disable the gravity for the bullets so that they don't fall down on the ground.

-Plamen
My apologies. I forgot to mention that you need to get the latest revision from SVN. Your question inspired me to spend some time actually implementing the qb2GravityField class, but it's not available in a downloadable .zip release yet.

An example for disabling gravity for certain objects...
Code:
var world:qb2World = new qb2World(); // world has default 0,0 gravity

var myBullet:BulletClass = new BulletClass(); // let's say here that BulletClass extends from qb2Body.
world.addObject(myBullet);

var field:qb2GravityField = new qb2GravityField();
// field.addObject(qbStock.newRectShape(new amPoint2d(100, 100), 100, 50);  // uncomment this to give your field boundaries...otherwise it will affect all objects in the world.
field.gravity.y = 9.8;  // realistic earth gravity
field.disableForType(BulletClass);    // this will disable gravity for all instances of BulletClass (and its subclasses).
//field.disableForInstance(myBullet);   // this will disable gravity for this one particular instance of BulletClass.
world.addObject(field);


At this point, I want to focus on implementing features that people will actually use, not what I think they might use, so let me know again if you come across any feature you think might be missing and I'll see what I can do to address it.

I'm still thinking of a nice way to integrate one-sided shapes into the library...we'll see 🙂
Oh, that looks very nice 🙂. Really powerfull feature. Bad news is I decided to not use such complex physics for my platformer. Reason: I spend a lot of time to adjust my charter's jump behavior and with physics engine it looks too realistic. It doesn't fit the gameplay. I tried changing the position directly but this adds issues with the collision resolving. I guess I'll come back with my next game and try to use the engine then. But here are things that I consider very helpful for games (platformers, my target genre).
- gravity objects - for example, ninja game where the ninja can climb walls. When the ninja jump towards the gravity objects (or w/e you call it) it sticks to the wall and you can move up and down. When you press jump key, the ninja will jump away from the wall. The same way you can make Sonic-like games where you run in these roller-coaster things 🙂. I'm pretty sure it's possible to do it without any changes to the QuickB2, I just can't think right now for solution. Edit: I think that this actually can be done with the GravityField class.
- terrain destruction is something else that will open many possibilities for the games. Not sure if it's implemented, but you could do games like worms or just games where explosions change the enivornment.
- bullets gravity is already solved 🙂

Let me know if you need more ideas.
I just spit some that were in my head right now 🙂
Thanks for the feedback.

Yes changing character position directly seldom works...it only really works if you're only interested in collision detection, and not collision response.

There are a few ways you can do platform physics as far as character movement. A typical unrealistic thing is to be able to reverse character direction in mid-air. With QuickB2, this would simply be
Code:
character.linearVelocity.x = -character.linearVelocity.x
when a left-right key change is detected. If you have any other specific issues with character movement, let me know and I might have a solution.

Destruction I've been working on for a few weeks 🙂...pretty complicated though...not sure when I'll get done.
Note, I've changed the API for qb2EffectField to make it cleaner...you pass instances/types through enable/disable functions now instead of having to make your own dictionary...much easier...the changes are reflected in the above sample code for disabling bullet gravity.
You do work really hard on that. I'll try to make a list while I work on my game and see what feedback I can do for you. I have tight deadline as of right now, so I'll use my own code.. but I definitely will come back to QuickB2 since it's awesome. Thanks for your help and attention 🙂

Edit: can I use collision detection without instantiating qb2World? I could use that for now if it's self-contained. I could update some objects' positions and check for collisions and do my own resolving with the information from the collision contact.
I didn't tried that, but I hope that the answer is easy and I'm not making you trying things for me.
Yea, I want QuickB2 to be the best solution out there for 2d physics, so feedback is awesome.

For collision detection, you do need to instantiate a world and add stuff to it. To turn off collision response:
Code:
var world:qb2World = new qb2World();
world.isGhost = true;

// add stuff to world...everything will fire collision events, but not collision response


So in this way, you can use QuickB2 as not really a physics engine at all, but just a collision detection engine, which can be useful for many different games.
Hi Doug,

I have this code:

Code:
public var circle:qb2CircleShape
      public function Main():void
      {
         var world:qb2World = qb2Stock.newDebugWorld(new amVector2d(0, 30), this.graphics, stage);
         world.start();
         
         circle = qb2Stock.newCircleShape(new amPoint2d(100, 100), 50, 1);
         world.addObject(circle);
         circle.restitution = .0
         
         var rectangle:qb2PolygonShape = qb2Stock.newRectShape(new amPoint2d(200, 400), 150, 15, 0);
         world.addObject(rectangle);
         //
         //var body:qb2Body = qb2Stock.newLineBody(new amPoint2d(300, 300), new amPoint2d(400, 400), 10, 1, qb2Stock.ENDS_ROUND);
         //world.addObject(body);
         
         var walls:qb2StageWalls = new qb2StageWalls(stage);
         world.addObject(walls);
         
         world.addEventListener(qb2UpdateEvent.PRE_UPDATE, onPreUpdate);
         qb2Keyboard.makeSingleton(stage);
         //world.addEventListener(qb2ContactEvent.PRE_SOLVE, onContactStarted);
         //circle.addEventListener(qb2ContactEvent.PRE_SOLVE, onPreSolve);
         rectangle.addEventListener(qb2ContactEvent.PRE_SOLVE, onPlatformPresolve);
      }
      
      private function onPlatformPresolve(e:qb2ContactEvent):void
      {
         if (e.contactNormal.y == 1) e.disableContact();
      }
      
      private function onPreSolve(e:qb2ContactEvent):void
      {
         //trace(e.type == qb2ContactEvent.PRE_SOLVE);
         if (e.contactNormal.y == 1) e.disableContact();
      }


What I want to do is to disable the contact. I got error only when I hit the platform from below.
Here is the error:
Code:
[Fault] exception, information=Error: Contacts can only be enabled/disabled for "pre-solve" events.



EDIT: This code inside qb2BaseContactEvent looks suspicious:
Code:
private function checkForError():void
      {
         if ( type != qb2ContactEvent.PRE_SOLVE || type != qb2SubContactEvent.SUB_PRE_SOLVE )
            throw new Error("Contacts can only be enabled/disabled for \"pre-solve\" events.");
      }


Which means that I'll always have the error unless the type is both PRE_SOLVE and SUB_PRE_SOLVE, because if either of them is not the correct type it will return "true" and throw the error.
EDIT3: I commented out SUB_PRE_SOLVE and it works for now, but since I didn't wrote the lib.. I'm not sure whether this will not break something else. Doug, can you confirm that the change is okay? The version of qb2 I'm using is the version from the SVN.


EDIT2: Here is something else that I can't make work:


Code:
   //var effect:qb2GravityField = new qb2GravityField(true);
         var effect:qb2GravityField = new qb2GravityField(false);
         effect.gravity.y = 9.8;
         //effect.gravity = new amVector2d(0, 30);
         //effect.disableForType(qb2CircleShape);
         //effect.disableForInstance(circle);
         //effect.enableForType(qb2CircleShape);
         effect.enableForInstance(circle);
         world.addObject(effect);


I tried different variants.. but I can't control the circle instance (it's the same circle from the example in the prev post). If I set the field to be ubiquitous then I can't filter the circle and it applies to everything, if I set ubiquitous to false, then I can't make the filter to act only on the circle.. Probably I don't understand something.. but it doesn't work for me.

Original thread: https://box2d.org/forum/viewtopic.php?t=6366