Carharttguy Posted July 20, 2017 Share Posted July 20, 2017 Hello I'm porting an application from AppGameKit to BabylonJS. Now I came to the point of moving the camera on keypresses. In AGK I could use the local position of a mesh. This is pseudocode If keypress = up Then Camera.MoveLocalZ(5) Endif This moves the camera 5 units forward on it's local axes, so it's always going forward, no matter the direction. After a lot of reading and searching the docs, I found this: http://doc.babylonjs.com/overviews/how_rotations_and_translations_work#more-advanced-positioning So locallyTranslate seems awesome. So I cleared the inputs on the camera and tried something like this: window.addEventListener("keydown", ProcessKeyDown, false);function ProcessKeyDown(kbe: KeyboardEvent){ switch(kbe.keyCode) { case 38: //UP gEditor._camera.locallyTranslate(new BABYLON.Vector3(0, 0, 5)); }} But I get an error on locallyTranslate that it's not a function. I get the same error on gEditor._camera.Translate. That also isn't a function it seems. Or is .Translate and .locallyTranslate not applicable on a camera? Thanks Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted July 20, 2017 Share Posted July 20, 2017 player = { mesh:player_box, speed: { f:0, s:0, u:0, } }; //PHYSICS START player_box.body = player_box.setPhysicsState(BABYLON.PhysicsImpostor.SphereImpostor, { mass: 1.0, friction: 0.0001, restitution: 0.0}); player_box.body.grounded = false; player_box.body.collisionResponse = 0; player_box.body.addEventListener("collide", function(e){ player_box.body.grounded = true; player_box.body.collisionResponse = 1; }); //CONTROLS keys = {}; keys.up = false, keys.down = false, keys.left = false, keys.right = false, keys.space = false, keys.w = false, keys.s = false, keys.a = false, keys.d = false, keys.ctrl = false; function onKeyDown(evt) { console.log(evt.keyCode); switch (evt.keyCode){ case 37: // left keys.left = true; break; case 39: //right keys.right = true; break; case 38: //up keys.up = true; break; case 40: //down keys.down = true; break; case 32: //space keys.space = true; break; case 87: //w keys.w = true; break; case 83: //s keys.s = true; break; case 65: //a keys.a = true; break; case 68: //d keys.d = true; break; case 17: //ctrl keys.ctrl = true; break; } } function onKeyUp(evt) { switch (evt.keyCode){ case 37: // left keys.left = false; break; case 39: //right keys.right = false; break; case 38: //up keys.up = false; break; case 40: //down keys.down = false; break; case 32: //space keys.space = false; break; case 87: //w keys.w = false; break; case 83: //s keys.s = false; break; case 65: //a keys.a = false; break; case 68: //d keys.d = false; break; case 17: //ctrl keys.ctrl = false; break; } } window.addEventListener("keydown", onKeyDown); window.addEventListener("keyup", onKeyUp); //console.log(player_box.body); //console.log(camera); settings = { accel : 1, speeds :{ normal :{ f: 50.0, b: -20.0, s: 30.0, }, run :{ f: 1.0, b: 0.45, s: 0.65, }, walk :{ f: 0.3, b: 0.1, s: 0.2, }, }, }; //CONTROL RESPONSE scene.registerBeforeRender(function(){ //console.log(player_box.position); if(player_box.body.collisionResponse){ player_box.body.grounded = true; player_box.body.velocity.y = 0; }else{ player_box.body.grounded = false; } if(player_box.body.grounded || gravity.y == 0){ if(keys.w){ if(player.speed.f < settings.speeds.normal.f){ player.speed.f += settings.accel; }else{ player.speed.f = settings.speeds.normal.f; } }else if(keys.s){ if(player.speed.f > settings.speeds.normal.b){ player.speed.f -= settings.accel; }else{ player.speed.f = settings.speeds.normal.b; } } if(keys.a){ if(player.speed.s > settings.speeds.normal.s*-1){ player.speed.s -= settings.accel; }else{ player.speed.s = settings.speeds.normal.s*-1; } } if(keys.d ){ if(player.speed.s < settings.speeds.normal.s){ player.speed.s += settings.accel; }else{ player.speed.s = settings.speeds.normal.s; } } if(keys.space){ if(gravity.y != 0 && player_box.body.grounded){ player_box.body.collisionResponse = 0; var jumpVector = v3(0,6.5,0); player_box.applyImpulse(jumpVector, player_box.position); }else{ if(player.speed.u < -2){ player.speed.u -= settings.accel; }else{ player.speed.u = -2; } } } if(keys.ctrl){ if(gravity.y != 0 && player_box.body.grounded){ player.speed.f*=0.5; player.speed.s*=0.5; }else{ if(player.speed.u > 2){ player.speed.u += settings.accel; }else{ player.speed.u = 2; } } } player.speed.f*=0.82; player.speed.s*=0.82; player.speed.u*=0.82; if(player.speed.f<0.05 && player.speed.f > -0.05){ player.speed.f = 0; } if(player.speed.s<0.05 && player.speed.s > -0.05){ player.speed.s = 0; } var forward = scene.activeCamera.getTarget().subtract(scene.activeCamera.position).normalize(); forward.y = 0; var right = BABYLON.Vector3.Cross(forward, scene.activeCamera.upVector).normalize(); right.y = 0; var f_speed = 0, s_speed = 0, u_speed = 0; if(player.speed.f != 0){ f_speed = player.speed.f; } if(player.speed.s != 0){ s_speed = player.speed.s; } if(player.speed.u != 0){ u_speed = player.speed.u; } move = (forward.scale(f_speed)).subtract((right.scale(s_speed))).subtract(scene.activeCamera.upVector.scale(u_speed)); player_box.body.velocity.x = move.x; player_box.body.velocity.z = move.z; player_box.body.velocity.y = move.y; } if(keys.ctrl){ camera.position = v3(player_box.position.x,player_box.position.y+1,player_box.position.z); }else{ camera.position = v3(player_box.position.x,player_box.position.y+2,player_box.position.z); } This might be over kill... but this is a controller that I have used in the past. You need to get a forward vector and do something with that! Quote Link to comment Share on other sites More sharing options...
jerome Posted July 20, 2017 Share Posted July 20, 2017 these functions are for meshes only Quote Link to comment Share on other sites More sharing options...
Carharttguy Posted July 20, 2017 Author Share Posted July 20, 2017 @Pryme8 Thanks for your answer. But to be honest, I completely fail to see what your solution does. I have very little BabylonJS experience, and it looks like your moving a physics body that has a camera attached to it? @jerome Thanks! It's a real shame tho, it would be so easy So I guess the only way to mova a camera is using those input systems? I already read the tutorial 3 times and looks super complex for what I want. Quote Link to comment Share on other sites More sharing options...
adam Posted July 20, 2017 Share Posted July 20, 2017 var distance = 5; camera.position.addInPlace(camera.getDirection(BABYLON.Axis.Z).scale(distance)); Carharttguy and Pryme8 2 Quote Link to comment Share on other sites More sharing options...
Carharttguy Posted July 20, 2017 Author Share Posted July 20, 2017 @adam That is spot on what I need. But wow, I'm impressed, it would"ve taken me quiet some time to come up with such a solution. I have some new material to research in the class ref. Thanks everybody! This forum is crazy fast with replies. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted July 20, 2017 Share Posted July 20, 2017 My bad, I miss read your first post! >_< I was rushing out the door to get to lunch, good thing adam knows what hes talking about! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.