cocos2dx-3.10 covertToWorldSpace result is wrong when using OrbitCamera

62 Views Asked by At

cocos2dx-3.10 covertToWorldSpace result is wrong when using OrbitCamera devices test on:Windows Steps to Reproduce: local size = cc.size(400, 400)

local layer = cc.LayerColor:create(cc.c4b(200, 0, 0, 255), size.width, size.height)
layer:setPosition(display.cx - size.width, display.cy - size.height)
self:addChild(layer)

local label = cc.Label:create()
label:setSystemFontSize(30)
label:setSystemFontName("Arial")
label:setString("XXX")
label:setColor(cc.c3b(0, 255, 100))
label:setPosition(size.width * 0.5, 0)
layer:addChild(label)

local lsize = label:getContentSize()
local action = cc.OrbitCamera:create(1, 1, 0, 0, 70, -90, 0)
local callFunc = cc.CallFunc:create(function()
local worldPos = label:convertToWorldSpace(cc.p(lsize.width * 0.5, lsize.height * 0.5))

label = cc.Label:create()
label:setSystemFontSize(30)
label:setSystemFontName("Arial")
label:setString("AAA")
label:setColor(cc.c3b(100, 0, 100))
label:setPosition(worldPos)
self:addChild(label)
end)
layer:runAction(cc.Sequence:create(action, callFunc))

enter image description here

1

There are 1 best solutions below

0
tkzcfc On

I found a solution:

local mat4 = label:getNodeToParentTransform(self)
        local vec4 = {x = lsize.width * 0.5, y = lsize.height * 0.5, z = 0, w = 1}
        -- convert to world space (we need to calculate its Z-value)
        local pos = mat4_transformVector(mat4, vec4, cc.mat4.createIdentity())
        -- convert GL-screen-space coordinates
        local worldPos = cc.Camera:getDefaultCamera():projectGL(pos)

enter image description here