How to find world position of a physics contact? Cocos2d-x 3.17.2 (chipmunk)

I’m trying to find the world position of a given collision between 2 physics bodies. I can get a position from the PhysicsContactData object that is passed to the onContactBegin() callback, but the coordinates don’t seem to be correct for the world.

Here’s some example code to get and print the PhysicsContactData:

bool GameWorld::onContactBegin(PhysicsContact& contact)
    const PhysicsContactData* contactData = contact.getContactData();
    CCLOG("Contact data: num: %d, x: %f y: %f", contactData->count, contactData->points[0].x, contactData->points[0].y);

There is only ever 1 point in the contactData->points[] array, and it’s a long way from the world position of the player object it’s colliding with. For example, here’s a comparison of the player’s world position vs. the contactData position (player object size is roughly 50x80):

Player: 777.291016, -44.964111
Contact: 1059.727783, 526.777527

Player: 777.291016, -44.964111
Contact: 1060.115723, 524.990051

Player: 805.231506, -235.684113
Contact: 965.624634, 475.454651

Player: 999.437500, -222.059448
Contact: 1016.683411, 576.433777

I’m hoping there’s some kind of transformation I can do from the contact point to get it to make sense in the game world.


Hi there this is Box2d What is your Scaling set to, please Rember you need function’s scaling functions in be converted.

You need a ScaleToWorld() and a WorldToScale() function to get correct postions.
You must set up your scaling in box2d so if i was running a tilemap of 32x32px use 32
set bodx2d to 32 then thats your sclaing now the functions.

#define PTM_RATIO 32.0f
#define SCREEN_TO_WORLD(value) (float)(value)/PTM_RATIO
#define WOLRD_TO_SCREEN(value) (float)(value)*PTM_RATIO
so when you put
sprite-setPostion( SCREEN_TO_WORLD(newPosition.x), SCREEN_TO_WORLD(newPosition.y));
This will scale down to Box2d at 32 then just get it back with WOLRD_TO_SCREEN to sclae up again.

Heres a quick tut from this guy pay attntion to how the sclaing work and you will realy get the hang of box2d.

search Youtube for “Introduction to Box2D - The Nature of Code”

Thanks very much for taking the time to reply, but it’s chipmunk I’m using. It’s the default integrated physics engine for 3.17.2. I’ve updated the post title to make that clear for anyone who isn’t aware :slight_smile:

I think 1 point is ok (it is the first contact) :wink:

Yep there’s no problem with it being just one point. The problem is position doesn’t seem to be correct, or doesn’t make sense anyway!

Yes yes Sorry, did not see chipmunk on End of formum label, but this was a problem for me as well. Witch is why i swiched to Box2d I did not undersatnd the scaling and found out the was no way to set it in cocos2dx. had some bug in setting up chipmunk, I think when i traced the code back but cocos2dx function it only wanted to set box2d and not chipmunk scaling when setting it up maybe this mite be your problem too. it mite also be that i need to add that CHIPMUNK MACRO when compiling. but i dont know. I had poblem setting it becos i want 64x64 found no way to set it. gave up then went box2d.

Which platform works not correct?

Ah I see! Unfortunately my game is finished so I’m stuck with chipmunk. I’m trying to add a new feature.

I’m using chipmunk, which is the default with cocos2d-x 3.17.2. I’m compiling for win32, visual studio 2019.

You useing the physics Class which is using Chipmunk2D.

That’s pretty much what I said isn’t it?

Switch to Physics with Box2D

or use a working well fork of Cocos2d-x 4.0:
adxeproject/adxe: This is another more radical fork of Cocos2d-x-4.0, use OpenAL for all platforms, single texture multi GPU texture handler, C++14/17… (

Thanks but as I already mentioned, my game is finished so I don’t want to completely change the physics engine for a single feature. Would upgrading to Cocos 4.0 definitely fix the issue anyway?

Im not sure. Is it a issue?
Or use you the wrong method?

That’s what I’m asking :grin: