ricardocouto8 Posted December 28, 2013 Share Posted December 28, 2013 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). 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 More sharing options...
sbat Posted December 28, 2013 Share Posted December 28, 2013 http://en.wikipedia.org/wiki/Cross_product In computational geometry of the plane, the cross product is used to determine the sign of the acute angle defined by three points [...] Link to comment Share on other sites More sharing options...
ricardocouto8 Posted December 28, 2013 Author Share Posted December 28, 2013 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 tells whether lies to the left or to the right of line ", 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 More sharing options...
Fricken Hamster Posted December 28, 2013 Share Posted December 28, 2013 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. Mike and WitheringTreant 2 Link to comment Share on other sites More sharing options...
ricardocouto8 Posted December 28, 2013 Author Share Posted December 28, 2013 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 More sharing options...
Fricken Hamster Posted December 28, 2013 Share Posted December 28, 2013 I wrote that ages ago for as2, glad it worked. Link to comment Share on other sites More sharing options...
rich Posted December 29, 2013 Share Posted December 29, 2013 Posting this from my phone but this may work:Math.nearestAngleBetween(a1, a2, radians) → {number} Link to comment Share on other sites More sharing options...
WitheringTreant Posted January 19, 2015 Share Posted January 19, 2015 Thank you very much for your solution Fricken Hamster.After replacing the "or" statement and removing ":Number"s I managed to get it to work perfectly.I have been trying to figure this out for so damn long.. Link to comment Share on other sites More sharing options...
mischka Posted April 26, 2016 Share Posted April 26, 2016 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 More sharing options...
Recommended Posts