[SOLVED] Getting a Quaternion from a Normal

I have this issue. I’m raycasting with a weapon, and I just can’t seem to get bullet holes (being that I have an open-world element/sprite) to get the right angle on any surface it hits.

Here is what I have right now:

if (result) {
        var hitEntity = result.entity;
        var hitAngle = result.normal;
        var hitPoint = result.point;
        var newHole = this.bulletHole.clone();
        var q = new pc.Quat().setFromAxisAngle(hitAngle, 90);
        this.app.root.findByName('Debug Text').element.text = newHole.getPosition();
        // newHole.setRotation(hitResult.hitAngle);

The hitAngle is actually a normal (E.G. new pc.Vec3(0, 1, 0); )

I have no idea what math I need to do to get this normal to turn into a simple rotation for my element.

This is my fifth iteration, and I know that it’s incorrect, I just can’t seem to find a proper solution using the engine’s currently available math. Also, Unity forums and other game development forums haven’t been of much help…

I know this is a stupid question, but I’d really appreciate it if one of you could through me in the right direction!


Maybe you can utilize this function:


Set the bullet entity position to hitPoint, let it look at (hitPoint + result.normal). In case the negative Z axies of the bullet object is “up”, this works. Otherwise you have to rotate it in its local coordinate system to get the result you want, or you align a “Container” entity like described above which has the actual bullet hole entity as a child which you rotate as long as it fits :wink:

I think that the negative z axis of the bullet faces the player, which would be Vec3.BACK, right?

I got it! I used the container suggestion! Thanks for the help, mate!

1 Like

You are welcome, I’m glad that it worked out