0
我想在水龙头上添加CC3Node。但是,我使用的代码产生了一个奇怪的事情。我的目标相应地放置在触摸位置,但在第三象限(例如,我的屏幕会在4分)设置CC3Node在触摸位置的位置
这里的问题的一个例子:
只是总结一下,当我点击在白色方块的中心,我把你好的世界放在象限的中心等等。当我点击右上角时,我得到了与白色方块重叠的紫色问候世界。
下面是我使用的代码:
CGSize winSz = [[CCDirector sharedDirector] winSizeInPixels];
GLfloat aspect = winSz.width/winSz.height;
// Express touchPoint X & Y as fractions of the window size
GLfloat xtp = ((2.0 * touchPoint.x)/winSz.width) - 1;
GLfloat ytp = ((2.0 * touchPoint.y)/winSz.height) - 1;
// Get the tangent of half of the camera's field of view angle
GLfloat effectiveFOV = ((ViewInterface*)[ViewInterface sharedViewInterface]).currentScene.activeCamera.fieldOfView/((ViewInterface*)[ViewInterface sharedViewInterface]).currentScene.uniformScale;
GLfloat halfFOV = effectiveFOV/2.0;
GLfloat tanHalfFOV = tanf(DegreesToRadians(halfFOV));
// Get the distance from the camera to the projection plane
GLfloat zCam = ((ViewInterface*)[ViewInterface sharedViewInterface]).currentScene.activeCamera.globalLocation.z;
// Calc the X & Y coordinates on the Z = 0 plane using trig and similar triangles
CC3Vector tp3D = cc3v(tanHalfFOV * xtp * aspect * zCam,
tanHalfFOV * ytp * zCam,
0.0f);
tp3D是最终的坐标,我使用.location属性在我的节点设置。
我的层接收到手势并执行以下操作:
-(void) handleTapSelection: (UITapGestureRecognizer*) gesture {
if ([self cc3ValidateGesture: gesture] && (gesture.state == UIGestureRecognizerStateEnded)) {
CGPoint touchPoint = [self cc3ConvertUIPointToNodeSpace: gesture.location];
[[StateInterface sharedStateInterface] tapDetected:gesture inView:nil locationInView:touchPoint];
//[self.mashUpScene pickNodeFromTapAt: touchPoint];
}
}
如果我不转换首先使用这种方法:cc3ConvertUIPointToNodeSpace
,然后我得到了上下自来水反转。
我验证了在winSz中返回的屏幕大小确实是正确的大小(2048,1320)。
此外,tp3D x和y时总是负的,这是奇怪的...代码来自这里:http://www.cocos2d-iphone.org/forum/topic/14790