1
\$\begingroup\$

I'm using Crafty.js and am trying to point a sprite towards the mouse pointer.

I have a function for getting the degree between two points and I'm pretty sure it works mathematically (I have it under unit tests). At least it responds that the degree between { 0,0} and {1,1} is 45, which seems right.

However, when I set the sprite rotation to the returned degree, it's just wrong all the time. If I manually set it to 90, 45, etc, it gets the right direction..

I have a jsfiddle that shows what I've done so far.

Any ideas?

\$\endgroup\$
4
\$\begingroup\$

With some experimentation, your angle appears to be inverted, but otherwise correct. Negating your angle works:

obj1.rotation = -Engine.degree(pos2, pos1);

And the modified jsFiddle.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.