0

I am trying to develop a game using openGL where i have used GLTextureLoader class to load images and these sprites are moving from left to right with some calculated velocity , i need to detect touch on these images.

genpfault
  • 51,148
  • 11
  • 85
  • 139
ankit
  • 113
  • 2
  • 8
  • please search on stackoverflow, this questions has been asked several times. (try color coding for example) – Pochi Jul 26 '12 at 07:01
  • @LuisOscar this is one [link](http://www.iphonedevsdk.com/forum/iphone-sdk-development/42432-detecting-touches-on-an-opengl-texture.html) which i saw and was trying to follow but i don't have the rect for the texture, so could not use it, please guide me to a right direction – ankit Jul 26 '12 at 07:19

3 Answers3

1

Since your purpose is very simple, all you have to do is draw whatever object you have twice, the visible one and another one with just color on an invisible buffer. then you check for the location where the user pressed in the invisible buffer, see what color it is and there you have your object.

http://www.lighthouse3d.com/opengl/picking/index.php3?color1

That is the basic theory.

Pochi
  • 13,391
  • 3
  • 64
  • 104
0

OpenGL is a rendering API. It only draws stuff. Techniques like lighthouse3d do work, but glReadPixels is slow.

You should check this on the CPU; that is, for each drawn sprite, test if the touch position is inside.

Calvin1602
  • 9,413
  • 2
  • 44
  • 55
0

I found out how to do it ,as per my requirement , As i said i am not the expert in openGL but managed to do it some way around.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
CGPoint touchLocation = [touch locationInView:self.view];
touchLocation = touchLocation = CGPointMake(touchLocation.x, 320 - touchLocation.y);
//self.player.moveVelocity = GLKVector2Make(50, 50);
//NSLog(@"Location in view %@", NSStringFromCGPoint(touchLocation));
//NSLog(@"bounding box is %@",NSStringFromCGRect([self.player boundingBox]));

GameSprite *temp;
for (GameSprite *tempSprite in self.children) {
    if (CGRectContainsPoint([tempSprite boundingBox], touchLocation)) {
        NSLog(@"touched the player");

        temp =tempSprite;
    }  
}

[self.children removeObject:temp];

}

- (CGRect)boundingBox {
CGRect rect = CGRectMake(self.position.x, self.position.y, self.contentSize.width, self.contentSize.height);
return rect;
}
ankit
  • 113
  • 2
  • 8