2D オブジェクトのみを含む OpenGL ES 2.0 シーンがあります。次の 2 つの行列を適用しています。
width = 600;
CC3GLMatrix * projection = [CC3GLMatrix matrix];
height = width * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-width/2 andRight:width/2 andBottom:-height/2 andTop:height/2 andNear:4 andFar:10];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix * modelView = [CC3GLMatrix matrix];
[modelView populateFromTranslation:CC3VectorMake(xTranslation ,yTranslation, -7)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
タッチ開始メソッドでは、タッチポイント座標を OpenGL ES 2.0 シーン座標にマッピングしようとしています。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches Began");
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self];
float differentialWidth = (768-width)/2; //Accounts for if OpenGL view is less than iPad width or height.
float differentialHeight = (1024-height)/2;
float openGlXPoint = ((touchPoint.x - differentialWidth) - (width/2));
float openGlYPoint = ((touchPoint.y - differentialHeight) - (width/2));
NSLog(@"X in Scene Touched is %f", openGlXPoint);
CGPoint finalPoint = CGPointMake(openGlXPoint, openGlYPoint);
for (SquareObject * square in squareArray) {
if (CGRectContainsPoint(stand.bounds, finalPoint)) {
NSString * messageSquare = (@"Object name is %@", square.Name);
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Touched"
message:messageSquare
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
}
}
このコードは、OpenGL 座標を返すという点で機能します。たとえば、画面の中央をクリックすると、0,0 が返されます。ただし、問題は (私が思うに) シーンのズーム スケールを何らかの形で考慮する必要があることです。150,0 の原点で描画されたオブジェクトは、iPad でクリックした場所 (112,0 を返す) と一致しないためです。上記のコードを使用します)。誰かがこれを修正する方法を提案できますか?
ありがとう !