Jump to content

freecamera - adding a up and down function


Zimbofly
 Share

Recommended Posts

Hi all....

 

I have freecamera with some custom hotkeys to move the 'first person shooter' .

 

Pageup button - the camera floats up

Pagedown button - the camera 'crouches' down.

 

BUT - when I press forward or back keys, camera returns to original height (Y.position) instead of continuing at the new height.

window.addEventListener("keydown", function (event) {//pageup        if (event.keyCode === 33) {        	if (camera.position.y < 6) {        		animateCameraPositionAndRotation(camera, camera.position,					new BABYLON.Vector3(camera.position.x, camera.position.y + 2, camera.position.z),					camera.rotation,					camera.rotation);        	}        }        //pagedown        if (event.keyCode === 34) {        	if (camera.position.y > 3) {        		animateCameraPositionAndRotation(camera, camera.position,					new BABYLON.Vector3(camera.position.x, camera.position.y - 2, camera.position.z),					camera.rotation,					camera.rotation);        	}        }}, false);var animateCameraPositionAndRotation = function (camera, fromPosition, toPosition,                                                 fromRotation, toRotation) {        //needs work to even out the time of animation between short and long distances        var animTime        if (fromPosition - toPosition > 10) { animTime = 40 }        else { animTime = 50 }        var animCamPosition = new BABYLON.Animation("animCam", "position", animTime,                                  BABYLON.Animation.ANIMATIONTYPE_VECTOR3,                                  BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);        var keysPosition = [];        keysPosition.push({            frame: 0,            value: fromPosition        });        keysPosition.push({            frame: 100,            value: toPosition        });        animCamPosition.setKeys(keysPosition);        var animCamRotation = new BABYLON.Animation("animCam", "rotation", animTime,                                  BABYLON.Animation.ANIMATIONTYPE_VECTOR3,                                  BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);        var keysRotation = [];        keysRotation.push({            frame: 0,            value: fromRotation        });        keysRotation.push({            frame: 100,            value: toRotation        });        animCamRotation.setKeys(keysRotation);        camera.animations.push(animCamPosition);        camera.animations.push(animCamRotation);        camera.checkCollisions = true;        scene.beginAnimation(camera, 0, 100, false);    };

any ideas how I can get the camera to continue at the new height after pageup or down pressed?

eg - I want to fly up into the sky, then continue forward at that height :)

many thanks

Link to comment
Share on other sites

Heya Zimbofly, how ya been?  I hope well.

 

I made a quick playground scene from your code... http://www.babylonjs.com/playground/#1EVRXC .  It seems to be working properly. I adjusted the upper range from 6 to 20... no big deal.  After each page-up (and later page-down), all cursor in/out actions seem to start from the new height (work properly).  I have not seen any unusual camera y-position stuff, so far.

 

Looks like good code to me... but... I am certainly not a pro.  Others may be able to see something that I am missing.  Make adjustments and more saves to that playground scene above, as wanted, of course.

 

Right click here and save link target... to get the same version of Babylon.js that is used in the Babylon.js Playground.  *shrug*

 

And keep us posted on your discoveries, okay Z?  Cool.

 

Update: Zimbofly, you don't HAVE TO use animation (you probably already know that).  Take a look at http://www.babylonjs.com/playground/#1EVRXC#1 .  Since the page-up and page-down keys can be held-down, you can just force the incrementing and decrementing of the camera.position.y.  It's not as smooth as animating, but it still gets the job done.  I also did a #2 (ahem) that re-sets the camera.target to 0,0,0 after every page-up and page-down.  It was boring.  :)  ok bye again.

Link to comment
Share on other sites

Hey Wingnut - You rock dude!

thanks - things are great - real busy :)   

Hope you too...?

 

thanks for taking the time to do that.

 

So - I've found the culprit...

 

camera.applyGravity = true;camera.ellipsoid = new BABYLON.Vector3(1, 3, 1);
 
these 2 line together were forcing the height to all ways return to the same.
after removing them, then, just like your examples - the height function began to work as expected.
 
I thought camera.ellipsoid was needed for collision checking - but works fine without.
 
Most of all - what you've done by taking everything back to basics, was exactly what i needed to do (and learn to do with all problems)
simplify - take it back to basics, then work out which line/s is causuing problems :D
 
Thanks again man!
 
 
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...