Calculating Strafe Angle Vector for Top Down Shooter?

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:


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

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 :

posX+=moveX 
posY+=moveY

to straff right:

posX+=moveY
posY+=-moveX 

to straff left:

posX+=-moveY
posY+=moveX 

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:


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! :slight_smile:
Thanx for the help!

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!