How to orient an object along a direction that goes from point A to point B?

Hi everyone, i got a problem that I don’t know how to solve.
I have an “enemy” object, a capsule, that rotates along a sphere toward a “player” object, a cube.
The movement is done by rotating a pivot positioned in the center of the sphere, but this is not important.
The problem I want to solve is that the enemy should face the direction in which is travelling.
To do this I thought about using an approximation fo the derivative: I get the global position of the enemy object, I move very little along the movement direction and take again the global position.

Now I have two positions (P0 and P1) and I can compute the direction between the two points (D=P1-P0),
all I need to do now is to orient the enemy object along the direction computed. How can I do this?

From other forum topics I found this piece of code:

        var d=p1.subtract(p0)
        const target = p0.add(d);
        this.enemy.lookAt(d);

But it works only sometimes, other times the object is rotated 90° around the local y axis I think.

I prepared a playground with a basic example, my solution, and some utility function to get a random position, orient the object to stick out of the surface and so on. Around every 0,3 seconds a line is printed that represent the direction.
https://playground.babylonjs.com/#JKDD0G#4

Any help is appreciated!

Not too sure what I am looking into :frowning: @Cedric might be able to help when he is back from vacation next week

1 Like

Thank you for the anwer, if you don’t understand something about what I want to achieve just tell me and maybe I could explain it bettter

ohhh no, I simply can not wrap my head around the issue hence the ping to @Cedric :slight_smile:

1 Like

I think I understand what you’re hoping to accomplish here, and it’s fortunately pretty easy to do it. I’m not sure what the addition is for in the maths, but it’s moot because that’s not the operation I think you want.

To get a Vector3 representing the capsule’s current direction of travel you want to take the Vector3 Cross Product between the velocity vector and an orthogonal basis vector, like mesh.up. Make sure you have normalized the velocity vector first!

If you’re using vectors for rotation, just assign the cross product’s result to the meshs’ rotation property.

Ed: you can find the velocity vector simply by comparing the meshs’ position between two frames; subtract one from the other and normalize.

1 Like

Really interesting, but I tried that and it doesn’t work, I think it’s because I need the local UP axis and by doing mesh.up you get the world axis. Maybe this is problem or there is something I did wrong.
btw thank you for the answer, I’ll update here if I make it work

1 Like

Would something like this form a basis for what you want to achieve https://www.babylonjs-playground.com/#3H1BRD#1 ?

Slightly faster Babylon.js Playground

4 Likes

With world coordinates: I would compute a plane from 3 points : the sphere center, enemy position, player position The plane normal will become the right vector of the enemy. The enemy up vector will be (enemy position - sphere center). With those 2 vectors, you then need to compute the forward vector with a cross product. You know the enemy position so you have everything to compute the enemy matrix. Then, you can decompose it to euler or quaternion rotation.
The trick here is to compute the shortest path between enemy and player. This is done by computing a plane.

2 Likes