Reputation: 6166
I want to add a CC3Node on a tap. However, the code I'm using produces a strange thing. My object is placed accordingly to the touch location, but on the third quadrant (such that my screen would be divided in 4)
Here's an example of the problem:
Just to sum up, when I clicked at the center of the white square, I got the hello world put at the center of the quadrant, etc.. When I clicked in the top right corner, I got the purple hello world that overlaps the white square.
Here's the code I'm using:
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 is the final coordinate that I set in my node using the .location attribute.
My layer is receiving the gesture and does the following:
-(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];
}
}
If I don't convert first using this method: cc3ConvertUIPointToNodeSpace
, then I get the up and down tap inversed.
I verified that the size of the screen returned in winSz is indeed the correct size (2048, 1320).
Also, the tp3D x and y are alway negative, which is strange... the code comes from here: http://www.cocos2d-iphone.org/forum/topic/14790
Upvotes: 0
Views: 218
Reputation: 479
Did you look at CoCos3d DemoMash, it has explanation and code for all the features of cocos3d,and what you are trying to do , is already implemented there .
Upvotes: 1