View Single Post
Strafing in a 2D world!?
Old
  (#1)
stefanhendriks
RealBot Author
 
stefanhendriks's Avatar
 
Status: Offline
Posts: 3,088
Join Date: Nov 2003
Location: Netherlands
Default Strafing in a 2D world!? - 23-07-2004

Well here comes non-math-guru stefan asking for some help from Aspirin type people:

I have a guy on the world, location x, y (double's). I can make it walk to any X,Y using sin and cos. I understand how it works , i have a tutorial. I convert real angels into Allegro degrees in order to draw my sprite correctly too. I thought that was hard, but its actually pretty easy...

now the tough part:
I want my guy to strafe all the time in a _STRAIGHT LINE_. meaning, It should strafe 90 degrees to the left/right. The problem is that i do not know how to compute this and make my character actually move.

The processing function looks kinda like this:

Code:
double newx, newy;
newx=x;
newy=y
 
// do something here to move forward
// store these newx and newy
 
// now handle strafing
 
// done...
x=newx;
y=newy;
Mind that the player LOOKS at the mouse pointer. So i know i got a triangle here:

x,y of mouse
x,y of player

any direction to left/right makes an x,y and voila, a triangle. The problem is here.
Ie, when my character is on 100,100, my mouse is on 0,0. I press LEFT (strafe left). My character looks to the UPPER LEFT of the screen now. How do i calculate the 'strafing x,y'?

I thought of circles, but i figured this is not right; at the moment i have some 'circular strafing' that does NOT work properly. Ie, it strafes, in circles, baaaddd. i want it to be 'straight'.

So, lets say; i have this x,y of the mouse. I should start here; i can get the radians of this:

Code:
 
   double delta_x = (mouse_x-x);

   double delta_y = (mouse_y-y);  
 
// radians...
   double r = (atan2(delta_y, delta_x));
(double MUST be used for accurate results, all msvc functions return doubles, when not using them i get fucked up (took me half a day to figure THAT out... sheesh)).

Ok. So now i have the radians to the X,Y of the mouse. So you could say i already have the 2 coordinates right?

So... how do i calculate the 90 degrees to the left? I feel like this is simple to solve, i just lack the knowledge... and the math brain.


Author of RealBot, "Arrakis" and "Dune 2 - The Maker" | co-Founder of Bots-United | Fundynamic | Blog | E-Mail me
  
Reply With Quote