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.
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.
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?