简体   繁体   中英

android touch event in 3D cube or sphere

I want to touch a point (for example a red point or a image) on the surface of 3D earth or 3D cube (android opengl). After user touch the point,new activity wll be out. You know the touch event is x, y coordinate system of screen. the point in 3D earth or 3D cube is xy,z coordinate. How to know the touch point is match the point in 3D earth? or how to transform the xy(touch event) coordinate to xyz coordinate. please give me some hints or website about this!

There are two methods I can think of.

First, you can try to convert 2D screen coordinate to a 3D line in space and do 3d collision detection. Each pixel on screen maps to a line in 3D space. I never tried this but you can do something like that.

To convert 3d to 2d, you do something like this. I am not sure about math, I would be very happy if someone confirms this.

2d_coordinate = world_view_project * 3d_coordinate

If 3d_coordinate is [x, y, z, 1], 2d_coordinate becomes [x', y', depth, 1] if I am not mistaken. You can inverse world_view_project matrix and multiply it [mousex, mousey, 1, 1] and it should give you the 3d point that passes through mouse and is at the maximum distance you can see. Once you find it, you can create a line between this point and camera and use this line for 3d collision testing. Line sphere is quite easy, you find the distance between line and origin of sphere and it collides if it is below radius [ link ] . For polygons, you have to find plane that polygon is on, find the intersection between line and this plane [ link ], and check if intersection point on polygon [ link ]. If you don't want that much complexity, you can also use a sphere collision for your cube too.

Another solution is the opposite, you can map 3d objects to screen and check if 2d mouse cursor is inside this object. For spheres, this is quite easy. I did a similar thing in webgl . That should give you the 2d origion of the circle. If distance between this point and mouse is below radius, user is clicked on circle. To find radius, you can map origin of sphere + cameraUpVector*radius of sphere and this will give you the position of top point of the 2d circle.

For polygons, it will be a little harder. Each 3d triangles maps to a 2d triangle on screen. After that you can check if mouse is inside the triangle or not. [ link ].

If you implement the second method, you should also consider if objects are in front of the camera and visible (0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM