Jump to content



Recommended Posts

After a long brainstorming with me, me and myself, I finally chose the name rebase() for the upcoming (I hope) function.


Let's go :



Line 39 = simple box creation as usual, ok ?

Line 42 =  I choose an arbitrary axis1

following lines = I want my mesh oriented like this (my own arbitrary constraints) :

- its current x axis should become the axis1

- its current y axis should be in the world axis1Oy plane

- it should be placed at (4, 4, -4)


that's all.

I don't know how to rotate it from its current orientation to the target one. And I don't want to learn about quaternion. Sorry, I'm lazzy.


Let' go on.

Axis1 will be my new x axis in the target orientation. Ok ?

So in the following lines, from the axis1, I just created an orthogonal axis2 with the cross product axis1 ^ Axis.Y... which results in a temp vector orthogonal to axis1 and to Y world axis, re-crossed with axis1 and temp to be then in the axis1Oy plane as wanted by my own constraints. Ok ?

I just create a last third orthogonal axis 3 = axis1 ^ axis2


And I draw them normalized, from (4, 4, -4) as asked, so you can understand better (line 48) : x = red, y = green, z = blue.


A just created box, a target orientation/location defined by 3 orthogonal arbitrary vectors and no knowledge about rotations/quaternions. Right ?


Let's uncomment lines 56-57 : http://www.babylonjs-playground.com/#XSX1M#5



Well, just a proto, there are yet many defaults :

  • the normals don't seem correct,
  • the mesh is created as updatable

I guess I could solve the normal issue.

No idea about how to avoid the updatable...

Can we set a mesh updatable for a while, then re-freeze it (if it wasn't originally updatable) ?

Link to comment
Share on other sites

Waw, it seems you find the solution, even I don't understand exactely how.


Thank you Jerome, DBawel will be glad too!


Now the way to implement it in my case, surely, Wingy will help me to understand your "trick"! :D

Link to comment
Share on other sites

well, according to this post : http://www.html5gamedevs.com/topic/14352-setting-mesh-updatable-property/?p=81678

I guess I will do two different things


a localRebase(origin, axis1, axis2, axis3) method : this will need to set mesh as updatable and will allow to change the mesh position/orientation after building in its local space. The mesh is modified in its local space. This is quite the prototype function.

If this function could be called within the mesh constructor, it wouldn't be necessary to set the mesh as updatable... but it would need an extra parameter to pass the target base.

Hard choice.


a worldRebase(origin, axis1, axis2, axis3) method (not yet even thought, too busy mind) : this will allow to keep a mesh non-updatable and will rebase the mesh in the world in the same way that the former method does. This will not modify the mesh in its local space. This should only modify the mesh.worldMatrix.

Link to comment
Share on other sites



The original goal of worldRebase() is to give to the user, who has no good rotation competences (almost all of us  ;) ) the ability to set directly his mesh to a target location/orientation.

Usually people know that they want their mesh, just after creation, then placed here and facing this other mesh, with its base parallel to this other axis or with this face in this place.

In brief, they quite know in what cartesian system (3 orthogonal vectors + 1 origin) in the space  to set their mesh, but they can't achieve to compute the rotations (translations are quite obvious) to orientate their mesh the right desired way.


BJS provides tools about HOW rotate/move things. We've got an initial status and tools to transform it. Then we get a result.


I just want a method for a new approach : the user knows the result he wants, but doesn't know how to set BJS tools to get it.

So I want to give him the way to define the target orientation, without any notion about angles, rotations to apply, etc. Just a target cartesian system : 3 orthogonal vectors + 1 origin, defined in the world system.

The worldRebase() method would do this : move/rotate the mesh to the target system.


I know this kind of tool is really awaited by many users since the rotation/quaternion aren't that obvious to everyone.



The localRebase() (almost done) does the same but modify the mesh (so upgradable = true) in its local system.

Same paradigm : the user doesn't know HOW to do, but know WHAT (the target) he wants.

Well maybe the localRebase() has not real interest actually.




example : http://www.babylonjs-playground.com/#12ZRI0

Here Alby creates text planes (grounds actually so they are created in the x0z plane).

He wants each plane to be aligned with the box-sphere (bs) axis on this length, he wants the plane to be quite "vertical" on its width (so width in bsOy plane), he wants each plane to be located in the middle of the box-sphere segment...

