Opengl Iphone SDK: how do I know if you are touching an object on the screen?

First, my touchhesBegan function, and then the structure that stores the values ​​for my object. I have an array of these objects, and I'm trying to understand when I touch the screen, if I touch an object on the screen.

I don’t know if I need to do this by repeating all my objects and figuring out if I am accessing the object this way or, perhaps, a 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; 
+2
source share
1 answer

There are several ways to do this.

  • You can additionally draw a scene with all solid drawn objects using a unique color, then read the color of the selected pixel using glReadPixels and associate it with the object.

  • Use something like unproject to get the ray in the scene, and then find the intersection of the rays of the triangle. The non-project function as well as the intersection function of the rays of the triangle are in the glm library, which is ideal for developing open gl.

    / li>
+4
source

Source: https://habr.com/ru/post/1314423/


All Articles