.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   Strafing in a 2D world!? (http://forums.bots-united.com/showthread.php?t=2355)

stefanhendriks 23-07-2004 16:28

Strafing in a 2D world!?
 
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.

stefanhendriks 23-07-2004 17:53

Re: Strafing in a 2D world!?
 
w00t. 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;


@$3.1415rin 23-07-2004 20:09

Re: Strafing in a 2D world!?
 
you could also put that 2D coordinates to the x,y components of a 3d vector ( z = 0) and crossproduct it with (0,0,1). then you'd also get a vector perpendicular to your first vector in the 2d plane

stefanhendriks 23-07-2004 23:04

Re: Strafing in a 2D world!?
 
EUH!? in code? ;)

@$3.1415rin 23-07-2004 23:11

Re: Strafing in a 2D world!?
 
Code:


 
Vector2D VDirection;
Vector VDir3D,VSide;
 
VDir3D.x = VDirection.x;
VDir3D.y = VDirection.y;
VDir3D.z = 0;
 
VSide = CrossProduct(VDir3D,Vector(0,0,1));

not tested, but it should look somewhat similar

the crossproduct of 2 vectors is perpendicular to both arguments and the length of the result is |length1| * |length2| * sin phi where phi is the angel between the arguments ...

sfx1999 27-07-2004 03:16

Re: Strafing in a 2D world!?
 
Why aren't you using matrix math for this?

@$3.1415rin 27-07-2004 11:07

Re: Strafing in a 2D world!?
 
why should one use matrices for this kind of operations ?

mirv 27-07-2004 11:55

Re: Strafing in a 2D world!?
 
Although I still think floats are better than doubles (just don't use msvc), a few optimisations are:
Don't bother converting radians to degrees. Simply put in a number for pi/2 (90deg).
From angles directly, or vector cross products, I'd probably say vector cross product - do you really need to know the magnitude if you're simply adding vector components btw? - as you can pull the x and y values needed to offset the movement directly then, without the need to even use sin/cos/tan/etc.

@$3.1415rin 27-07-2004 15:59

Re: Strafing in a 2D world!?
 
yes, you can use radians instead of degrees with that sin stuff :) most ppl are used to degrees and even HL Engine is using degrees in most cases, that somehow strange ...

the length information was just for to stefan know it, often you just normalize it anyway :)

mirv 27-07-2004 16:37

Re: Strafing in a 2D world!?
 
Radians make much more sense to me.
But yet, normalisation is useful - speaking of which, time to go trawling through cmath to find those functions my little book here doesnt cover...


All times are GMT +2. The time now is 20:55.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.