Jump to content

Move mesh relative to camera angle


Hersir
 Share

Recommended Posts

Hi, I am trying to move mesh relative to ArcRotateCamera alpha value. Basically it should move the same direction as mouse is moving (up, down, left, right) , works fine with 0 angle but different angles need to flip x/z and change their directions. What the best way to calculate direction vector from angle and 0 angle direction?

Working example: http://www.babylonjs-playground.com/#W8MYRB#7 (angle 0)

Not working: http://www.babylonjs-playground.com/#W8MYRB#10 (angle 55)

 

I tried to rotate vector using:

const matrix = BABYLON.Matrix.RotationAxis(BABYLON.Axis.Y, camera.alpha);
const movement = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(x, 0, z), matrix);

 

Link to comment
Share on other sites

Ok found how to fix: 

const matrix = BABYLON.Matrix.RotationAxis(BABYLON.Axis.Y, Math.PI - camera.alpha);
const movement = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(x * -1, 0, z * -1), matrix);

Will need to study a bit matrices to understand why Math.PI - camera.alpha was needed
Working version: http://www.babylonjs-playground.com/#W8MYRB#11

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