Hi. I am working on learning Cocos2d-x (v3.17.2) by programming the classic artillery game (2 cannons fire shells in hyperbolic arc at given angle and force - simple but covers basics of IO, sprites, etc.).
When my falling sprite’s edge (gravity physics) hits edge I placed on view, program aborts in source ccPhysicsBody.cpp L376:
…
void PhysicsBody::setPosition(float positionX, float positionY)
{
cpVect tt;
tt.x = positionX + _positionOffset.x;
tt.y = positionY + _positionOffset.y;
cpBodySetPosition(_cpBody, tt); // <--ABORT HERE
}
…
Based on test cpp and internet I have:
- Created scene and a sprite
- Added physic and gravity to scene and sprite - which now convincingly start falling and accelerating.
- Added an edge box to view like test cpp, and edge box to sprite
- Enabled debugging for physics and edges - I see box around sprite and view as expected.
- When sprite hits view edge, the sprite edge box goes a few pixels across view edge, and I get a program abort in void PhysicsBody::setPosition(float positionX, float positionY)
Can anyone offer suggestions to why collision leads to abort in physics code? What I missed / should change?
Note, if I remove the edge from sprite physics body it falls off screen as expected without crash, so I think problem is about physics and edges, not going off-screen.
My Code in scene init (some removed for clarity):
…
initWithPhysics();
getPhysicsWorld()->setGravity(Vec2(0, -980));
//Create edge around view
auto node = Node::create();
node->addComponent(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size));
node->setPosition(VisibleRect::center());
this->addChild(node);
getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); //show collision boxes
//add cannon class’ sprite to scene
this->addChild(_cannon1.Sprite(), 2);
//add physics to sprite
auto pb = PhysicsBody::create();
pb->addShape(PhysicsShapeEdgeBox::create(_cannon1.Sprite()->getContentSize()));
_cannon1.Sprite()->setPhysicsBody(pb); //need edge or cannon falls through ground - if not here no crash
//place in center to fall
_cannon1.Sprite()->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->scheduleUpdate();
return true;
…