grire974 Posted August 21, 2016 Share Posted August 21, 2016 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? Quote Link to comment Share on other sites More sharing options...
dbawel Posted August 21, 2016 Share Posted August 21, 2016 The following topic should provide you with the math you're looking for, as well as issues you might run into: grire974 1 Quote Link to comment Share on other sites More sharing options...
RaananW Posted August 21, 2016 Share Posted August 21, 2016 BABYLON.Quaternion.RotationYawPitchRoll(rotation.y, rotation.x, rotation.z) Should do the trick. grire974, dbawel and ian 3 Quote Link to comment Share on other sites More sharing options...
grire974 Posted August 22, 2016 Author Share Posted August 22, 2016 Thank you both! I think @RaananW suggestion will work nicely; great to have some background on it though @dbawel Quote Link to comment Share on other sites More sharing options...
grire974 Posted August 25, 2016 Author Share Posted August 25, 2016 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/ Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Emil Posted February 16, 2017 Share Posted February 16, 2017 @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 Quote Link to comment Share on other sites More sharing options...
JohnK Posted February 16, 2017 Share Posted February 16, 2017 Have a look at the pages on rotations and quaternions here http://babylonjsguide.github.io/advanced.html They might help. 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.