Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) 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 4643 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.

 BurntPizza (29 views) 2015-04-23 03:42:11 theagentd (34 views) 2015-04-22 16:23:07 Riven (48 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (41 views) 2015-04-14 20:13:12 Riven (43 views) 2015-04-12 21:36:37 bus hotdog (60 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (69 views) 2015-04-06 22:06:58 ags1 (67 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 16x wessles 15x kingroka123 11x alwex 11x 65K 11x kevglass 8x Rayvolution 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x Ecumene 7x ra4king 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding 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:27
 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