Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a very simple platform game builder and I've specified the ceilings, walls and floor as line segments.

A line has a winding, or normal; you can determine the direction that a line should be solid from. And I want the line to be non-existent when passing from behind through it - I want one-way platforms (and ceilings you can fall through but not jump back up out of).

Here's an example level, with yellow for ceilings and red for floor (with winding determining their facing, they don't differ in implementation):

enter image description here

I want to move to box2D (Javascript). How can I best port across these line segments and make them one-way static barriers in box2D?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Box2D has functionality called a "Pre-Solve Event".

Pre-Solve Event

This is called after collision detection, but before collision resolution. This gives you a chance to disable the contact based on the current configuration. For example, you can implement a one-sided platform using this callback and calling b2Contact::SetEnabled(false). The contact will be re-enabled each time through collision processing, so you will need to disable the contact every time-step. The pre-solve event may be fired multiple times per time step per contact due to continuous collision detection.

If you search the box2d user manual for "Pre-Solve" you'll find an example for implementing one-way platforms:

void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
    b2WorldManifold worldManifold;
    contact->GetWorldManifold(&worldManifold);
    if (worldManifold.normal.y < -0.5f)
    {
       contact->SetEnabled(false);
    }
}

As for what collision object to create, Edge Shapes fit the bill, again from the manual:

Edge shapes are line segments. These are provided to assist in making a free-form static environment for your game.

They are simple to create, as you would expect, they're created from two points:

// This an edge shape.

b2Vec2 v1(0.0f, 0.0f);
b2Vec2 v2(1.0f, 0.0f);

b2EdgeShape edge;
edge.Set(v1, v2);

Combine those two together and you have static one-way barriers.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.