[SOLVED] Multiplying Vector by Quaternion Won't Work

I am attempting to multiply a vector by a quaternion to rotate the vector. I was able to script it down to this simple code:

var v = new pc.Vec3(1, 0, 0);
var q = new pc.Quat();
q.setFromAxisAngle(pc.Vec3.UP, 45);
v = q * v;
console.log(typeof v);

I tried this, yet the console said that the vector was now a number. I’m not sure what the problem is, but similar code worked completely fine in Unity. What is going on? Thanks.

JS doesn’t have operator overloads. Use Quat | PlayCanvas API Reference (transformVector) function instead

Thank you, it worked like a charm. Is there possibly a similar function for adding a position to a directional vector?

Not sure what you mean but you can look at the API for pc.Vec3 here Vec3 | PlayCanvas API Reference for the add functions to see if one of them would help you

What I mean is that I want to take a position (e.g. the position of object1) and a directional vector (e.g. object1’s forward vector), then set the position of object2 to the object1’s position plus 3 on object1’s forward vector so that object2 appears in front of object1. Sorry if that was complicated. Thanks again. I tried the add function, but it didn’t work.

Would it be easier to paste my code and add comments on what I think it should do, then you can tell me where I went wrong?

The way I would do it is to take the direction vector, scale it and then add to the position.

var direction = object1Entity.forward.clone();
direction.mulScalar(3);
var newPosition = object1Entity.getPosition().clone();
newPosition.add(direction);
object2Entity.setPosition(newPosition);

Untested but you get the idea.

Note that in Unity, vec3 are structs so are copied by value. JS doesn’t have this so it always copies by reference hence all the clone calls

Thank you. See, this is where I’m confused. This is my code attempt at making a third person camera (note this script is attatched to the camera and “target” is the player):

    //camVector is now the target's forward
    camVector = this.target.forward.clone();
    //attatches the forward vector to the player so that the camera's final position is based off the player
    camVector.add(this.target.getPosition());
    //declaring the quaternion and setting it to a 45 degree rotation around the targets right vector to make the camVector be slightly above the player
    q = new pc.Quat();
    q.setFromAxisAngle(this.target.right, 45);
    //using the quaternion to rotate the camVector
    q.transformVector(camVector);
    //making the camVector go backwards, behind the player
    camVector.scale(-1);
    //making the camera go to the camVector
    cam.setPosition(camVector);
    //this does not work at all, the movement of the camera is not intended and hard to explain

And, to me at least, the code looks pretty similar, so I’m not too sure what I did wrong. Did I do it in the wrong order or something?

Hmm, the code looks correct, maybe it be best to share the project/small example of the issue?

Yes, here is the link:
https://playcanvas.com/project/1045567/overview/blank-project
Note that I replaced the camera with a white cube to make it easier to see what was going wrong.

Made a few fixes in this fork:

https://playcanvas.com/project/1053468/overview/f-blank-project

The biggest one is that transformVector returns the result or you can also supply a second vec3 to store the result in

See example usage in the API docs

So the code now looks like this:

Cam.prototype.update = function(dt) {
    const cam = this.entity;
    const camVector = this.target.forward.clone();
    const q = new pc.Quat();
    q.setFromAxisAngle(this.target.right, -45);
    camVector.scale(-2);
    q.transformVector(camVector, camVector);
    camVector.add(this.target.getPosition());
    cam.setPosition(camVector);
};