Well, he knows the target position/orientation for each plane but can't achieve how to rotate them the right way because it is quite complex.

So he knows WHAT is the wanted result and not HOW to get it.

Link to comment
Share on other sites

Well said, J!  (I suspect.  I didn't understand much.)


These "standard orienting" tools are the basis for "snaps", too.  (align/constraint, etc)  And great snaps... are the basis of another tool I've dreamed about... an editor that lets you quickly assemble BJS basic shapes... into a single model.  Or maybe takes a pile of basic shapes, and keeps trying to (re-) assemble them in different ways.  (cooooool!)


It's a bit related to another subject... making moving cameras and characters slide-along walls, and drop into slots in those walls - sideways tetris-like.  Ok, maybe it's not like that at all.  :)


Back on topic... a guy could select a base mesh and an align-me mesh, and start whacking the TAB button.  Smart-aligner code keeps aligning the object... to the target... in different ways.  Find one you like, and then exit align-edit mode.  HOW the user would get the CODE that DID that, so they could use it in their project... might not be easy. 


The SnapperBot!  Yay!  That SOB could assemble some serious BJS compound shapes.  Place IT... and a pile of BJS shapes... into a scene.  Come back in 5 seconds, and SnapperBot will have assembled all the shapes into SOMETHING.  A Parent-a-thon!  :)  Full random-seeding, chaos, and fuzzy-logic allowed, of course.  Wow! 


Later, we'll build CSG bot.  He just enters a BJS scene with a bag of points, and a day later, he has built a mess!  :)  FUN!


freebasing!  Alright!  Pardon?  Oh, sorry...  rebasing... cool!  ;)

Link to comment
Share on other sites

@DK :




Does this mean we can even update a non-updatable mesh (updatable param set to false on creation) with :

mesh.setVerticesData(kind, data, true);


seems weird if we can ...


I can see the bake code that the passed updatable value is got from this.getVertexBuffer().isUpdatable().

Isn't this property set on mesh creation with the mesh updatable parameter value ?

Link to comment
Share on other sites

mmh... after hours spent in grinding my lookAt() derivated home made method, I'm forgiving this way ... I just can't get the intermediate angles correctly.


If a math expert wants to try to solve this problem :

- Given 3 orthogonal normalized vectors (ie : world axis), the source (A, B, C), and knowing the world coordinates of their image after rotation (so 3 other orthogonal normalized vectors), the target (V1, V2, V3), find out either the 3 Euler angles, either raw, pitch, roll, either the quaternion, either the rotation matrix used for this transformation :

A => V1

B => V2

C = > V3


You just know initial and target orientations (vectors).

You've got an hour :) .


I think bake() won't fit because it needs a transformation matrix and it's just what don't know here ...

Link to comment
Share on other sites

sa race !

Didn't notice it  !!!


Ok, so maybe I can go this lead : non-updatable mesh but using setVerticesData

ie : my own bake without knowing any matrix but only the target result

Will give a PG try


BTW : about the math problem (knowing source and target, find out the in between rotations), I just asked a math profs here.

He sincerally has no idea how to do this

Link to comment
Share on other sites

Ok, let's go back into this rebase() method with the setVerticesData() approach.



My box is now created explicitly as non-updatable (line 66) :

var mesh = BABYLON.Mesh.CreateBox("box", 5, scene, false); 

And I still have my target system (x = red, y = green, z = blue) somewhere else in the space.

I want my box to be set in this target system according to new x, y, z axis.


line 69n, tadaaa : http://www.babylonjs-playground.com/#JVXM#2


