最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to apply a scalar multiplication to a Quaternion - Stack Overflow

programmeradmin2浏览0评论

So, I'm converting some c++ to javascript and I really need to know how D3DX defines their quaternion operators.

 //Here's the c++ version
 D3DXQUATERNION Qvel = 0.5f * otherQuat* D3DXQUATERNION(angVel.x, angVel.y, angVel.z, 0);


 //Here's the js quat multiplication
   function quat_mul(q1, q2) {            
   return 
    [q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x,
    -q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y,
     q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z,
    -q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w]

Is the scalar operation quat * 0.5f just like this?

     quat.x *= .5;
     quat.y *= .5;
     quat.z *= .5;
     quat.w *= .5;

So, I'm converting some c++ to javascript and I really need to know how D3DX defines their quaternion operators.

 //Here's the c++ version
 D3DXQUATERNION Qvel = 0.5f * otherQuat* D3DXQUATERNION(angVel.x, angVel.y, angVel.z, 0);


 //Here's the js quat multiplication
   function quat_mul(q1, q2) {            
   return 
    [q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x,
    -q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y,
     q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z,
    -q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w]

Is the scalar operation quat * 0.5f just like this?

     quat.x *= .5;
     quat.y *= .5;
     quat.z *= .5;
     quat.w *= .5;
Share Improve this question asked Jan 26, 2012 at 23:48 FlavorScapeFlavorScape 14.3k12 gold badges79 silver badges123 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

According to this link, it's like you say (and also in the quaternion number system):

inline D3DXQUATERNION& D3DXQUATERNION::operator *= (FLOAT f)
{
    x *= f;
    y *= f;
    z *= f;
    w *= f;
    return *this;
}

This is a very old question but I can verify this from a published source: Vince 2011 - Quaternion for Computer Graphics, page 57-58.

In simpler form:

q = [s, v] where s \in R and v \in R^3
\lambda q = [\lambda s, \lambda v] where \lambda \in R

In more plex notation:


q = a + bi + cj + dk

q * s = s * a + bi * s + cj * s + dk * s
      = s * a + (b * s)i + (c * s)j + (d * s)k

in javascript


let quat = [0.4, 0.3, 0.7, 0.1];

function quaternion_scalar_multiplication(quat, s){
    quat[0] *= s;
    quat[1] *= s;
    quat[2] *= s;
    quat[3] *= s;
    return quat;
}

发布评论

评论列表(0)

  1. 暂无评论