Jump to content

Call lookAt for rotation value only, without actually transforming the mesh?


Zephos
 Share

Recommended Posts

Hi all,

I have noticed that with animations, etc, it is often necessary to call a function that changes a mesh's position, get the new position, then undo the change by assigning the old value, and then animating between them. Now, perhaps there are already ways to get the new value without actually changing the position before animating, but I have had a hard time figuring out what methods those are. For example, recently I wanted to animate the rotation of a TransformNode that was my camera's parent. I used the lookAt() function and it worked fine, but it rotates my mesh before the animation. Is there a similar function that just returns the new rotation value? Looking through the source code, I couldn't find one that was obvious, but that may be because I am still getting the hang of the linear algebra stuff and don't know what things are doing.

If there isn't something like this, I am actually all set up with a clone of the repo (or my fork of it) and would love to add a getLookAtValue() function and do a PR. It would do the same thing as lookAt but without changing the mesh's actual rotation and would just return the values instead. Thoughts?

Thanks,

Zephos

Link to comment
Share on other sites

Hi Z.  Likely, the easiest way is to steal code from this PG:

https://www.babylonjs-playground.com/#JEHHJ2

I think it is doing what you wish... in a rather fancy way.  Lerps and slerps, interpolations, the "meat and potatoes" behind BJS animations.  Line 60... a Quaternion Slerp... holy crap!  :) I wonder who coded that PG.  A Quat-wrangler, for sure.   Simply-stated (by a potential idiot - me), interpolations (lerps) are a calculation of steps-across-time... used heavily in animation systems.

Under certain circumstances, BJS adds a property to mesh... called .rotationQuaternion.  After it is added, the old .rotation property is not used (much).  Babylon Animation system can animate Quaternions same as Euler vector3.  Some people/software LIKE working-with mesh orientation using Quaternions, while others prefer working-with classic Eulers. 

You are allowed/encouraged to add a mesh.rotationQuaternion = BABYLON.Quaternion.Identity(); ...at ANY time you wish (if a mesh.rotationQuaternion property does not already exist).  Line 28 in the above playground... does exactly that.

---------- extra crap (Euler) -----------

More-basic-speaking, I think @brianzinn once helped me with a similar issue, but I can't find the thread/playground right now.

Let's verify wishes:

At various times during live scene, you want to rotationally-animate somemesh1... to look-at somemesh2.  You need to "derive" an ENDING somemesh1.rotation value, so you can build the animation on-the-fly.  Sound correct?  I hope so.

Here's what I THINK I remember. 

1.  var dirToMesh2FromMesh1 = somemesh1.position.subtract(somemesh2.position);
     or maybe it's...   var dirToMesh2FromMesh1 = somemesh2.position.subtract(somemesh1.position);

1b.  Possibly do a dirToMesh2FromMesh1.normalize() here.

2.  var endingRotForMesh1 = somemesh1.rotation.clone().multiply(dirToMesh2FromMesh1);

I THINK that might be the procedure, but I could be completely wrong.  I've always had troubles with this stuff. 

Probably, I should not have posted this extra crap.   I'm too unsure.   :)   Let's see if there are more/wiser comments.

Link to comment
Share on other sites

It's not my PG, but I think you want to slerp to the lookAt - like this?
https://www.babylonjs-playground.com/#UMQ4UR#2

edit: actually, sorry I didn't read your question fully - just wingnuts response more.. oops!  How do you know if you are returning a quaternion or vector3 or do you translate to one?  this is more a question for @Deltakosh if that would be good to have in the framework :)

Link to comment
Share on other sites

Thx BZ and hi!  Yeah, I knew it wasn't your playground. 

I think, what IS yours, BZ, is that you once solved an issue of mine... by multiplying a vec3 rotation... by a vec3 direction.  But maybe it was someone else.

To me, multiplying those two things... seems ridiculous.  But I think it worked for my issue.   I know very little about geometry and projections.  :)

In my last post, I showed how to derive a "lookAt direction"... by subtracting one mesh position... from the other mesh position.  (I learned that when practicing aiming spotlights at constantly orbiting/moving mesh.)

Anyway, once Zephos has an ending .rotation needed to cause mesh1 to lookAt mesh2, he can build/trigger his own animation (which is essentially a slerp, right?).

All in all, I was trying to figure a way to allow Zephos to work COMPLETELY in Euler/Vector3... and avoid Quaternions for now.  *shrug*

The more I talk, the more I confuse people, eh?  The fog of clarity.  heh.  Party on!

Update:  https://www.babylonjs-playground.com/#HH1U5#86

Early tests on my direction * rotation = neededEndingRotationValue ...thoughts... terrible results.  :) Fun, though.  I get to use my handy spinTo() func, again.  :)

Sometimes, I think youngsters like me... should not drive top-fuel dragsters like BJS.  :o  But BJS tastes good with hot dogs, and goes well with Hogan's Heroes reruns, so it's perfect Sunday night fun.

Link to comment
Share on other sites

Well in this case, you can just call Matrix.LookAt to get the new matrix and then decompose it:

Pseudo-code (not tested):

var mat = Matrix.LookAtLH(position, target, BABYLON.Vector3.Up());

var translation;
var rotationQuaternion;
var scaling;

mat.decompose(scaling, rotationQuaternion, translation)

 

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...