Handmade Network»Forums
Den V
19 posts
None
Object rotation in the direction of a vector (3D Math/Rendering question)
Edited by Den V on
Hi All,

I am working on some prototypes for Android using ARCore and OpenGL 2.0 ES

Here is a problem I am trying to solve, but got stuck. I have two points (on the same plane, so same y-coordinate) created by a screen taps. Each point is describes as Pose, which means I can get its ModelMatrix and rotation information. Since each point is different and was added independently, they will have different Pose and rotation matrices. Typically, when Pose is added, the object is facing the camera. So it can be said that both points are facing the camera.

Lets say one point is A, the other one is B. Now I have vector AB = B - A. And I need to place and object C that is facing the same direction as vector AB.

When I place an object with a tap, it creates and Anchor, which contains the Pose. Again, Pose is just Model->World transform. But the default behavior, is to make the placed object face the camera. I don't need the object to face the camera. I need to create Model->World transform such that the object C will be facing same direction as AB vector.

Is there a way to create a rotation, know only the direction vector? Since both vector AB and object C are on the same plane, the problem can be reduced from 3D to 2D.

Would appreciate any help!

View and Projection matrices are obtained from Camera object.

Full transform is as follows:
gl_Position = Proj from Camera object * View from Camera object * ModelMatrix from Pose object * vertex
Mārtiņš Možeiko
2559 posts / 2 projects
Object rotation in the direction of a vector (3D Math/Rendering question)
Edited by Matt Mascarenhas on
Typically frameworks provide gluLookAt-like function for this. It allows to construct transformation matrix that sits at point "eye", and looks in specific direction, with up vector being "up".

Not sure if this helps, but Android has android.opengl.Matrix type that provides this kind of function: , int, float, float, float, float..., float, float, float)]setLookAtM. If you cannot use, you can check how it is derived and implement it yourself.

Here's example how to implement it in C: https://www.khronos.org/opengl/wiki/GluLookAt_code

EDIT: forum breaks the url, but just scroll to setLookAtM function in Matrix class.
Den V
19 posts
None
Object rotation in the direction of a vector (3D Math/Rendering question)
I tried getting lookAt matrix and using it at my model Matrix, but it wasn't rendering properly. So I thought maybe it wasn't the right approach. If it is the right approach, and it's a matter of proper setup, I will walk through it again to see if I get better results.

What should I use for "eye" is this case?
Simon Anciaux
1337 posts
Object rotation in the direction of a vector (3D Math/Rendering question)
If you want A to look at B, eye is the position of A, center is the position of B and up is what you would consider up to be in your application.