Jump to content

Moving Camera and Meshes


MattePatte
 Share

Recommended Posts

Hi!

I am having some problems that i think i can solve but i am wondering if there is any simpler solutions.

I want an FPS like camera i am not happy with the standard controls of the FreeCamera even though i have changed the arrow keys to W,A,S,D keys. I don't like that you have to press the left mouse key to rotate it.

 

Is it possible only using moveCamera controls and skip the rotation part of a FreeCamera?

 

If it's not, can i move the camera depending on it's rotation so it always moves forward when pressing the W key.

 

I am also wondering if anyone nows a easy way of implementing a drag and drop for meshes?

 

I just want recommendations of functions and links, don't solve the algorithms for me. :) 

Link to comment
Share on other sites

I am blind and didn't found the cameraDirection property in the Documentation. 

 

So if anyone is interested building a FPS game the controls is the following:

//Pressing Wcamera.cameraDirection= camera.cameraDirection.add(new BABYLON.Vector3(0,0,0.1));//Pressing Scamera.cameraDirection= camera.cameraDirection.add(new BABYLON.Vector3(0,0,-0.1));//Pressing Acamera.cameraDirection= camera.cameraDirection.add(new BABYLON.Vector3(-0.1,0,0));	//Pressing D camera.cameraDirection= camera.cameraDirection.add(new BABYLON.Vector3(0.1,0,0));//The rotation is done by creating mouse variable:var mouse = new BABYLON.Vector2();function onDocumentMouseMove( event ) {   mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;   mouse.y = -( event.clientY / window.innerHeight ) * 2 + 1;}function yourRenderFunction(){   if(mouse.x>0.7 || mouse.x<-0.7 || mouse.y>0.7 || mouse.y<-0.7){      camera.rotation = camera.rotation.add( new BABYLON.Vector3((-mouse.y)/100,0,0));      camera.rotation = camera.rotation.add( new BABYLON.Vector3(0,(mouse.x)/100,0));   }}

Works fine for me.

Link to comment
Share on other sites

Hi again, MP.  Sounds like you are having a good time experimenting with cameras.

 

I have not experimented with free cam .setTarget(some target)...  but maybe... if you re-set the target each time you did a camera move (with that function), maybe that would give you some success.

 

Also, in some ways, it sounds like you are wanting an arcRotate camera, which always maintains its target.  Maybe you want to use that type of camera, and have the W and S keys adjust its .radius (zoom in and out), and the A and D keys adjust its .alpha (orbital pan left and right).  Maybe experiement with that.

 

I am not clear when you say 'forward'.  If your camera is to have a 'locked target', then forward/backward means closer-to, and farther-from... the target.  That is 'zoom' (at/from target).  The other version of 'forward' would mean you want the camera to move along a scene axis, such as +z and -z.

 

If I remember correctly, some people nearby were talking about lookAt, but I see that was mesh.lookAt.  But maybe, you could set camera.parent = some_mesh, and then use the mesh.lookAt (as well as mesh.rotate and mesh.position) to drive that camera.  The camera would do what the mesh does.  You can make that camera handle/gizmo/parent invisible by setting mesh.visibility = 0;

 

Just some ideas... not necessarily good ones.  Be patient with yourself.  It seems to me that you have a good brain for deductive reasoning.  Generally speaking, don't accidentally write too much code.  Let babylon.js do most of the work FOR you.  :)

Link to comment
Share on other sites

//Pressing Wcamera.cameraDirection= camera.cameraDirection.add(new BABYLON.Vector3(0,0,0.1));//Pressing Scamera.cameraDirection= camera.cameraDirection.add(new BABYLON.Vector3(0,0,-0.1));//Pressing Acamera.cameraDirection= camera.cameraDirection.add(new BABYLON.Vector3(-0.1,0,0));	//Pressing D camera.cameraDirection= camera.cameraDirection.add(new BABYLON.Vector3(0.1,0,0));

 

Not a huge improvement but shouldn't it be more efficient to do this:

var step = 0.1;//Pressing Wcamera.cameraDirection.z += step;//Pressing Scamera.cameraDirection.z -= step;//Pressing Acamera.cameraDirection.x -= step;	//Pressing D camera.cameraDirection.x += step;

You would avoid to instanciate a new Vector3.

Link to comment
Share on other sites

Thanks for the ideas Wingnut and the optimization qwaenael.

 

But i still haven't solved the problem.

 

I can't use the arcRotateCamera since it's locked to look at a fixed point. 

 

What i am looking for is a camera which is exactly like the FreeCamera except you don't have to hold the left mouse key to rotate it. 

 

I have managed with the rotation of the camera, but not when i want it to move forward, backwards, left and right.

 

This post describes exactly what i want to do.

http://www-rohan.sdsu.edu/~stewart/cs583/LearningXNA3_lects/Lect15_Ch11_CreateFirstPersonCamera.html

 

The problem is that the camera.cameraDirection don't updates when i am rotating the rotation of the camera. 

I need a Vector that always points forward, in the direction i the camera is looking.

 

This should be the code, except it's not written in JavaScript.

 

if (Keyboard.GetState( ).IsKeyDown(Keys.W))
cameraPosition += cameraDirection * speed;
if (Keyboard.GetState( ).IsKeyDown(Keys.S))
cameraPosition -= cameraDirection * speed;

// Move side to side
if (Keyboard.GetState( ).IsKeyDown(Keys.A))
cameraPosition +=
Vector3.Cross(cameraUp, cameraDirection) * speed;
if (Keyboard.GetState( ).IsKeyDown(Keys.D))
cameraPosition -=
Vector3.Cross(cameraUp, cameraDirection) * speed;

Link to comment
Share on other sites

Hi MP.

 

 

I need a Vector that always points forward, in the direction the camera is looking.

 

  I am confused. Take a look at this little demo.  (there is much extra code in there, please ignore the garbage - it is a messy work area).

 

I am using a standard freeCamera and using standard rotating.  I am printing the camera.rotation Vector3 numbers at the top of the screen.

 

Let us forget about cameraDirection for a moment.

 

No matter how you rotate this camera, the numbers at the top are a vector always pointing camera - forward, yes?  Camera.rotation is not only a SET-able vector.  It is also a GET-able vector.  (if that matters)

 

I will keep reading that link, and your posts... and try to understand better.  I think we are near a solution.  I can smell it.  :)

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