Cocos2d-x Node-Sprite Anchor Points and Removing PhysicsBody Bugs

After edit: The anchor point problem has nothing to do with PhysicsBody. Sprite objects don’t respect the anchor point when I add a new child. There isn’t the same problem with node objects, only sprites.

When you add a PhysicsBody component in a node;

-If you add a child to this physicsbody node, the new added node’s local position isn’t center of the parent node. The position is wrong. And there is this problem for only PhysicsBody nodes.

-If you call removeComponent for remove physicsbody, return “EXC_BAD_ACCESS” error.

So PhysicsBody added nodes don’t behave like default nodes.

Please try it (I tried these in 3.10 and 3.11 versions)

Hi there. What can I do about that? I tried v3.11.1, it didn’t change.

I’m telling you what I did;

-Created “box” named node and added a PhysicsBody component with the a box collider shape, and setted dynamic is true.
-There was an “arrow” named sprite in the scene. And I added the arrow to “box” named node in the runtime. And I setted position Vec2(0,0)
-“arrow” named sprite position wasn’t center of the box. It added to bottom-left position of the box.

Please help me. :frowning:

you should show some code. What you write that you did and what you coded may be different.

//Box code in here
Sprite *box =Sprite::create("Sprites/box.png");
physicsBody=PhysicsBody::createBox(Size(32,32),PhysicsMaterial(0.1f,0.0f,1.0f));
box->setPhysicsBody(physicsBody);

//I'm creating new sprite in the runtime;
Sprite *arrow=Sprite::create("Sprites/arrow.png");
box->addChild(arrow);
arrow->setPosition(0,0);

But arrow named sprite isn’t at the center. It’s adding at bottom-left of the box.

I checked box->getPosition()…etc everything is true until adding an sprite as a child.

this is not center, it is bottom left.

Perhaps also take a look at this

but;

objects have a default anchor point of is (0.5, 0.5).

So, If I don’t add PhysicBody in the node, there isn’t the same problem.(Anchor point at the center) And I if I tried that code;

arrow->setPosition(box->getPosition());

Arrow sprite position was positioning to center of the box. The problem is addChild() with PhysicsBody.

I don’t understand what you mean here.

Sorry for my english :frowning: I said it’s a bug. Because if I don’t add PhysicsBody component to a node, I don’t live any problem about that.

It’s writed your sended document “objects have a default anchor point of is (0.5, 0.5).”

Yes, new node anchor points are already 0.5 and 0.5 as a default.(it mean center point) And my codes working well for nodes If I don’t add PhysicsBody.

And did you try remove PhysicsBody component? I’m getting “EXC_BAD_ACCESS” error in the runtime. It’s another bug.

Make me a sample in “HelloWorld.cpp” and send me the .cpp and .h that I can drop into Xcode and test. That way I can see exactly what you are doing. Perhaps I see something. If not I will also use this example to file a bug report.

Ok, I tested with a new project. In fact, it has nothing to do with PhysicsBody. All about nodes and sprites problem.

I’m sharing this file for you.(HelloWorldScene.cpp) You will see that problem. You may use default HelloWorldScene header file with that. The official document is saying “objects have a default anchor point of is (0.5, 0.5).” But it’s not true for sprite objects for now. And setAnchorPoint doesn’t work for sprite objects.

I think, it’s very important problem.

Another bug, I said that in the number 2; I can’t remove PhysicsBody component. I’m always getting “EXC_BAD_ACCESS” error.

Sample HelloWorldScene.cpp file.

Yes, I faced similar issue many months back. But had my way figured out using an hack.
Roughly, the issue was the anchor points don’t behave as (0.5,0.5) if there is hierarchy of child nodes!
where for child nodes, positioning becomes difficult. I don’t rem. exactly what was that but yes, I also faced similar issue… However, I am not sure if physics has to do anything with this issue.

1 Like

Nope. You’re right, it has nothing to do with physics, I tried again with empty project.

The problem is about that, the sprite objects doesn’t respect anchor point when I add a new child object. I think it’s a bug. But If I try that with node objects, no problem.

Physics bug is that, I can’t remove PhysicsBody component from an node/sprite object. I’m getting crash error.

Aah! finally someone agrees with me that there is problem in if we’ve child nodes then they don’t respect anchor as (0.5,0.5)… Though I forgot at which hierarchy level they stop respecting it.
Can you please make a dummy project displaying both the things in the same scene and tag @slackmoehrle so that he can take this issue further to the devs.
I can’t create the issue bcoz I forget exactly when does it happen, it’s been an year since I did that.

Physics bug!
Also, are you trying to remove the physics body in collision listener?
You can remove the physics body at the instant when physics is doing calculations on it. Just look for ‘how to remove physics body …’

What’s the crash message by the way?

1 Like

I shared an sample cpp about the physics remove bug. (I tried that with removing listeners)

And other issue, I think it’s a bug. I hope it will be fixed next version.

Note: I’m sorry for my broken english :frowning:

Hi,

I remember I faced this issue in March, 2015
I raised this issue in forums but no one responded and issue got submerged.

So, just create a dummy project for the anchor point issue with child nodes…And tag @slackmoehrle otherwise, this will again get forgotten for another few more years

I am setting the environment for cocos2d-x in my new laptop and I am having some issue with cocos command… So can’t check your sample cpp right now.

1 Like

Alright, let me have a look see.

1 Like

Any improvement?

I’m taking a look at this. however all the links provided in this thread (and here https://github.com/cocos2d/cocos2d-x/issues/15812 ) are not working now… probably they are expired.

could someone post the smallest code to reproduce the bug? thanks.

After reading the thread and the github issue, I think I understand the problem.
Actually anchorPoint is used for transform properties. it will affect:

  • position
  • scale
  • rotation

but it won’t affect its children. the children is always added relative to the 0,0 parent’s coordinate.
Decoupling the “transform anchor point” from the “children anchor point” is a design decision. Not sure if was the best decision I’ve made, but it is a design decision… and not a bug. Perhaps we need better documentation.

This feature caused lot of confusion in cocos2d-iphone. In hindsight, I would have associated transform anchor point with children anchor point, but this is something that we can’t change without breaking compatibility in thousands of games.