# solved Rotation to the target vector

## Recommended Posts

Guys, I need help.

How to turn the mesh towards the point given by the vector? What method of rotation is needed here?  Rotations is my weakness..

##### Share on other sites

Is the face a vector ? If yes, then what you can do is -
First find the new direction; this can be achieved by doing (some coord - face vector)

After that you have find the angle between the old face vector and the new vector found by step 1. Use this angle to rotate the object mesh

##### Share on other sites

4 minutes ago, ssaket said:

Is the face a vector ? If yes, then what you can do is -
First find the new direction; this can be achieved by doing (some coord - face vector)

After that you have find the angle between the old face vector and the new vector found by step 1. Use this angle to rotate the object mesh

Face is just initial direction of my object without rotation (front side of my mesh). I do not sure understand how i can get the vector of this direction.

##### Share on other sites

Use  (some coord - position vector of object) to find the new direction, then using face normal's direction vector as old face vector , find the angle between them.

##### Share on other sites

1 minute ago, ssaket said:

Use  (some coord - position vector of object) to find the new direction, then using face normal's direction vector as old face vector , find the angle between them.

Thanks! I'll try it now

##### Share on other sites

44 minutes ago, ssaket said:

Use  (some coord - position vector of object) to find the new direction, then using face normal's direction vector as old face vector , find the angle between them.

Not so fast.. I can't get it.

I do not understand how to get face normal's direction vector.. I need to create Vertex Normal's Data?

And the function for calculating angle between Vectors has a third parameter, it is not clear what should be in it..

##### Share on other sites

Hello, I thought you know the face direction; well anyways, in the skull example, first task would be to figure out the direction which we know is the face. As of now, I use pickInfo to find the index. Here check it out https://www.babylonjs-playground.com/#ADGVAT#2
You need to click on the face first then it will rotate to sphere's position.

Edit: for skull mesh, the front normal index is 49731(approx.), if you have this you can't skip the pickInfo part, here https://www.babylonjs-playground.com/#ADGVAT#3

##### Share on other sites

21 minutes ago, ssaket said:

Hello, I thought you know the face direction; well anyways, in the skull example, first task would be to figure out the direction which we know is the face. As of now, I use pickInfo to find the index. Here check it out https://www.babylonjs-playground.com/#ADGVAT#2
You need to click on the face first then it will rotate to sphere's position.

Edit: for skull mesh, the front normal index is 49731(approx.), if you have this you can't skip the pickInfo part, here https://www.babylonjs-playground.com/#ADGVAT#3

Thanks, this way really works. But the use of FacetData seems to me to be overcomplicated ... Is there really no simpler way, for example, using the initial rotation value (put it initially in the direction of the axis) . In my project, the mesh should move between different points and always look in the direction of its movement. Maybe I'm using something wrong.

##### Share on other sites

Not sure ! It won't be accurate also, 0 by default. How would you figure out the angle?

for example, us﻿ing the initial rotation value (put it initially in the direction of the axis)

Meanwhile you can try these resources   -
https://www.babylonjs-playground.com/#1YTZAC#3

##### Share on other sites

You may find this useful https://www.babylonjs-playground.com/#E0KDHN as well which rotates the mesh using lookAt

EDIT 21/12/2018

A bug in lookAt, to do with z -direction, was corrected in BJS 4.0 making the correct version of the above PG for version 4.0 and above https://www.babylonjs-playground.com/#E0KDHN#2

##### Share on other sites

7 hours ago, JohnK said:

You may find this useful https://www.babylonjs-playground.com/#E0KDHN as well which rotates the mesh using lookAt

Amazing! So simple. This is done in just one short line of code.

``skull.lookAt(some_coord);``

Thanks very much!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.