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