Yo @Deltakosh and @sebavan … In Unity… I guess you can just multiply a quaternion and a vector3
Example Unity Code:
Vector3 wantedPosition = target.position;
wantedPosition.y = currentHeight;
wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
How the heck would port this line of code when wantedPosition is a Vector3 and its trying to add the current rotation angle expressed as a Quaternion plus the new usedDistance as a Vector3
wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
Please Help
Hey,
Maybe you can convert the Quaternion to Vector3 first, and after this 2 vectors are easy to multiply.
https://doc.babylonjs.com/api/classes/babylon.quaternion#toeulerangles
Raggar
August 17, 2019, 7:53am
3
You can also look at other implementations:
return this;
};
/**
* Multiply the quaternion by a vector
* @method vmult
* @param {Vec3} v
* @param {Vec3} target Optional
* @return {Vec3}
*/
Quaternion.prototype.vmult = function(v,target){
target = target || new Vec3();
var x = v.x,
y = v.y,
z = v.z;
var qx = this.x,
qy = this.y,
qz = this.z,
qw = this.w;
JohnK
August 17, 2019, 9:11am
4
You can turn the quaternion to a matrix Matrix - Babylon.js Documentation and the apply the matrix to the vector with Vector3 - Babylon.js Documentation
1 Like
Yo @Raggar … Thank a bunch… You too @JohnK …
I think i am going to try and use this in my Utilities
/** Multiply the quaternion by a vector */
public static MultiplyQuaternionByVector(quaternion:BABYLON.Quaternion, vector:BABYLON.Vector3): BABYLON.Vector3 {
const result:BABYLON.Vector3 = new BABYLON.Vector3(0,0,0);
BABYLON.Utilities.MultiplyQuaternionByVectorToRef(quaternion, vector, result);
return result;
}
/** Multiply the quaternion by a vector to result */
public static MultiplyQuaternionByVectorToRef(quaternion:BABYLON.Quaternion, vector:BABYLON.Vector3, result:BABYLON.Vector3) : void {
// Vector
const x:number = vector.x;
const y:number = vector.y;
const z:number = vector.z;
// Quaternion
const qx:number = quaternion.x;
const qy:number = quaternion.y;
const qz:number = quaternion.z;
const qw:number = quaternion.w;
// Quaternion * Vector
const ix:number = qw * x + qy * z - qz * y;
const iy:number = qw * y + qz * x - qx * z;
const iz:number = qw * z + qx * y - qy * x;
const iw:number = -qx * x - qy * y - qz * z;
// Final Quaternion * Vector = Result
result.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
result.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
result.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
}
We shall see how that works… Ill let you know
1 Like