cocos2dx 3.0 PhysicsBody Position!


#1

Hi

SpriteA is at (100,100)
SpriteB is child of SpriteA and is at (50,0)

If SpriteB->setPhysicsBody

this PhysicsBody’s Position is 50,0

But the SpriteB’s position is 150,0

How to fix it?

Thanks a lot


#2

I modify Node.cpp to fix this Issue…

And fix PhysicBody will not remove from world if Node has been release

This is correct??

Node::~Node()
{
    CCLOGINFO( "deallocing Node: %p - tag: %i", this, _tag );

    if (_updateScriptHandler)
    {
        ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler);
    }

    CC_SAFE_RELEASE(_actionManager);
    CC_SAFE_RELEASE(_scheduler);

    _eventDispatcher->cleanTarget(this);
    CC_SAFE_RELEASE(_eventDispatcher);

    // attributes
    CC_SAFE_RELEASE(_shaderProgram);
    CC_SAFE_RELEASE(_userObject);

    for (auto& child : _children)
    {
        child->_parent = nullptr;
    }

    removeAllComponents();

    CC_SAFE_DELETE(_componentContainer);

#if CC_USE_PHYSICS
    if (_physicsBody && _physicsBody->getWorld() != nullptr)
    {
        _physicsBody->removeFromWorld();
    }
    CC_SAFE_RELEASE(_physicsBody);
#endif
}

void Node::setPosition(const Point& newPosition)
{
    _position = newPosition;
    _transformDirty = _inverseDirty = true;

#if CC_USE_PHYSICS
    if (_physicsBody)
    {
        if(getParent())
            _physicsBody->setPosition(getParent()->convertToWorldSpace(newPosition));
        else
            _physicsBody->setPosition(newPosition);
    }
#endif
}

void Node::setPhysicsBody(PhysicsBody* body)
{
    if (_physicsBody != nullptr)
    {
        _physicsBody->_node = nullptr;
        _physicsBody->release();
    }

    _physicsBody = body;
    _physicsBody->_node = this;
    _physicsBody->retain();
    if(getParent())
        _physicsBody->setPosition(getParent()->convertToWorldSpace(getPosition()));
    else
        _physicsBody->setPosition(getPosition());
    _physicsBody->setRotation(getRotation());
}