freetoplay Posted May 13, 2018 Share Posted May 13, 2018 Is lookAt the best function for rotating the mesh around the Y access based on the location of the mouse pointer? My code currently looks something like this: let rotateMesh = function(evt) { let vec3 = new BABYLON.Vector3(0, evt.clientX, 0); mesh.lookAt(vec3); } The problem is that it throws the mesh out of position. How would I rotate it around only the Y position, while keeping the X and Z coordinates the same? If using the lookAt function is not the best approach, what would be a better approach? Quote Link to comment Share on other sites More sharing options...
dbawel Posted May 14, 2018 Share Posted May 14, 2018 Playground scene? Quote Link to comment Share on other sites More sharing options...
Guest Posted May 14, 2018 Share Posted May 14, 2018 Hello, the main problem is that mouse.Y is not in 3D space (world space). You will have to do something like in this demo to project mouse on a plane to get a Y value: https://www.babylonjs-playground.com/#9JNE8S Quote Link to comment Share on other sites More sharing options...
freetoplay Posted May 15, 2018 Author Share Posted May 15, 2018 I tried using scene.pointerY and scene.pointerX, but it doesn't seem to work for me. Here is my current code for the playground, along with comments on where I am running into issues: https://www.babylonjs-playground.com/#9IN03D#6 Is the lookAt function the right function to use in this case? Quote Link to comment Share on other sites More sharing options...
Magilla Posted May 15, 2018 Share Posted May 15, 2018 I don't know enough about Babylon to help with that yet, but I've refactored your event handling code to remove a bug: you never cancel the onMouseMove once the object hit is confirmed. This will hopefully be a bit easier to work with: https://www.babylonjs-playground.com/#9IN03D#7 The console shows that the onMouseMove code only runs when the button is down. Quote Link to comment Share on other sites More sharing options...
Magilla Posted May 15, 2018 Share Posted May 15, 2018 Is this what you needed? https://www.babylonjs-playground.com/#9IN03D#8 Edit: re-reading the original post, I'm beginning to thing you want the duck to look at the mouse. Never mind me... GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
freetoplay Posted May 15, 2018 Author Share Posted May 15, 2018 @Magilla Wow yes, that's almost exactly what I need! The only problem now is I want the duck to face the mouse (right now it seems to be facing the opposite direction from the mouse) and be able to do a 360 degree rotation? Do you know how I can do that? Quote Link to comment Share on other sites More sharing options...
Magilla Posted May 15, 2018 Share Posted May 15, 2018 @freetoplay How do you decide when the mouse is behind the duck for the back 180 degrees of rotation? Quote Link to comment Share on other sites More sharing options...
freetoplay Posted May 16, 2018 Author Share Posted May 16, 2018 @Magilla Actually, it works like I expected, I just had to increase the speed from 0.01 ?. Quick question though, what would be the best way to make the rotation more smooth? Like now it looks kind like it's lagging. Thanks for all the help! Quote Link to comment Share on other sites More sharing options...
Magilla Posted May 16, 2018 Share Posted May 16, 2018 It's super smooth for me already, so I have no idea how to start helping with that ? Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 16, 2018 Share Posted May 16, 2018 engine.runRenderLoop(function () { scene.render(); }); You do not need this code within a playground as the playground wrapper already supplies this. See if removing it smooths the scene as you are double calling. 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.