Sign in to follow this  
Lightnet

How to move the mesh direction in camera relate rotation and position?

Recommended Posts

Trying to create a third person camera. Where the character move in right direct when facing the pointer to move while you rotation the camera around.

Need some help how to move the character/pointer where it pointing.

A,S,D,W = character movement.

Mouse orbit the character.

Here a simple demo.

http://www.babylonjs-playground.com/#1RHSYU#0

 

Been trying to figure thing out which hard to do. Trying to work toward game pad joy stick movement. Any Idea?

Share this post


Link to post
Share on other sites

Oh that work but I wanted the camera to follow the camera and it acting odd.

--update-

Here the update the version but I need the joy stick. That can go 360 direction.

http://www.babylonjs-playground.com/#1RHSYU#2

//this breaks move in different direction when reach out side of the center bounder of the plane
//var forward = camera.getFrontPosition(1).subtract(camera.position).normalize(); //does not work bug

//this works
var target = model.position.clone();
var forward = target.subtract(camera.position).normalize();

 

Share this post


Link to post
Share on other sites

Oh cool.

Manage to get the gamepad left joystick to move the pointer around but felt that left and right x axis is not really going right direction as angle little off. But not sure how to deal with the input conflict with orbit controls. Since gamepad left stick y axis does zoom in and out.

Here the working example.

http://www.babylonjs-playground.com/#1BA9FJ#1

Converting angle I have trouble. I wonder if the coding is right since I need to making sure it right way to code.

Share this post


Link to post
Share on other sites

nice script lightnet, thanks. A word of advice- avoid declaring new variables in update structures, because those variables are just getting thrown away after the loop and have to be handled by the browser's garbage collector. At 60 fps with like 10 variables that's 3000 collected every 5 seconds. It causes framerate spikes

Share this post


Link to post
Share on other sites

My way of achieving this was to rely on BabylonJS to do the calculations for me. 

I first get the forward direction ray position of the camera, and then create a version of that without the Y value ('camera' is an instance of BABYLON.ArcRotateCamera) :

var cameraForwardRayPosition = camera.getForwardRay().direction;
var cameraForwardRayPositionWithoutY = new BABYLON.Vector3(cameraForwardRayPosition.x, 0, cameraForwardRayPosition.z);

 

I then apply that to the mesh itself by adding the new camera ray direction data to the current mesh position, and then using 'lookAt' to face in that direction ('model' is an instance of BABYLON.Mesh):

model.lookAt(model.position.add(cameraForwardRayPositionWithoutY), 0, 0, 0);

 

See the playground example below (I changed the controls a bit to use A and D for 'strafing'):

http://www.babylonjs-playground.com/#0D883E

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.