[SOLVED] Setting rotation by a vector

Good afternoon,

I have a vector pointing the direction my object should be pointing. How can I set the rotation for this object so that it points in the same direction as the vector?

Here is an example:
1
The cube must be point in the same direction
2

I imagine it to be a trivial question, but I’m messing around here with those rotations

Hello.

Okay. I see.

Maybe you’re looking for entity.forward?

the entity.forward returns a vector with the forward direction, but how to change the forward to align with my other vector?

Just use getRotation() and setRotation(). I guess it should work as well.

It’s a little tricky but a solved problem: https://answers.unity.com/questions/467614/what-is-the-source-code-of-quaternionlookrotation.html

Or you can copy what the mat4.setLookAt function does: https://github.com/playcanvas/engine/blob/master/src/math/mat4.js#L483

Except you have already calculated what vector z is as that’s your forward direction.

1 Like

Good night guys,

I’ve translated the code to playcanvas, but it creates a distorted rotation and not what i need. To contextualize, i have an program that need to do a raycast at an object, and at the raycast point, create an object with normal direction of this point.

What do you mean by distorted rotation?

Are you trying to create an object that is looking in the direction of the normal of the intersection point?

Are you trying to create an object that is looking in the direction of the normal of the intersection point?

Yes, that is my goal.

What do you mean by distorted rotation?

The object not point to right direction

Here’s an example of setting the forward direction of an entity to look in the direction of the normal on the intersection point of a raycast: https://playcanvas.com/editor/scene/605466

This is from copying the lookAt code that I linked to earlier.

raycast-normal

3 Likes

I’ve tried this solution. Worked perfectly! Thank you guys for the attention =D

1 Like