Jump to content

Best Way To Move Camera By Position


Recommended Posts

What is the BEST way to move a camera by position... When using one of the built-in cameras ... you get good smooth movement... If I try to manually move the camera around using camera.position I don't get the desired effect. I can move the camera around using code like this (delta movements from input):

public updateCameraPosition(camera: BABYLON.FreeCamera, horizontal: number, vertical: number, speed: number): void {
            if (camera != null) {
                var local = camera._computeLocalCameraSpeed() * speed;
                var cameraTransform:BABYLON.Matrix = BABYLON.Matrix.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, 0);
                var deltaTransform:BABYLON.Vector3 = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(horizontal * local, 0, vertical * local), cameraTransform);
                camera.cameraDirection = camera.cameraDirection.add(deltaTransform);


but when I just try set camera position... its funny (I know that vague)... But it would hard to show with all the "Test Project Code" I am using to learn how to move a camera around "Unity-Style"

Just wondering of simply setting camera.position (on a universal camera) should give me free range movement like a unity camera.


Link to comment
Share on other sites

1 hour ago, Deltakosh said:

TLDR: Use cameraDirection :D

I will try that... I have been having betting luck just sticking the camera in an empty mesh and moving the empty mesh around like they would move a camera transform around in unity... But I will try the cameraDirection method so not sooo kludgy using the empty mesh to move camera :)


Link to comment
Share on other sites

  • 11 months later...

@Deltakosh how do you address the issue where you rotate the camera rotation? cameraDirection seems to be agnostic of the current active cameras rotation...how would you update the cameraDirection while also taking into account the cameras current rotation? For instance...let's say I rotate the camera 45 degrees....then I want to update the cameraDirection to move forward 100 units IN that direction. Any helpers in BABYLON to help calculate this?

Link to comment
Share on other sites

Well you can consider that your default direction is (0, 0, 1). so if you want to move forward you will need to add (0, 0, 100). And to take current camera rotation in account, you can simply transform this vector by BABYLON.Matrix.Invert(camera.getViewMatrix())

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