I am making game in cocos2d-x, but it's the first time I am using a physics engine i.e. Chipmunk. (It's built-in to cocos2d-x.)

I want to make a one-way platform: When my character jumps into a platform from below, they should not collide, but when falling on to one from above, they should collide. Then the player could jump up onto platforms from below, like this:

what should happen


I think I can help you someway. In Cocos2dx we could handle collision event with EventListenerPhysicsContact.

check out this code:

auto character = Sprite::create("chacracter.png");
auto body = PhysicsBody::createCircle(sprite->getContentSize().width / 2);

auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin= [=](PhysicsContact &contact) -> bool 
    // We we handle what happen when character collide with something else
    // if we return true, we say: collision happen please. => Top-Down Char Jump
    // otherwise, we say the engine to ignore this collision => Bottom-Up Char Jump
    auto character = contact.getShapeA()->getBody()->getNode();
    if( character.isJumpingUp() )
            return false;
            return true;
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, character);

In contactListener->onContactBegin we handle what should Chipmunk do when chcarcter collide with collision line. sometimes ignore, sometimes not.

Hope Help You

| improve this answer | |
  • \$\begingroup\$ i already implemented like this only but i was not sure whether my approach is correct or not. thanks. \$\endgroup\$ – smit patel Nov 26 '14 at 14:26

I'm assuming that the physics engine for cocos2d-x already has this ability and you will just need to dig a little bit more.

Otherwise, I'm also thinking of trying to implement something similar myself for a mockup game I'm trying to create. Right now (not sure whether it's a good idea or not) I'm thinking of adding a new property that determines what kind of collision can apply to a certain object. And depending on the velocity/accel (whatever your movement indicator is defined as) will be the check for this collision.

You can also check this out: http://www.wildbunny.co.uk/blog/2011/12/14/how-to-make-a-2d-platform-game-part-2-collision-detection/

It has some in-depth explanation of how to implement a kind of collision detection and it also includes your question.

Update: From my understanding... the key conditions for a one-way platform are:

  • if the velocity of the object is positive, ignore the collision.
  • if the base of the object's position is less than the position of the top of the platform, ignore the collision.

You will need to determine what kind of conditions are required for a one-way platform.

On another note, I went and took a quick look into Chipmunk and it looks like one-way platform is very trivial.

Here's a code snippet from the Demo Chipmunk provides:

// Add our one way segment
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f));
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);
cpShapeSetCollisionType(shape, 1);
cpShapeSetLayers(shape, NOT_GRABABLE_MASK);

// We'll use the data pointer for the OneWayPlatform struct
platformInstance.n = cpv(0, 1); // let objects pass upwards
cpShapeSetUserData(shape, &platformInstance);

//at the very end
//preSolve -> looks like checking the direction of the object with a normal vector
//and fails the collision if the direction of the normal is negative
  cpSpaceAddCollisionHandler(space, 1, 2, NULL, preSolve, NULL, NULL, NULL);

The file is called OneWay.c https://github.com/slembcke/Chipmunk2D/blob/master/Demo/OneWay.c

| improve this answer | |
  • \$\begingroup\$ thanks for the link, i will look into it. and for category wise collision detection is already there in chipmunk. chipmunk has 3 methods for that, setCategoryBitmask(int bitmask) setContactTestBitmask(int bitmask) setCollisionBitmask(int bitmask) check out this link: chipmunk-physics.net/release/ChipmunkLatest-Docs/… \$\endgroup\$ – smit patel Sep 25 '14 at 8:33
  • 1
    \$\begingroup\$ -1 not really an answer. I should be able to read your answer and implement a solution. If you wan't to link to a tutorial or guide please summarize the relevant parts so that the answer is useful if the link is ever broken. \$\endgroup\$ – ClassicThunder Sep 25 '14 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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