Opengl Iphone SDK: How to tell if you're touching an object on screen?
Posted
by TheGambler
on Stack Overflow
See other posts from Stack Overflow
or by TheGambler
Published on 2010-05-24T02:17:11Z
Indexed on
2010/05/24
2:20 UTC
Read the original article
Hit count: 368
First is my touchesBegan function and then the struct that stores the values for my object. I have an array of these objects and I'm trying to figure out when I touch the screen if I'm touching an object on the screen.
I don't know if I need to do this by iterating through all my objects and figure out if I'm touching an object that way or maybe there is an easier more efficient way.
How is this usually handled?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
UITouch* touch = ([touches count] == 1 ? [touches anyObject] : nil);
CGRect bounds = [self bounds];
CGPoint location = [touch locationInView:self];
location.y = bounds.size.height - location.y;
float xTouched = location.x/20 - 8 + ((int)location.x % 20)/20;
float yTouched = location.y/20 - 12 + ((int)location.y % 20)/20;
}
typedef struct object_tag // Create A Structure Called Object
{
int tex; // Integer Used To Select Our Texture
float x; // X Position
float y; // Y Position
float z; // Z Position
float yi; // Y Increase Speed (Fall Speed)
float spinz; // Z Axis Spin
float spinzi; // Z Axis Spin Speed
float flap; // Flapping Triangles :)
float fi; // Flap Direction (Increase Value)
} object;
© Stack Overflow or respective owner