malkomalko Posted March 2, 2014 Share Posted March 2, 2014 Hello Forum! Babylon, Blender, and Gamedev are all new to me, but I'm soaking up a lot and taking it all in stride. I've been playing around with all 3 and learning a bunch from reading code (I'm mostly a developer/designer), but I was wondering if somebody could assist me in answering a few questions. I figured out how to make a simple scene in Blender and assign physics to certain objects (camera included), so that I could load up the scene and walk around. A few questions came out of this simple experiment that I'd love to try to figure out. 1) What's the best way to have the camera follow an object? Would it be something like: When you move the camera, also move the object by x/y/z depending on the direction of the movement. 2) How do you alter/override the default controls of the camera? I find that the movement is a little to loose for my like and would like to alter the speed and play around with getting a feel of my character movement. Would it be something like: Grab access to my camera objects, and then alter some combination of attributes? Thank you very much, really really loving this stuff. Quote Link to comment Share on other sites More sharing options...
Xanmia Posted March 2, 2014 Share Posted March 2, 2014 1) I believe you just want to use: camera.target = your object Not sure on 2... Quote Link to comment Share on other sites More sharing options...
Dad72 Posted March 2, 2014 Share Posted March 2, 2014 You can attach a camera to a character with 'relative' in order for the camera to follow. //Exemplecamera.parent = actor;For the speed of the camera you can use speed. camera.speed = 2;// The default speed is 1Or if your camera is attached to the character, you can advance your character more quickly and the camera will follow. Here is the code to move a character in all directionsvar keys={letft:0,right:0,arriere:0,forward:0};window.addEventListener('keydown',function(event){ var ch = String.fromCharCode(event.keyCode); if (ch == "Q") keys.left=1; if (ch == "D") keys.right=1; if (ch == "S") keys.arriere=1; if (ch == "Z") keys.forward=1;}); window.addEventListener('keyup',function(event){ var ch = String.fromCharCode(event.keyCode); if (ch == "Q") keys.left=0; if (ch == "D") keys.right=0; if (ch == "S") keys.arriere=0; if (ch == "Z") keys.forward=0;}); scene.registerBeforeRender(function(){ if (keys.forward==1){ var posX = Math.sin(cube.rotation.y); var posZ = Math.cos(cube.rotation.y); cube.position.x += posX; cube.position.z += posZ; } if (keys.arriere==1){ var posX = Math.sin(cube.rotation.y); var posZ = Math.cos(cube.rotation.y); cube.position.x -= posX; cube.position.z -= posZ; }}); Quote Link to comment Share on other sites More sharing options...
malkomalko Posted March 2, 2014 Author Share Posted March 2, 2014 Thanks everybody, I will try both of your suggestions. Quote Link to comment Share on other sites More sharing options...
malkomalko Posted March 2, 2014 Author Share Posted March 2, 2014 Also, for future note, did both of you know about the camera by reading the documentation or was it in an example somewhere? I'm still trying to feel my way around the best way to learn Babylon. I found http://doc.babylonjs.com which I think is the latest documentation. Cheers Quote Link to comment Share on other sites More sharing options...
Xanmia Posted March 2, 2014 Share Posted March 2, 2014 I found camera.target through the tutorials: https://github.com/BabylonJS/Babylon.js/wiki/05-CamerasOtherwise the documentation link you posted has been helping me a lot. As well as the examples on the main babylonjs page.And, of course, digging through this forum. Quote Link to comment Share on other sites More sharing options...
malkomalko Posted March 2, 2014 Author Share Posted March 2, 2014 Following both your suggestions I got an object moving. Two more follow up questions since I couldn't figure it out while playing. 1) How do you turn off the default controls of the camera (mouse/left/right/up/down).2) Now I need to figure out how to make my cube (player) have collision detection. It says it does but it doesn't seem to work. Either way, this is fun! Quote Link to comment Share on other sites More sharing options...
Xanmia Posted March 3, 2014 Share Posted March 3, 2014 1)You probably have the below some where in your code, just comment it out or remove it. That will stop the mouse/left/right/up/down controls. scene.activeCamera.attachControl(canvas); 2)Are you using the below?meshObject.intersectsMesh(otherMeshObject, true)I am guessing you were looking at this tutorial for collisions: https://github.com/BabylonJS/Babylon.js/wiki/10-Object-collisions Quote Link to comment Share on other sites More sharing options...
Dad72 Posted March 3, 2014 Share Posted March 3, 2014 You also have: scene.activeCamera.detachControl(canvas); //For detach the control of the camera 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.