Now it works as expected (maybe I need to really check the axis order, but it's not a big deal).



However, I'm not satisfied if I scale the mesh (line 70-71) : http://www.babylonjs-playground.com/#JVXM#3

scaling applies along the original x and y system axis here after the mesh has been rebased.


Is this problem the same with the bake() method ? Probably not, because bake() works upon a transformation matrix, so the scaling should be embedded.

Link to comment
Share on other sites

Mmmh... last version

Here is a scaled box : http://www.babylonjs-playground.com/#JVXM#4


Rebasing it now : http://www.babylonjs-playground.com/#JVXM#5

Quite the expected result, isn't it ?


The only constraint is that we need to scale the mesh before rebasing it, else it doesn't work.


So I would like to know if you satisfy with it (I wish I could deduct the transformation matrix from source-target status instead) ?

Is it worth it a PR or rather a bjsx module (quite small for a real module) or simply an Alby's user custom centralized function ?

Link to comment
Share on other sites

We don't need it in-framework in a hurry, do we?  ;)  We all see the cool new functions in the playground editor.  Consider them stolen.  :)  Let's take some time to do some more demos with plenty of visualization, and ponder the state of affairs.


Jerome, don't be afraid to use matrices in these tools, because, they will be behind-the-scenes.  You are writing helper functions, and you can let callers use Vec3 args to call your helper funcs.  Underneath, you do whatever is necessary.  :)


Ok, WHO put the "quaternion" line in the originial cylinderBetweenPoints(), and WHY?  It was probably deltaSquash... and he likely did it... for some kind of reason.  Baking is weird.  It is a "last thing"... because if you try standard rotating and scaling AFTER you did an offsetting bake... the axes will be weird.  But, this, too, can be worked-with, under certain circumstances.


I think Jerome is seeking sane rotating and scaling AFTER the mesh has been aligned to some radial vector.  And this... MIGHT require yaw, pitch, and roll.  Quaternions SEEM to be "around a radial"-based, right?  *shrug*


Maybe... the helper func should be mesh.rotationQuaternion.alignToVector(vector)  *shrug*  Just guessing.  Thanks for thinking/talking about this stuff, you guys!  Take your time.  :)

Link to comment
Share on other sites

Well, the complex math problem underneath is :


deduct a rotation matrix (or quaternion) just given the source and the image (the rotated source).


( beer offered in Rodez to the changeller )


I know the initial status, I know the target status (not in terms of rotation but in terms of world coordinates), I want to find out the needed rotation from initial to target status in order to apply it to the initial... and so to remain in the BJS engine flow : local => world => projection


Using setVerticesData() is just a trick because it changes the original mesh vertex positions (the mesh is rebuilt rotated), it doesn't really rotate the mesh.

Link to comment
Share on other sites


Unless some genius finds out what I'm looking for (rotation matrix deduction) -in this case, I'll implement the rebase() method with this deducted rotation matrix and PR asap, I will give you the current rebase() function (this will not be integrated in BJS then).


What does rebase() do ?

You've got a just created mesh, say a plane.

You want to set it at this position with its length aligned on this axis and its width aligned on this other axis for instance.

Moreover, you have no idea how to rotate it to achieve this result.


Well, you'll just use rebase(mesh, position, axis1, axis2, axis3)

Link to comment
Share on other sites

I beg you to use quaternions when rotating objects.  Otherwise you'll never get your object to rotate beyond -89.9 and 89.9 degrees (approximately.)  And once you reach these limits, your object may appear to rotate on more than one axis - depending on it's beginning orientation.  An example would be to rotate a carousel in your scene - using euler vectors the carousel would flip each time you reached these limits.  However, using a constant function such as f(x) = C to provide a constant delta to a quaternion float, you are able to continually rotate the carousel around a single axis with continuity.  


At the bottom of this thread is a little primer on quaternion floats and values - not easy to visuallize, but if you just use:


mesh.rotationQuaternion = new BABYLON.Quaternion(x, y, z, w); 


And place it in a scene and experiment by changing the values based on the explaination below, you'll most often use quaternions to rotate objects.



1. A quaternion is a complex number with w as the real part and x,y,z as imaginary parts.

2. When a quaternion represents a rotation, then w = cos(theta / 2), where theta is the rotation anglearound the axis of the quaternion.

3. The axis v(v1,v2,v3) of a rotation is encoded as a quaternion: x = v1 sin (theta /2), y = v2 sin (theta /2), z = v3 sin (theta /2).

4. If w = 1 then the quaternion defines 0 rotation angle around an undefined axis v = (0,0,0).

5. If w = 0 then the quaternion defines a half circle rotation since theta then would be +/- pi.

6. If w = -1 the quaternion defines +/- 2pi rotation angle around an undefined axis v = (0,0,0).

7. And a quarter circle rotation around a single axis causes w to be +/- 0.5 and x/y/z to be +/- 0.5


