Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (521) Games in Android Showcase (127) games submitted by our members Games in WIP (589) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Calculating Strafe Angle Vector for Top Down Shooter?  (Read 4282 times) 0 Members and 1 Guest are viewing this topic.
gridur

Senior Newbie

Groovy

 « Posted 2010-03-27 22:25:38 »

Hey!

It was ages since I've posted anything here at JG, but I think I'll have to get active again!
Anyway, I have a problem that I've pondered on for quite some time (actually made me trash a project a couple of years ago) this being rooted in my bad knowledge in vector math really. But I'm pretty sure theres a lot of ppl here that can help me out with this little math bugger.

I'm working on a game where I control a character through a Top View layout. I got the UP and DOWN controls working smooth and fine calculating the angle vector between the player (thats in the center of the screen) and the mouse cursor, By doing this when I push the UP key the player moves towards the mouse cursor (a cross hair in the game). And when pressing DOWN the player moves away from the cursor.

What I want to do now is implement a Strafe like movement, wheres the Player moves sideways in a 90 degree angle by pressing RIGHT and LEFT buttons.
So how do I calculate the angle vector that is 90 degrees in both ways to make this work really?

And I have to warn you, I'm pretty bad at working with mathematical formulas, so It would be cool to see the solution in pseudo code really hehe. Anyway, heres a code snippet where I calculate the basic angle vector that I use to move my player:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42 public float[] direction(float x1, float y1, float x2, float y2)   {      float length;      boolean minusX = false, minusY = false;      float[] direction = new float[2];      a = 0;      b = 0;      c = 0;            if(x1 < x2)         a = x2 - x1;      else      {         a = x1 - x2;         minusX = true;      }      if(y1 < y2)         b = y2 - y1;      else      {         b = y1 - y2;         minusY = true;      }               c = (b*b) + (a*a);            length = (float) Math.sqrt(c);                  direction[0] = ((length + a)/length)-1;      direction[1] = ((length + b)/length)-1;            if(minusX)      {         direction[0] -= (direction[0]*2);      }      if(minusY)      {         direction[1] -= (direction[1]*2);      }            return direction;   }

I would become a very happy man getting a solution to this problem, being in the back of my head for years really (I actually think I was pretty active on these forums when I first stumbled onto it a couple of years ago).

// Gridur<br />--------------------------------------<br />Spell of Play Studios<br />http://www.spellofplay.com<br /><br />Download and test the open Beta of Time Breaker (3D Arkanoid) and comment on our forum!<br />---------------------------------
DzzD
 « Reply #1 - Posted 2010-03-28 00:10:41 »

assuming that to move front/towards the mouse, you move using two value : moveX moveY

then you only have to invert  those two value to make straff movment

to move toward mouse :
 1  2 posX+=moveX posY+=moveY

to straff right:
 1  2 posX+=moveYposY+=-moveX

to straff left:
 1  2 posX+=-moveYposY+=moveX

gridur

Senior Newbie

Groovy

 « Reply #2 - Posted 2010-03-28 07:18:20 »

Sweet! That did the bill DzzD!
I had my config pretty similar, but I didn't have one positive and one negative value but more like this:

 1  2 posX+=moveY;posY+=moveX;

But now I finally works! Maybe I'll dig up that old project as well and see how It is with the working strafe action!
Thanx for the help!

// Gridur<br />--------------------------------------<br />Spell of Play Studios<br />http://www.spellofplay.com<br /><br />Download and test the open Beta of Time Breaker (3D Arkanoid) and comment on our forum!<br />---------------------------------
gridur

Senior Newbie

Groovy

 « Reply #3 - Posted 2010-03-28 08:25:24 »

Got the old project up n' working with the strafe movement as well! Sweet stuff straight there! Now I got my behinds full with candy programming in the nearest future!
And as said I hope to be more active here on JG from now on!

// Gridur<br />--------------------------------------<br />Spell of Play Studios<br />http://www.spellofplay.com<br /><br />Download and test the open Beta of Time Breaker (3D Arkanoid) and comment on our forum!<br />---------------------------------
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 xFryIx (60 views) 2014-11-13 12:34:49 digdugdiggy (39 views) 2014-11-12 21:11:50 digdugdiggy (32 views) 2014-11-12 21:10:15 digdugdiggy (28 views) 2014-11-12 21:09:33 kovacsa (50 views) 2014-11-07 19:57:14 TehJavaDev (54 views) 2014-11-03 22:04:50 BurntPizza (53 views) 2014-11-03 18:54:52 moogie (68 views) 2014-11-03 06:22:04 CopyableCougar4 (67 views) 2014-11-01 23:36:41 DarkCart (153 views) 2014-11-01 14:51:03
 theagentd 29x basil_ 27x BurntPizza 23x princec 17x Spasi 17x pitbuller 15x HeroesGraveDev 14x Riven 14x KevinWorkman 13x SHC 11x kpars 10x gouessej 10x Grunnt 10x Longarmx 9x Nate 9x kevglass 9x
 Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org