Search code examples
cocos2d-xcocos2d-x-3.0chipmunk

Connecting physics bodies with PhysicsJoint


Lately I realized that Chipmunk (am I right?) was integrated with >= Cocos2d-x 3.0. Now I am trying make use of it (I am using cocos2d-x 3.2).

So can you tell if it's possible to connect the bodies (PhysicsBody) with PhysicsJoint (eg. PhysicsJointSpring), but with connection points being placed for example on the edge of the body?

I saw the "Joints tests" from the package with library, but it looks like all of them (joints) are placed in the midlle of the body - you could say that anchor point is placed at Vec2(0.5f, 0.5f).

Test are here (PhysicsDemoJoints class): https://github.com/cocos2d/cocos2d-x/blob/v3/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp

Here is an example what I am talking about (two balls, left bottom corner): https://www.youtube.com/watch?v=ZgJJZTS0aMM

I would appreciate any help! And give you an internet hight five as a gift!


Solution

  • In such cases take a look at the class reference.

    You'll find that joints can be created with constructors similar to this one:

    static PhysicsJointDistance *   construct (PhysicsBody *a, PhysicsBody *b, const Vec2 &anchr1, const Vec2 &anchr2)
    

    The two vectors are the joint anchor points. Unfortunately it's not documented what they are, but typically they'll be in world (scene) coordinates.