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

@pococogames
@ricardo
Thanks for the clarification on anchor points.
Can we have a custom function(function overloading) which takes additional parameters for x and y where each valuse ranges from 0-1. Based on this, we can decide where on anchor point we want to place the child!.

Eg. If I do, parentSprite->addChild(childSprite, 0.5,0.5) then it will add the child in the center of the parent Sprite.

1 Like

Thanks @ricardo. I agree with @catch_up.

If I add a sprite to a node object just like that;

auto mySprite=Sprite::create("sampleSpriten.png");
myNode->addChild(mySprite);

I donā€™t have any problem. Because the sprite object anchor point was in the center.(Default anchor point 0.5 and 0.5) Iā€™m adding this sprite to a node object, zero point is always at center of sprite in the node object. But I canā€™t do that with when I add a child sprite object to a parent sprite object.

For example;

auto parentSprite=Sprite::create("parentImage.png");
auto childSprite=Sprite::create("childImage.png");  
parentSprite->addChild(childSprite);

If I use that, child sprite is always add at bottom left of the parentSprite. But the child sprite and the parent sprite anchor point values are already (0.5,0.5). And I canā€™t think that is logical.

Note: I have solved my own problem with that way. (Itā€™s like a joke :smiley: )

childSprite->setPosition(parentSprite->getPosition());
Vec2 deltaPos=parentSprite->convertToNodeSpace(childSprite->getPosition());
parentSprite->addChild(childSprite);
childSprite->setPosition(deltaPos);
1 Like

So, we donā€™t say that anchorPoint doesnā€™t affect children: http://www.cocos2d-x.org/docs/programmers-guide/2/index.html#parent-child-relationship

I will change this text and publish an update.

I prefer not overload addChild, but you can add a free function that does that.
eg:

// might not compile... treat it as pseudo code
void addChild(Node* parent, Node* child, const Vec2& anchorPoint)
{
  auto s = parent->getSize();
  parent->addChild(child);
  child->setPosition(s * anchorPoint);  // you can't multiply Vec2 with Size, but you get the idea
}

yes, we should say that anchorPoint only affects the transform operations (scale, position, rotate, skew, etcā€¦) and does not affect children positioning. In fact, children will be always added to the bottom-left (0,0) corner of its parent

I need to be sure.

parentChild->getPosition() //center of  parentChild
parentChild->addChild(childSprite) added on the left-bottom corner

Do you think, itā€™s not a problem? Who want to add a child object to left-bottom corner point of a center aligned sprite object as a default? :frowning:

@pococogames

Hi pocoā€¦
Itā€™s not a problem. Itā€™s just the design which looks not normal :smiley: and also I think less people would be knowing about it. @slackmoehrle Can you please mention this in programmerā€™s guide if it is of significance.

@pococogames
If you feel, like me, that it should position in the center, then following things can be done:

1) Make a helper function in our code that will have this functionality and will automatically position the child according to our wish. Something like customaddChild() in our own sprite classā€¦ Note, you would have to extend this class from Sprite(obviously)

2) Or else you can directly add custom function inside Sprite class of the cocos2d-x library itself, build the library(if youā€™re using prebuild library).

3) If you think this above 2nd point is of significance and it should be present in the Node/Sprite class by default then discuss with ricardo/slackmoehrl/ @zhangxm and pull request to git can be sent.

:smile:

1 Like

Well, I will use my custom function for that. (But I still think itā€™s not reasonable :frowning: )

@ricardo Another problem, I get EXC_BAD_ACESS error when I call removeComponent() method for PhysicsBody objects.

Yes, it may/may not be. But as ricardo told, it is probably not the mistake but choice for designā€¦
So, the best thing cocos2dx offers is that it is open sourceā€¦ so you can edit the functionality any timeā€¦ like 2nd step, I told is the best pick in case you feel intrigued by implementing helper functionā€¦

1 Like

yes, you mentioned thatā€¦ could remind me how to reproduce it? thanks.

Iā€™m clarifying in a few places and will post an updated version in the AM.

What does ā€œAMā€ mean?

the morning time.

PhysicsBody *pb=PhysicsBody::createBox(Size(15,5),PhysicsMaterial(0.1f,0.0f,1.0f));
this->addComponent(pb);
this->removeComponent(pb); // I get EXC_BAD_ACCESS error

@pococogames
Are you sure thatā€™s how you remove the physics body?

This is also a way to do it.
someSprite->getPhysicsBody()->removeFromPhysicsWorld();

what is this ? what kind of object is that? thanks.

Node based object.

Yep. I get the same error. I tried. Iā€™m using this way in my project for now;

this->getPhysicsBody->setEnabled(false);

Itā€™s not problem for me , because this object will be removed next time in the scene. But If I canā€™t remove any object in my project, I need remove PhysicsBody component from the node object. (Imagine, maybe I need more performance optimization )

@pococogames
Itā€™s strange looking at your issueā€¦
Iā€™ll try our your code, probably day after tomorrow(sorry travelling somewhere, so taking time).

Meanwhile, please put your code to github or somewhere bcoz as @ricardo told your codeā€™s link is expired.
Iā€™ve done physics body removal in my prototype in v3.3. In case anything going wrong, Iā€™ll cross check.

1 Like

I already created an issue: https://github.com/cocos2d/cocos2d-x/issues/15932

and I can reproduce the crashā€¦ Iā€™m looking at it right now

UPDATE: crash fixed. will be part of v3.13

1 Like

Thanks. Itā€™s a good news. gby