smart-fun / smartGL

SmartGL is a Graphic Engine for creating Android Games and Apps. It is based on OpenGL and handles 2D Sprites and 3D Textured Objects.
Apache License 2.0
109 stars 24 forks source link

Object3D location vs screen coordinates #10

Open creek23 opened 7 years ago

creek23 commented 7 years ago

Is there a documentation on how to properly map 3D object's location on phone screen?

I'm trying to touch the 3D and move it around my screen. I tried iteration of 1 but the jump wasn't per pixel but by some kind of unit I'm currently not aware of.

I can cheat around for my phone's screen size but what would assure me that my app will work properly to someone else's phone?

~creek23

smart-fun commented 7 years ago

Hi,

there is no "pixel" position for 3D Objects.

The size of the objects depends on the size of the view. You can compare this to a picture that you resize. On small screens you have a small picture, on big TVs you have a big picture, but the picture is the same.

If on a device the aspect ratio is different (rectangle vs square) it may have some more or less space on the side of your object.

Arnaud.

creek23 commented 7 years ago

How do you suggest how I should handle situations like, I touch to screen's X/Y coordinate and have a 3D object move to that location?

~creek23

smart-fun commented 7 years ago

If you can cheat I would recommend to do it as it is not that simple to go from 2D to 3D. As you can imagine, there are infinite solutions when you click on a X,Y position (the 3D point can be from very near to infinite distance)

When going from 3D to 2D, a projection matrix is used. To do the opposite operation (2D to 3D), you need to invert the process. Here is a good and short explanation of the 2 operations: https://stackoverflow.com/questions/31613832/converting-screen-2d-to-world-3d-coordinates#31617382

to get the matrix for the projection you can use: openGLView->getOpenGLRenderer()->getProjection3DMatrix()

to get the matrix of your Object3D you can use: object.getMatrix()

Probably this is something I could do in the future, but if you have time to experiment, go for it ^^

Arnaud.