.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
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
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
Re: Strafing in a 2D world!?
Old
  (#3)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: Strafing in a 2D world!? - 23-07-2004

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


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

EUH!? in code?


Author of RealBot, "Arrakis" and "Dune 2 - The Maker" | co-Founder of Bots-United | Fundynamic | Blog | E-Mail me
  
Reply With Quote
Re: Strafing in a 2D world!?
Old
  (#5)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: Strafing in a 2D world!? - 24-07-2004

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 ...



Last edited by @$3.1415rin; 24-07-2004 at 00:13..
  
Reply With Quote
Re: Strafing in a 2D world!?
Old
  (#6)
sfx1999
Member
 
sfx1999's Avatar
 
Status: Offline
Posts: 534
Join Date: Jan 2004
Location: Pittsburgh, PA, USA
Default Re: Strafing in a 2D world!? - 27-07-2004

Why aren't you using matrix math for this?
  
Reply With Quote
Re: Strafing in a 2D world!?
Old
  (#7)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: Strafing in a 2D world!? - 27-07-2004

why should one use matrices for this kind of operations ?


  
Reply With Quote
Re: Strafing in a 2D world!?
Old
  (#8)
mirv
Super Moderator
 
mirv's Avatar
 
Status: Offline
Posts: 152
Join Date: Apr 2004
Default Re: Strafing in a 2D world!? - 27-07-2004

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.


mirv the Silly Fish.
  
Reply With Quote
Re: Strafing in a 2D world!?
Old
  (#9)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: Strafing in a 2D world!? - 27-07-2004

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


  
Reply With Quote
Re: Strafing in a 2D world!?
Old
  (#10)
mirv
Super Moderator
 
mirv's Avatar
 
Status: Offline
Posts: 152
Join Date: Apr 2004
Default Re: Strafing in a 2D world!? - 27-07-2004

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...


mirv the Silly Fish.
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com