   Making a 2D sprite face another with rotation
  Hi, i have a cannon that i want to face the player at all times. I'm sure this is a pretty simple task I am just not sure how to do it. In case that wasn't clear i have a float that I am  using to track the cannon rotation, when the player moves i want the cannon to rotate to face the player...thanks for any help!   
float MathEngine::computeAngle(D3DXVECTOR2 origin, D3DXVECTOR2 target) {float f = (atan(computeDistance(target, D3DXVECTOR2(target.x, origin.y))/(computeDistance(D3DXVECTOR2(target.x, origin.y), origin)))); if(target.y < origin.y) f *= -1;if(origin.x > target.x) {f += 3.14f;f *= -1;}return f;}maybe not the best routine... but it works for me.it returns f, then apply it to some D3DMatrix2DTranstormation(), just update 'f' at every frame.hope can help.davedit:you can find some maths to compute "f" angle here: http://en.wikipedia.org/wiki/Inverse_trigonometric_functions[Edited by - davj88 on June 16, 2009 11:28:07 AM];Quote:Original post by davj88 *** Source Snippet Removed ***No need for all that. Assuming you have it available, just use atan2():float computeAngle(vector2 origin, vector2 target) {    return atan2(target.y - origin.y, target.x - origin.x);}Note that your method may fail when the target is vertically aligned (or nearly vertically aligned) with the query point.   ;  oh xD thanks for the update. As I said it was not the best routine of course 8)I can assure it does not fail even with aligned objects (I have had some failing methods before, but this one incredibly works).btw the atan2 is incredibly better, I just did not know about it 8)thanks again, I'll switch to it.dav   ;  this doesn't seem to work for me, if its any help, i'm using xna and C#. BTW im guessing that origin is the center of my turret and target is my player position right?   ;  OH, didnt even see the next post... my bad... I do have atan2 with XNA let me try that.   ;  yes it is. origin = turret, target = player.I'm using c++ with d3dx9 and everything works fine with:// out, scaling centre, scaling rotation, scaling, rotation centre, rotation, translationD3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,&spriteCentre,rotation,&trans);Can you post some code maybe or tell how your turret is acting right now :)?   ;  Ok the atan2 method works better, however it still acts a little erratic... let me see about that code   ;  Ok to clarify the following code, the turret is actually a snake head... but the snake head acts like a turret so i just call it a turret, headrotation is the rotation of the snake head (or turret)using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Media;using Microsoft.Xna.Framework.Net;using Microsoft.Xna.Framework.Storage;namespace AlienDome{    class Snake : SpecialAliens    {   public Animation Snakeheadanimation;   public Rectangle[] BodyPartBounds;   public Vector2[] BodyPartpositions;   public bool[] AreBodyPartsAlive;   public float[] bloodtimer;   public int BodyPartcount = 5;   public float HeadRotation = 0f;   public Vector2 OriginofHead;   public Snake()   {  Snakeheadanimation = new Animation(2, 30, 30, 8f);  AlienPosition = new Vector2(300, 300);  BodyPartBounds = new Rectangle[BodyPartcount];  AreBodyPartsAlive = new bool[BodyPartcount];  bloodtimer = new float[BodyPartcount];  BodyPartpositions = new Vector2[BodyPartcount];  for (int i = 0; i < BodyPartcount; i++)  { AreBodyPartsAlive = true; BodyPartpositions = new Vector2(AlienPosition.X, AlienPosition.Y - (30 * i));  }   }   public void UpdateSnake(GameTime gametime, Player player)   {  OriginofHead = new Vector2(Snakeheadanimation.SourceRectangle.Width / 2, 15);  HeadRotation = computeangle(player);  MathHelper.ToRadians(HeadRotation);  Snakeheadanimation.UpdateAnimation(gametime);  for (int i = 0; i < BodyPartcount; i++)  { if (AreBodyPartsAlive) {BodyPartpositions = new Vector2(AlienPosition.X, AlienPosition.Y - (30 * i));for (int j = 0; j < BodyPartcount; j++){    BodyPartBounds[j] = new Rectangle((int)BodyPartpositions[j].X, (int)BodyPartpositions[j].Y, 30, 30);} }  }   }   private float computeangle(Player player)   {  return (float)Math.Atan2(player.PlayerPosition.Y - OriginofHead.Y, player.PlayerPosition.X - OriginofHead.X) * 6;   }    }}this is my entire snake class as you can see i have added the method you told me to add... it rotates alright but it seems like as soon as the player reaches the top left of the screen the snake head starts acting funny;  urg, can u please use source tags? :Pbtw... return (float)Math.Atan2(player.PlayerPosition.Y - OriginofHead.Y, player.PlayerPosition.X - OriginofHead.X) * 6;why do you multiply by 6?andMathHelper.ToRadians(HeadRotation);I'm not sure but if you compute HeadRotation using atan2, it will automatically return radians(http://www.cplusplus.com/reference/clibrary/cmath/atan2/)I don't thinks you should convert them again to radians, but I may be wrong,don't be hard on me :)try and tell us the results !   