foumfo Posted May 11, 2018 Share Posted May 11, 2018 Hey everyone I'm back! I have a mesh that rotates around the Y axis whenever I click and drag along the screen: var currRotationY = 0, currPositionX = 0, clicked = false, originalRotation; canvas.addEventListener("pointerdown", function(evt) { currPositionX = evt.clientX; currRotationY = player.mesh.rotation.y; originalRotation = player.axis.rads; clicked = true; }); canvas.addEventListener("pointermove", function(evt) { if (!clicked) return; //player.mesh is my mesh player.mesh.rotation.y = currRotationY + (evt.clientX - currPositionX) / 500.0; }); canvas.addEventListener("pointerup", function(evt) { clicked = false; }); Whenever my mesh completes a 360deg turn, its rotation.y value continues to grow beyond Math.PI*2. I'd like it to be reset to 0 whenever that happens. Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 11, 2018 Share Posted May 11, 2018 player.mesh.rotation.y %= 2*Math.PI Quote Link to comment Share on other sites More sharing options...
foumfo Posted May 11, 2018 Author Share Posted May 11, 2018 4 minutes ago, JohnK said: player.mesh.rotation.y %= 2*Math.PI where would this line of code fit exactly? Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 11, 2018 Share Posted May 11, 2018 After you set the value. After player.m........./ 500; Quote Link to comment Share on other sites More sharing options...
foumfo Posted May 11, 2018 Author Share Posted May 11, 2018 1 hour ago, JohnK said: After you set the value. After player.m........./ 500; player.mesh.rotation.y = currRotationY + (evt.clientX - currPositionX) / 500.0; player.mesh.rotation.y %= 2*Math.PI ; like this right? Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 12, 2018 Share Posted May 12, 2018 Hopefully you have tried it out by now and it works. If not add a playground and I will have a look. Quote Link to comment Share on other sites More sharing options...
foumfo Posted May 12, 2018 Author Share Posted May 12, 2018 2 hours ago, JohnK said: Hopefully you have tried it out by now and it works. If not add a playground and I will have a look. It works great thanks John! It was so simple. Everything I tried was much more complex. And another thing: when the mesh rotates anticlockwise, rotation.y's value get's reversed negative values. For example Math.PI/2*3 is -Math.PI/2. Essentially it's the same thing. Would it be possible for the rotation.y's value in this case to not be reversed? Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 12, 2018 Share Posted May 12, 2018 What happens when you try it anti-clockwise? Quote Link to comment Share on other sites More sharing options...
foumfo Posted May 12, 2018 Author Share Posted May 12, 2018 Just now, JohnK said: What happens when you try it anti-clockwise? you mean rotate it anti-clockwise right? (Or dragging the pointer to the left) player.mesh.rotation.y get's negative values like -1.1 Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 12, 2018 Share Posted May 12, 2018 I mean does everything work as you want it to without any more code changes? One of the best ways to answer the question 22 minutes ago, foumfo said: Would it be possible for the rotation.y's value in this case to not be reversed? is to try it out and see what happens. If what you want to happen does not happen and you do not know why create a simple playground and ask for help. If what you want to happen does happen Hurray! Quote Link to comment Share on other sites More sharing options...
foumfo Posted May 12, 2018 Author Share Posted May 12, 2018 https://playground.babylonjs.com/#LFMEFD if you rotate it anti-clockwise you'll see that it gets negative values Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 12, 2018 Share Posted May 12, 2018 and if you tried it as is without the if statements it still rotates as expected https://playground.babylonjs.com/#LFMEFD#1 1 hour ago, foumfo said: if you rotate it anti-clockwise you'll see that it gets negative values and the problem is? Quote Link to comment Share on other sites More sharing options...
foumfo Posted May 12, 2018 Author Share Posted May 12, 2018 not a problem, it's just that when it rotates anti-clockwise I'd like the value to be something like that: -Math.PI/2 is equal to 3*Math.PI/2. Unfortunately I can't explain it any other way Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 12, 2018 Share Posted May 12, 2018 Oh I see you do not like negative numbers. What you are asking for is for the angle to be such that 0 <= angle < 2 * Math.PI rather than -2*Math.PI < angle < 2*Math.PI OK https://playground.babylonjs.com/#LFMEFD#2 Quote Link to comment Share on other sites More sharing options...
foumfo Posted May 12, 2018 Author Share Posted May 12, 2018 3 hours ago, JohnK said: Oh I see you do not like negative numbers. What you are asking for is for the angle to be such that 0 <= angle < 2 * Math.PI rather than -2*Math.PI < angle < 2*Math.PI OK https://playground.babylonjs.com/#LFMEFD#2 That did it! All it took was just 2 lines of code. It baffles me just how some complicated problems have such simple solutions 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.