# Rotation Animation

## Recommended Posts

A question.

I need to rotate an animation, in the direction of another object.

Create a method that allows me to do this. but for a sprite, but when I try to type an object animation, but does not rotate.

watch the video, which have two methods, which are angles to me to turn my animation, you work to perfection and the other not.

Look how the sprite with the same angle, pointing to my hero.

and when I use the animation with the same angle and not pointing to it.

and when I change the method by spinning according to the pointer, rotating smoothly.

the animation changes its angle to the method I use for the angle of the pointer, but the method if it works, to change the angle for the sprite. but to change the angle of the animation is not working.

why?

##### Share on other sites

If you could rephrase what you are trying to ask better it would make it a lot easier to answer your question. Please do not be offended but at the moment I do not understand what you are saying. What is your goal?

##### Share on other sites

I need to make an object of class animation(game.Animation) rotate and point where my hero.

##### Share on other sites

I need to make an object of class animation(game.Animation) rotate and point where my hero.

you need to get the x,y position of both your hero and enemy and use a function like this to calculate the angle between them and then rotate the enemy by this angle

`function angle(cx, cy, ex, ey) {  var dy = ey - cy;  var dx = ex - cx;  var theta = Math.atan2(dy, dx); // range (-PI, PI]  theta *= 180 / Math.PI; // rads to degs, range (-180, 180]  //if (theta < 0) theta = 360 + theta; // range [0, 360)  return theta;}`
##### Share on other sites

Thank you very much for your answer, but look and achieve my goal.

look this is my function, is the angle of my two objects, and my mistake was that it was not updated each positioned himself as the animation was moving.

```
anguloEntre: function(source, target){
var dx = target.x - source.x;
var dy = target.y - source.y;
return Math.atan2(dy, dx);
}```

##### Share on other sites

Did you solve it already? if not I think what you are missing is an update function on your Enemigo class that will call the anguloEntre function on every frame and update the sprite's angle.

##### Share on other sites

yes, I resolved, and if that was my mistake, that by the method call in the wrong place.

Thanks for everything, for your help.

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