Jump to content

Calculate fastest side to rotate


ricardocouto8
 Share

Recommended Posts

Hello guys!

 

Probably more a math problem than a phaser one, but still I'm breaking my head finding a solution. Maybe you can help me!

 

Imagine I have a sprite pointing towards the direction of the red arrow. I want to make the sprite rotate slowly towards the pointer (in this image the green arrow), and to have it rotate clockwise/counter-clockwise depending on which way is faster (in this case counter-clockwise).

 

problem1.png

 

How can I calculate if the faster way is clock/counter-clockwise, providing the sprite can be pointing wherever and the pointer could be anywhere? If I was not clear I can try to clarify better :)

 

Thanks in advance!

 

 

Link to comment
Share on other sites

Ugh, I need to review my math!

 

Thanks for the quick reply, though unfortunately I am still unable to get the results I need. Maybe again some math noobism...

 

What I did was this, and it is not working properly...

function rotationSide(object, pointer){	oldAngle = object.rotation;	//Convert the angle to radians ranging from 0 to 2*PI	if (oldAngle < 0){		oldAngle = Math.abs(oldAngle);	}else{		oldAngle = 2*Math.PI - oldAngle;	}	x1 = object.x;	y1 = object.y;	x2 = object.x + Math.cos(oldAngle);	y2 = object.y + Math.sin(oldAngle);	x3 = pointer.worldX;	y3 = pointer.worldY;	return (x2-x1)*(y3-y1)-(y2-y1)*(x3-x1);}

As defined by http://en.wikipedia.org/wiki/Cross_product#Computational_geometry, "the sign of d7cfb23450291969e5f005706414a6ab.png tells whether b79809281d549131ba7f2c655c1f2083.png lies to the left or to the right of line b9b0a6cc54e34c071a3c977f2eb905d1.png", or in this case, the sign of the return should tell me if the pointer is to the left or to the right of the direction my sprite is pointing. But it is not doing that at the moment :(

 

Thanks!

Link to comment
Share on other sites

function lerp_dir( cur_dir:Number , tar_dir:Number , inc:Number){		if ( Math.abs( tar_dir - cur_dir) <= inc or Math.abs( tar_dir - cur_dir) >= (360 - inc))	{		cur_dir = tar_dir;	}	else	{		if ( Math.abs( tar_dir - cur_dir) > 180)		{			if (tar_dir < cur_dir)			{				tar_dir += 360;			}			else			{				tar_dir -= 360;			}		}		if ( tar_dir > cur_dir)		{			cur_dir += inc;		}		else		{			if ( tar_dir < cur_dir)			{				cur_dir -= inc;			}		}	}	return cur_dir;}

This was what I used a long time ago. Can't guarantee anything.

Link to comment
Share on other sites

Hello!

 

That code worked perfectly. I used sprite.rotation as cur_dir, angleToPointer(sprite) as tar_dir and a number like 0.05 as inc (increment). Also I changed 180 and 360 to Math.PI and 2*Math.PI to make it work with radians :)

 

Thanks sbat and Fricken Hamster for the quick replies!

Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

Hey there,

i used Fricken Hamster's Code in CreateJS, like this :

    function rotateDirection( current_angle, target_angle, increment){
        if ( Math.abs( target_angle - current_angle) <= increment || Math.abs( target_angle - current_angle) >= (360 - increment))	{
            current_angle = target_angle;
        }
        else
        {
            if ( Math.abs( target_angle - current_angle) > 180)		{
                if (target_angle < current_angle)			{
                    target_angle += 360;
                }
                else
                {
                    target_angle -= 360;
                }
            }
            if ( target_angle > current_angle)		{
                current_angle += increment;
            }
            else
            {
                if ( target_angle < current_angle)			{
                    current_angle -= increment;
                }
            }
        }
        return current_angle;
    }

I don't know why, and exactly when it happens but there is a small problem. I have an Circle Shape that rotates it's face into mouseposition. When i turn my mouse several times around

the circle shape then SOMETIMES it doesn't turn incrementaly, but jumps directly about 180 degrees or somewhat. That does not look cool at all.

The function seems okay for me, so does anyone know what the problem is? I could make a small JSFiddle if that would be better for understanding my problem,

but the problem is, it does not happen always....it's on a specific degree when i turn the mouse and it jumps directly to it. I guess it has something to do with the first line

where current_angle is set equal to the target_angle....but why does it make so big jumps? the IF question only comes if the subtraction is smaller increment?

Did anyone else notice my problem, when using the code?

 

Greetings Mischka

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...