Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Calculating Strafe Angle Vector for Top Down Shooter?  (Read 4147 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 />---------------------------------
Offline 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+=moveY
posY+=-moveX


to straff left:
1  
2  
posX+=-moveY
posY+=moveX

Offline 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! Smiley
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 />---------------------------------
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!