PhysicsWorld getSHapes function Crash

Hello,

In the scene update function, I am generating a random objects to appear, like boxes, then decided to improve the behavior by checking if there’s any other object in the random point I generated to avoid placing the boxes on top of any other objects.
here’s what I did :

cocos2d::Size visibleSize = Director::getInstance()->getVisibleSize();
cocos2d::Vec2 startPos(random(0, (int)visibleSize.width), random(0, (int)visibleSize.height)); //place of the box
if (!this->getPhysicsWorld()->getShapes(startPos).size() > 0) //this line crashes
//logic to generate

stepping into the getShapes function :
Vector<PhysicsShape*> PhysicsWorld::getShapes(const Vec2& point) const
{
Vector<PhysicsShape*> arr;
cpSpaceNearestPointQuery(_cpSpace,
PhysicsHelper::point2cpv(point),
0,
CP_ALL_LAYERS,
CP_NO_GROUP,
(cpSpaceNearestPointQueryFunc)PhysicsWorldCallback::getShapesAtPointFunc,
&arr);

return arr;

}

The cpSpaceNearestPointQuery function is the one that crashes with the following exception :
Unhandled exception at 0x10069350 (libcocos2d_v3.4_Windows_8.1.dll) in Game.Windows.exe: 0xC0000005: Access violation reading location 0x00000020.

Could you tell me if I am doing something wrong ?

Thanks.

So… this->getPhysicsWorld() == nullptr? Did you create the scene using createWithPhysics()?

1 Like

Good question, actually I created the scene with CreateWithPhysics, however, my scene, which happen to be a layer in the scene, is created with Create only.

auto scene = Scene::createWithPhysics();
auto layer = FieldScene::create();

I haven’t noticed this before as all the sprites physics bodies or physics logic was handled using setPhysicsBody and getPhysicsBody, which handle the switching between the scene and layer objects internally.

Anyway, things worked fine when I called physicsWorld from the getScene function like this :
this->getScene()->getPhysicsWorld()->getShapes(startPos).size()

Thanks @trojanfoe