foumfo

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;

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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

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

Share this post


Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.