As Quaternions are four-dimensional, you need four properties. The x/y/z properties don't correspond to x/y/z in euler angles. With quaternions, each of the properties is a normalized float between 0 and 1, so for example a euler angle of 45/90/180 is represented by a quaternion as approximately .65/-.27/.65/.27.

Got a headache yet?  I hope not, as you simply cannot apply deltas to euler vectors for rotation transforms and return any reliable rotational behavior beyond the limits.  Please, just play around with it and you'll get it.

Link to comment
Share on other sites

Thank you Jerome and Dbawel for your attempts (success!) to explain simply the use of some of the mysterious genuine or created functions (for me!) of babylon world.


I just find here some more historical informations about that stranger mathematical animal called named quaternion, culture is not incompatible with babylon, isn't it? ;)


So let us see what wiki said about it :


"Quaternion algebra was introduced by Hamilton in 1843.Important precursors to this work included Euler's four-square identity (1748) and Olinde Rodrigues' parameterization of general rotations by four parameters (1840), but neither of these writers treated the four-parameter rotations as an algebra.Carl Friedrich Gauss had also discovered quaternions in 1819, but this work was not published until 1900.

Hamilton knew that the complex numbers could be interpreted as points in a plane, and he was looking for a way to do the same for points in three-dimensional space. Points in space can be represented by their coordinates, which are triples of numbers, and for many years he had known how to add and subtract triples of numbers. However, Hamilton had been stuck on the problem of multiplication and division for a long time. He could not figure out how to calculate the quotient of the coordinates of two points in space.

The great breakthrough in quaternions finally came on Monday 16 October 1843 in Dublin, when Hamilton was on his way to the Royal Irish Academy where he was going to preside at a council meeting. As he walked along the towpath of the Royal Canal with his wife, the concepts behind quaternions were taking shape in his mind. When the answer dawned on him, Hamilton could not resist the urge to carve the formula for the quaternions,

    i2 = j2 = k2 = ijk = −1,

into the stone of Brougham Bridge as he paused on it.

From the mid-1880s, quaternions began to be displaced by vector analysis, which had been developed by Josiah Willard Gibbs, Oliver Heaviside, and Hermann von Helmholtz. Vector analysis described the same phenomena as quaternions, so it borrowed some ideas and terminology liberally from the literature of quaternions. However, vector analysis was conceptually simpler and notationally cleaner, and eventually quaternions were relegated to a minor role in mathematics and physics. A side-effect of this transition is that Hamilton's work is difficult to comprehend for many modern readers. Hamilton's original definitions are unfamiliar and his writing style was wordy and difficult to understand.

However, quaternions have had a revival since the late 20th century, primarily due to their utility in describing spatial rotations. The representations of rotations by quaternions are more compact and quicker to compute than the representations by matrices. In addition, unlike Euler angles they are not susceptible to gimbal lock. For this reason, quaternions are used in computer graphics,computer vision, robotics, control theory, signal processing, attitude control, physics, bioinformatics, molecular dynamics, computer simulations, and orbital mechanics. For example, it is common for the attitude-control systems of spacecraft to be commanded in terms of quaternions. Quaternions have received another boost from number theory because of their relationships with the quadratic forms.


Be  well!

Link to comment
Share on other sites

Alby collects all BJS internal methods AND all Wikipedia articles  :D  :D  ... endless collection  ;)



I saw RaananW's solution, but it's more complex here : we don't know either axis rotation, either rotation angle.

Everything about the transformation is unknown, we just know the result.



I wish I can use any kind of rotation (Euler or quaternions).

My problem is discovering the angles used by the transformation from two coordinate status only.





My background threaded brain just notified me yesterday night as I was reading some book :

"hey guy, you're certainly not Isaas Newton, but look at this book in your left hand rotated in space and that you want to set on the bedside table ...

Imagine the book in your hand is currently the target orientation and the bedside table position the origin orientation.

Now you will rotate this book only three times on each of its local cartesian system axis and translate it once to set it back on the bedside.

I'll show you how..."


And it (the brain) showed me a way.



Jumped onto a paper and a pen to write all that stuff down (means : kill -9 the background thread, I want to sleep please).


Now I have to translate this sequence into some maths and a decent algo (quite many steps and conditions) but I think I can now deduct the 3 angles, I really hope, and I know a process to get the intermediate systems in between target and original status.

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.

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.


  • Recently Browsing   0 members

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