Bug in Physics? (Cocos2d-x 3.0 beta 2)

Bug in Physics? (Cocos2d-x 3.0 beta 2)
0.0 0


Has anyone noticed bugs in Physics? Everytime I create a sprite with a physicsBody i have problems with rotation; moreover, when i try to move the layer that contains the sprite, the physics body doesn’t move with the sprite!
Is this a bug or is it me? Has anyone else noticed something similar?


Just to give you an example:
#include “HelloWorldScene.h”


Scene* HelloWorld::createScene()
// ‘scene’ is an autorelease object
auto scene = Scene::createWithPhysics();
// ‘layer’ is an autorelease object
auto layer = HelloWorld::create();

// add layer as a child to scene

// return the scene
return scene;


// on “init” you need to initialize your instance
bool HelloWorld::init()
// 1. super init first
if ( !Layer::init() )
return false;

Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();

// 3. add your codes below...

auto sprite = Sprite::create("fly1.png");

// position the sprite on the center of the screen
sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
hero = sprite;
hero->setPhysicsBody(PhysicsBody::createEdgeBox(Size(66,46) ));

// add the sprite as a child to this layer
this->addChild(sprite, 0);


return true;

HelloWorld::update(float dt){


First of all let me say this from the get-go that i don’t know how to use cocos2dx-3.0 (still waiting for the final stable release) and just a little bit of 2.x, and certainly don’t know how 3.0 integrate physics but it seems base on your code that they incorporate it inside a sprite class, i might be wrong.
if you may allow me to share some concepts may or may not be explicitly related to cocos2dx but just in general.
graphics rendering pipeline is a solo framework that deals with the displaying of colors to a pixel it is separate from a physics framework that deals solely with the computation of physics’ force equations which output are final positions after considering all internal and external variables and from there you can take those positions and render something. that is why you can incorporate different physics api with different rendering api. so you depend your rendering positions from physics not the other way around.
Now to your prob. I don’t know if you can control the physics positions from the rendering side in cocos2dx but if that’s possible it would create a weird teleporting effect (i don’t know if that’s your goal) where the physics world is stepping making the ball bounce vertically and suddenly you change it’s position which is not realistic. So many would advice if you want it to change position you’ll introduce a force and let the physics world do the iteration for a smooth animation.

The physics body moves away from the sprite while sprite is performing an action

Hi, thank you for your answer.
I’ll explain you the problem better: in Cocos2dx 3.0 physics is implemented through the class PhysicsBody. You can create PhysicsBody-es and a PhysicsWorld, and they store the position of the body, velocity, gravity etc, everything that a Phyisics engine should.
You can attach a physics body to a Sprite, giving the pointer to the PhysicsBody to the Sprite through the setPhysicsBody method. Once you attached the physics body to the sprite it is supposed to move with it. The problem is that it doesn’t always happen. In fact, you can see the position of the physics body highlighted through this method of the class PhysicsWorld:
In detail, when i move the layer containing the sprite with a physics body, it goes away without the sprite.
I can’t understand wether I’m doing something wrong or it is a bug


well thank you too for further explaining the issue.
but correct me if im wrong, so what you’re trying to say is that if you move the sprite the physics body should follow along?

Sorry about asking again i was just confused when you say that you move a sprite and the body doesn’t move along bec. as far as i know the sprite should follow the body and not the other way around although that would be possible.
I just scan the reference/documentation and I looked again at your code, i notice that you create your sprite using the plain Sprite class which i believe doesn’t have the feature you want. But there is this PhysicsSprite class derived from Sprite class that has that feature where if you set the position/rotation of the sprite manually the body will be updated. You may want to change that and see what the effect would be and pls. inform us too. thanks


Looking at the source code and at the documentation of cocos2d-x v3.0, there is no PhysicsSprite… There is just a Sprite class which behaves differently, depending on wether it has ha physics body or not. So I’ve understood (and, moreover, when i try to declare a PhysicsSprite I have a compilation error - non-existing symbol)


This is the link sir:
i believe it’s an extension under cocos2d::extension::PhysicsSprite


I can’t access it. “no member named extension in namespace cocos2d”


did you include the extension file before accessing the namespace?
Im not sure if it’s cocos-ext.h or something.