c# - Velocity at an angle -
i'm trying find velocity @ angle.
to i'm trying use triangle, i'm running road blocks.
this code moves object
velocity = new vector3((float)math.cos(mathc.angletopoint(enemyobject.transform.position, playerposition)), (float)math.sin(mathc.angletopoint(enemyobject.transform.position, playerposition)), 0);
this angletopoint method
public static double angletopoint(vector3 startingpoint, vector3 endpoint) { double hypotenuse = linelength(startingpoint, endpoint); double adjacent = linelength(startingpoint, new vector3(endpoint.x, startingpoint.y, 0)); double opposite = linelength(endpoint, new vector3(endpoint.x, startingpoint.y, 0)); double angle = adjacent / hypotenuse; return math.acos(degreetoradian(angle)); }
and linelength method
public static float linelength(vector2 point1, vector2 point2) { return math.abs((float)math.sqrt(math.pow((point2.x - point1.x), 2)+math.pow((point2.y - point1.y),2))); }
i getting lot of nan errors, , movement not behave how want to.
edit : have solved problem. i'm not sure explained myself well, allow me attempt again. not physicist excuse me if of these terms wrong.
my goal have object move @ constant speed regardless of orientation. can assume person holding gun in direction north (0, 1) , rotate 45 degrees , fire gun. speed of gun scalar quantity, such 50 units per second. wanted know x , y values of vector needed have movement, in direction. velocity (0, 50) rotated 45 degrees right.
i know velocity.x = speedcos(angle) , velocity.y = speedsin(angle), needed find value of angle.
to this, line of thinking make right triangle based off starting position , destination, find angle using trig.
public static double angletopoint(vector3 startingpoint, vector3 endpoint) { double hypotenuse = linelength(startingpoint, endpoint); double adjacent = linelength(startingpoint, new vector3(endpoint.x, startingpoint.y, 0)); double opposite = linelength(endpoint, new vector3(endpoint.x, startingpoint.y, 0)); double angle = adjacent / hypotenuse; return math.acos(degreetoradian(angle)); }
this code making right triangle. reason opposite there though it's not used because attempted multiple ways try accomplish this, kept encountering errors. after making right triangle, takes adj , hypo obtain cos(adj/hypo), call acos on give me angle in radians. didn't work however, did not return angle , created lot of nan errors.
after searching found thread , explanation of atan2
https://gamedev.stackexchange.com/questions/14602/what-are-atan-and-atan2-used-for-in-games
http://i.stack.imgur.com/xqiwg.png
this graph made me realize if translated line startingpoint, endpoint startingpoint = 0 call atan2 , returned desired angle. code accomplishes that.
public static double getangletopoint(vector3 startingpoint, vector3 endpoint) { endpoint -= startingpoint; return math.atan2(endpoint.y, endpoint.x); } velocity = new vector3(speed * (float)math.cos(mathc.getangletopoint(startingposition, playerposition)), speed * (float)math.sin(mathc.getangletopoint(startingposition, playerposition)), 0);
is helpful : how calculate velocity @ angle ?
(sounds gaming development related question, might more here) https://gamedev.stackexchange.com/
(or maybe here too) https://math.stackexchange.com/
Comments
Post a Comment