Jump to content

Convert rotation to Quaternion


grire974
 Share

Recommended Posts

This might be due to a lack of general understanding - however I would like to know if there is a way (within bablyonjs) to convert a rotation (e.g. rotation.x = a, rotation.y = b, rotation.z = c) to a quaternion (axis of rotation, and an angle for rotation around that axis; e.g. x,y,z,w).

According to the babylonjs docs; they seem to be exclusive of each other; e.g. if you set the AbstractMesh.rotate property (quaternion) then the .rotation property is automatically set to the Zero vector & vice versa.  Basic rotation around each of the three axes (e.g. .rotation) is easier for most people (e.g. users of my code) to understand (compared to quaternions) but some other software that I'm trying to integrate with works better with quaternions. 

Rotation matrix seems to have the same problem (e.g. setting the .rotation property doesn't affect it).  I converted some C++ code from cocos2dx into JS to convert a rotation matrix to a quaternion: http://www.babylonjs-playground.com/#1H0QGF#2

However this also doesn't give me a the desired result (because BJS is returning an identity matrix I think).

Any Thoughts? 

Link to comment
Share on other sites

Final update - I think I had my definition of Quaternion wrong; in fact I was actually looking for axis of rotation and angle of rotation; which is also expressed as 4 numbers and used in things like OpenGL's glRotate function and other libraries such as Open Scene Graph (OSG).  The Quaternion class has functions for creating Quaternions from axis+angle; but nothing (as far as I could tell) for creating axis + angle from a quaternion (or from a matrix, or from standard rotation angles).  Found some useful math pages though to help do this; The easiest one to follow is based off of a Quaternion; so if you don't have a Quaternion already - use @RaananW's code above & then convert the Java code listed at the bottom of this page - to convert that Quaternion to axis + angle; worked like a charm for me:

http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/

Link to comment
Share on other sites

  • 5 months later...

@grire974 I think I am having the same issue. I have a code in python:

z_axis = (0.0, 0.0, -1.0)
segments = 32
table_radius = 0.6
height_flat = 0.35
for index in range(segments):
    quat = Quaternion(z_axis, (index / segments) * PI_2)
    result = quat * Vector((table_radius, 0.0, height_flat))

converted:

var segments = 32;
var table_radius = 0.6;
var height_flat = 0.35;
for(var index = 0;index < segments;index++){
     var quat = new BABYLON.Quaternion(0.0,0.0,-1.0,(index / segments) * PI_2); 
     var a = new BABYLON.Vector3(table_radius, 0.0, height_flat);
     var result = quat*a; //does not work
}

I also tried the code from your suggested math page, but I get negative numbers under sqrt and even with abs still nothing useful. 

Can you share your piece of code with the conversion? I am stuck at this point...thanks

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...