[cocos2dx]从a点移动到b点

convertToWorldSpace用于将一个相对坐标转化为绝对坐标,convertToNodeSpace则是将一个绝对坐标转化为一个相对坐标。
这里的相对坐标指的是相对于某一个CCNode的坐标(CCNode左下角为0,向右向上为正),这里的绝对坐标是相对于屏幕的坐标(屏幕左下角为0,向右向上为正)。

源码

// 把世界坐标转换到当前节点的本地坐标系中
Vec2 Node::convertToNodeSpace(const Vec2& worldPoint) const
{
    Mat4 tmp = getWorldToNodeTransform();
    Vec3 vec3(worldPoint.x, worldPoint.y, 0);
    Vec3 ret;
    tmp.transformPoint(vec3,&ret);
    return Vec2(ret.x, ret.y);
}

// 把基于当前节点的本地坐标系下的坐标转换到世界坐标系中
Vec2 Node::convertToWorldSpace(const Vec2& nodePoint) const
{
    Mat4 tmp = getNodeToWorldTransform();
    Vec3 vec3(nodePoint.x, nodePoint.y, 0);
    Vec3 ret;
    tmp.transformPoint(vec3,&ret);
    return Vec2(ret.x, ret.y);

}
Vec2 Node::convertToNodeSpace(const Vec2& worldPoint) const 
{
    static Mat4 mat;
    mat.setIdentity();
    auto parent = getParent();
    if (parent)
    {
        mat = parent->getWorldToNodeTransform() * _attachBone->getWorldMat() * Node::getNodeToParentTransform();
    }
    else
    {
        mat = _attachBone->getWorldMat() * Node::getNodeToParentTransform();
    }
    return mat;
}

从a点移动到b点

void Test::doAnim()
{
	//这里必须是世界坐标
    Vec2 posWorld = mapWidget["txtCoin"]->getWorldPosition();

    //控件txtCoin与控件nodeImg在同一个坐标系下的位置
    Vec2 pos = mapWidget["nodeImg"]->getParent()->convertToNodeSpace(posWorld);
    mapWidget["nodeImg"]->runAction(Sequence::create(MoveTo::create(0.75, pos), CallFunc::create([&](){

    }), NULL));
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值