MackeyK24 Posted September 14, 2017 Share Posted September 14, 2017 Hey guys I got a situation (fallowing some training material) where I need to multiply and Vector3 * Quaternion... Example: var direction:BABYLON.Vector3 = new BABYLON.Vector3(0,0,-distance); var rotation:BABYLON.Quaternion = this.manager.euler(rotaionX, rotaionY, 0.0); // Something like this pseudo code below var result:BABYLON.Vector3 = this.cameraTarget.position + rotation * direction; How do I do something like this in Babylon ??? Quote Link to comment Share on other sites More sharing options...
adam Posted September 15, 2017 Share Posted September 15, 2017 I thank you can convert the quaternion to a matrix and then use Vector3.transform(). Skip the quaternion altogether if that is possible. Quote Link to comment Share on other sites More sharing options...
adam Posted September 15, 2017 Share Posted September 15, 2017 I looked again at what you're doing and you are probably going to want to use Vector3.TransformNormal() Quote Link to comment Share on other sites More sharing options...
MackeyK24 Posted September 15, 2017 Author Share Posted September 15, 2017 4 minutes ago, adam said: I looked again at what you're doing and you are probably going to want to use Vector3.TransformNormal() Yo @adam do you think you format this code to something useful in Babylon and I will make part of a MothTools or something: Edit: // Something like this pseudo code below var result:BABYLON.Vector3 = position + (rotation * direction); Quote Link to comment Share on other sites More sharing options...
adam Posted September 15, 2017 Share Posted September 15, 2017 var position = BABYLON.Vector3.Zero(); var direction = new BABYLON.Vector3(1, 0, 0); var rotation = BABYLON.Quaternion.RotationYawPitchRoll(Math.PI*.5, 0, 0); var matrix = BABYLON.Matrix.Identity(); BABYLON.Matrix.FromQuaternionToRef(rotation, matrix); var result = position.add(BABYLON.Vector3.TransformNormal(direction, matrix)); console.log(result); Quote Link to comment Share on other sites More sharing options...
MackeyK24 Posted September 15, 2017 Author Share Posted September 15, 2017 16 minutes ago, adam said: var position = BABYLON.Vector3.Zero(); var direction = new BABYLON.Vector3(1, 0, 0); var rotation = BABYLON.Quaternion.RotationYawPitchRoll(Math.PI*.5, 0, 0); var matrix = BABYLON.Matrix.Identity(); BABYLON.Matrix.FromQuaternionToRef(rotation, matrix); var result = position.add(BABYLON.Vector3.TransformNormal(direction, matrix)); console.log(result); Yo @adam Thanks man... I will put this in some kind of Helper Tool Quote Link to comment Share on other sites More sharing options...
MackeyK24 Posted September 15, 2017 Author Share Posted September 15, 2017 Yo @adam Just so I'm clear... Does this do the actual Multplication of the rotation (converted to a matrix) times Direction Vector.. So I should be able to do something like: public multplyVectorByQuaternion(vec:Vector3, quat:Quaternion):Vector3 { var matrix = BABYLON.Matrix.Identity(); BABYLON.Matrix.FromQuaternionToRef(quat, matrix); return BABYLON.Vector3.TransformNormal(vec, matrix); } And if that works... I make a xxxToRef version: public multplyVectorByQuaternionToRef(vec:Vector3, quat:Quaternion, result:Vector3):void { var matrix = BABYLON.Matrix.Identity(); BABYLON.Matrix.FromQuaternionToRef(quat, matrix); BABYLON.Vector3.TransformNormalToRef(vec, matrix, result); } Note: I will of a better name than: 'multplyVectorByQuaternion' But should that work ??? Quote Link to comment Share on other sites More sharing options...
adam Posted September 15, 2017 Share Posted September 15, 2017 I'd call it something like Quaternion.RotateNormal. Quaternion.RotateVector3 You could use a static temp matrix rather than allocating a matrix every time you call the function. Quote Link to comment Share on other sites More sharing options...
Kesshi Posted September 15, 2017 Share Posted September 15, 2017 Here are two versions of my "rotateVector3ToRef" function. basic version: export function rotateVector3ToRef(pV: BABYLON.Vector3, pRot: BABYLON.Quaternion, pResult: BABYLON.Vector3): void { //pResult = pRot * pV * conjugate(pRot) let tQuat = pRot.multiply(new BABYLON.Quaternion(pV.x, pV.y, pV.z, 0)); tQuat.multiplyInPlace(pRot.conjugate()); pResult.x = tQuat.x; pResult.y = tQuat.y; pResult.z = tQuat.z; } And a faster version based on this: http://blog.molecular-matters.com/2013/05/24/a-faster-quaternion-vector-multiplication/ This is the one i'm using in production. export function rotateVector3ToRef(pV: BABYLON.Vector3, pRot: BABYLON.Quaternion, pResult: BABYLON.Vector3): void { //t = 2 * cross(pRotation.xyz, pVector) //pResult = pVector + pRotation.w * t * cross(pRotation.xyz, t) let tx = 2 * (pRot.y * pV.z - pRot.z * pV.y); let ty = 2 * (pRot.z * pV.x - pRot.x * pV.z); let tz = 2 * (pRot.x * pV.y - pRot.y * pV.x); pResult.x = pV.x + pRot.w * tx + (pRot.y * tz - pRot.z * ty); pResult.y = pV.y + pRot.w * ty + (pRot.z * tx - pRot.x * tz); pResult.z = pV.z + pRot.w * tz + (pRot.x * ty - pRot.y * tx); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.