# Reseting a mesh's rotation after a full circle

## Recommended Posts

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;

currPositionX = evt.clientX;
currRotationY = player.mesh.rotation.y;
clicked = true;
});

if (!clicked) return;
//player.mesh is my mesh
player.mesh.rotation.y = currRotationY + (evt.clientX - currPositionX) / 500.0;
});

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.

##### Share on other sites

player.mesh.rotation.y %= 2*Math.PI

##### Share on other sites
4 minutes ago, JohnK said:

player.mesh.rotation.y %=﻿ 2*Math.PI

where would this line of code fit exactly?

##### Share on other sites

After you set the value. After

player.m........./ 500;

##### Share on other sites
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?

##### Share on other sites

Hopefully you have tried it out by now and it works. If not add a playground and I will have a look.

##### Share on other sites
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?

##### Share on other sites

What happens when you try it anti-clockwise?

##### Share on other sites
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

##### Share on other sites

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!

##### Share on other sites

if you rotate it anti-clockwise you'll see that it gets negative values

##### Share on other sites

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?

##### Share on other sites

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

##### Share on other sites

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

##### Share on other sites
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

That did it! All it took was just 2 lines of code. It baffles me just how some complicated problems have such simple solutions

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.