Need EulerAngles from normal

We want to create a hit effect when one thing collides with another thing. Pretty straightforward, so I listen for collisionstart and instantiate my effect. The problem I’m having trouble with is rotating this effect in the desired rotation.

Ideally, I would do something like

var normal = collisionResult.contacts[0].normal;
var eulerAngles = new pc.Quat().LookRotation(normal, pc.Vec3.UP);

But there is no such function in PlayCanvas and the math for it is hopelessly complex, as in this post. I was wondering if there ‘is’ a Unity LookRotation equivalent and I’m just missing it.

btw it doesn’t matter if the resulting rotation puts the right, up or forward vector in the normals direction, so long as one of them does: I’ll be able to figure it out from there.

Check this, you can pass the normal and the up vector to get a matrix that can be used for that:

var matrix = new pc.Mat4();
var quat = new pc.Quat();
this.setMat4Forward(matrix, raycastResult.normal, pc.Vec3.UP);
var alignAngles = quat.getEulerAngles();

Once again, you help me out of a sticky situation. That’s it: I’m putting you in the special thanks of our credits.

1 Like

I was getting a bit of trouble when the forward and up where equal, so I added this quick hack:

if(forward.equals(up)) {

Thought I’d let you know. Maybe it’s intended behaviour with look rotation like this.

1 Like

That sounds like gimbal lock where two axes align and makes it impossible to generated a third axes from a cross product.

Normally, if this happens, a different axes is used for ‘up’ such as right or one is supplied by the developer.