Hello! I have a sprite3D inside a node which is inside another node. A camera is also in the setup and all nodes will rotate and move. How do I get the 2D screen coordinates (x, y) of the sprite3D?
1- Convert from Point 3D to Point 2D
Point HelloWorld::projectGL(cocos2d::Vec3 p)
{
return _camera->projectGL(p);
}
2- Convert from point 2d and z to Point 3d
Vec3 HelloWorld::unProjectGL(Point p, float z)
{
Vec3 near = Vec3(p.x, p.y, 0);
Vec3 far = Vec3(p.x, p.y, 1);
Vec3 n = _camera->unprojectGL(near);
Vec3 f = _camera->unprojectGL(far);
float x = (((z - n.z)/(f.z - n.z))*(f.x - n.x)) + n.x;
float y = (((z - n.z)/(f.z - n.z))*(f.y - n.y)) + n.y;
return Vec3(x, y, z);
}
Thank you so much! Though, how can I get the absolute 3D coordinates of a sprite3D? For example, if a sprite3D is inside a node at (0,0,0) and if I move the node to (100,0,0) the sprite3D still says it’s at (0,0,0) even though it really is at (100,0,0). Another thing is that if the sprite3D is at (100,0,0) inside a node and I rotate the node.
I figured out the problem by moving the camera instead of the nodes.