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

. I figured it out myself. Actually i was close:

in theory:
- calculate radians to the mouse x, y
- convert to degrees
- do + or minus 90 degrees
- convert back to radians
- calculate this position now (with length,etc)
- now we know the 'destination x,y' , so we have our triangle. Now we can move straight to this line

in code:
(if you got any stuff to optimize it, please let me know)
Code:
  // calculate the angle ; increase it; calculate new x and y out from it.. phew
   double delta_x = (x-mouse_x);
   double delta_y = (y-mouse_y);  
   // calculate radians
   double r = (atan2(delta_y, delta_x));	 
   // degrees
   double degrees = r * (180 / PI);
   // length for circle
   double l = length(x, y, mouse_x, mouse_y);
   // depending on left/right (add 90 degrees to 'strafe')
   if (fSideSpeed > 0.0)
	degrees += 90;
   else
	degrees -= 90;
   
   // figure out radians again (strafed ones)
   double radians = degrees * (PI / 180);
   // With this we can calculate the vx and vy
   double vx = (cos (radians)) * (l);
   double vy = (sin (radians)) * (l);
   
   // include X and Y
   vx+=x;
   vy+=y;
   // We know now the x and y where to go to. Now use radians again to move to it (like
   // we do when we move straight to it.
   
   delta_x = (vx-x);
   delta_y = (vy-y);  
   // calculate radians
   r = (atan2(delta_y, delta_x));  
   float fSpeed = fabs(fSideSpeed);
   double nextx = (cos (r)) * fSpeed;
   double nexty = (sin (r)) * fSpeed;
   iNewX = iNewX + nextx;
   iNewY = iNewY + nexty;


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