   Heading vector from a rotation
  Hi, how can i find a vector in the direction an object is faceing if i know the angle its rotated by in 2D? Thanks   
Quote:Original post by neilukHi, how can i find a vector in the direction an object is faceing if i know the angle its rotated by in 2D? Thanksx = std::cos(angle);y = std::sin(angle);Remember that c++ trig functions work with radians, not degrees.   ;  yeah thats what i thought, must be something else up with my code, any help people? Basically its a function to rotate an enemy ship towards the player in a 2D shoot'em up. Is it ok just to set the Z values of the vectors to 0 as i have, as in actual fact both the player and emenies are positioned further back from the screen but im only wanting to align them in 2D. Strange behaviour shown by the enemies currently includes: continually rotating in a single direction, continually rotating in one direction and then changing direction of rotation when the player passes to the other side of them, or initially rotating and then staying in a set position untill the player passes them at a certain point. Any help much appreciated: [NB: % is dot product operator for 2 vectors]// **************************************************// * Rotate and shoot at the player// **************************************************void Enemy ::Shoot(Player *player, float dt){// Get vector pointing from this enemy to the playerVector direction = player->position - position;direction.z = 0.0;direction.Normalize();// Calculate vector in the direction the enemy is facingfloat radAngle = angle / RAD2DEG;Vector faceing = Vector(cos(radAngle), sin(radAngle), 0.0);faceing.Normalize();// Choose direction to turn using dot productif(acos(faceing%direction) > 0){angle--;}else if(acos(faceing%direction) < 0){ angle++;}}PS. im pretty sure the vector functions im using are correct as they are taken from prewritten code from a programming book, Thanks.[Edited by - neiluk on June 4, 2006 5:24:32 PM];  A few observations:- You may want 'position - player->position' rather than 'player->position - position'- Depending on the value of RAD2DEG, it may be that you're supposed to multiply by it rather than divide- You don't need to normalize the facing vector after you create it- I take it angle is an int; I can't remember what happens when you divide an int by a float in c++, but you might cast 'angle' just to be safe- acos(dot(a,b)) gives you the unsigned angle, which isn't of much use here; you want angle = atan2(perp_dot(a,b),dot(a,b))- This is IMO, but overloading operator%() for the dot product is bad, bad, bad (again, IMHO)Post back if after addressing those issues it still doesn't work...[Edited by - jyk on June 4, 2006 7:46:10 PM];Quote:Original post by jykA few observations:- You may want 'position - player->position' rather than 'player->position - position'Is that a suggestion to check if its wrong or are you saying its deffinatly wrong?? Im pretty sure thats the way i want it for a vector from the enemy too the player, did try changing it but didnt help anyQuote:- Depending on the value of RAD2DEG, it may be that you're supposed to multiply by it rather than divide is def divide ive used it already elsewhere in my programQuote:- You don't need to normalize the facing vector after you create itbut it wudnt cause any errors surely??Quote:- I take it angle is an int; I can't remember what happens when you divide an int by a float in c++, but you might cast 'angle' just to be safehave changed this just incase, dont know if its made any differenceQuote:- acos(dot(a,b)) gives you the unsigned angle, which isn't of much use here; you want angle = atan2(perp_dot(a,b),dot(a,b))guessing this is where my problem is then, was looking in another thread and got the idea of what this means, but how can i calculate perp(a), wud be gratefull if you could explain :)[Edited by - neiluk on June 6, 2006 4:04:37 AM];  Ummm.... I don't think that's how you're suppose to rotate towards an object, correct me if i'm wrong://both must be normalized, if necessaryt=target-position;t.normalize();d=direction_veoctor;//dot product of the perpendicularif (d.y*t.x-d.x*t.y<0)angle-=1;elseangle+=1;;Quote:Original post by TheC00L1Ummm.... I don't think that's how you're suppose to rotate towards an object, correct me if i'm wrong://both must be normalized, if necessaryt=target-position;t.normalize();d=direction_veoctor;//dot product of the perpendicularif (d.y*t.x-d.x*t.y<0)angle-=1;elseangle+=1;No, you appear to be right, thanks :) rating++ for you kind sir   ;Quote:Is that a suggestion to check if its wrong or are you saying its deffinatly wrong?? Im pretty sure thats the way i want it for a vector from the enemy too the player, did try changing it but didnt help anyI'm pretty sure it's wrong; you want the vector from the player to the enemy, not the other way around. If changing it didn't fix the problem, it's probably due to other errors.Quote:but it wudnt cause any errors surely??A vector constructed from the sine and cosine from an angle is already normalized.Quote:guessing this is where my problem is then, was looking in another thread and got the idea of what this means, but how can i calculate perp(a), wud be gratefull if you could explain :)TheCOOL1 posted the perp-dot solution; if you only need the sign of the angle and not the angle itself, you can just use the code he posted.[Edit: Looks like TheCOOL's solution worked. If you need the actual angle (say, to determine how fast to turn), you can use the atan2() version I posted earlier.][Edited by - jyk on June 6, 2006 4:14:51 AM];Quote:Original post by jykQuote:Is that a suggestion to check if its wrong or are you saying its deffinatly wrong?? Im pretty sure thats the way i want it for a vector from the enemy too the player, did try changing it but didnt help anyI'm pretty sure it's wrong; you want the vector from the player to the enemy, not the other way around. If changing it didn't fix the problem, it's probably due to other errors.yeah i did want it the other way round, they were turning correctly using TheCOOL1s solution but had there backs faceing the player not there fronts, thanks for you help also;  hi,i don't remember it well but,try itv = player->position - position;if( v.y != 0)    angleInRad = atan( v.x / v.y );else{    if( v.x > 0)   angleInRad = 0;    else if( v.x <   