Aistina

Hi everyone,

I'm making a simple top-down shooter using the XNA framework, kinda GTA style. Your character will look to where your mouse is, and you have to use the WASD keys to move. Now, the W and the S key work fine (move towards and away from the mouse, respectively), but I can't get A and D to work.

The idea is that when you press and hold A the character will move around your mouse clockwise, and D will do the same, but counter-clockwise. Can anyone tell me a good way to do this, or link me to some tutorial

Here's a screenie of the game in it's current state to show you in what direction I'm going with this.

*click*

Thanks,

Aistina aka The Serious Spoon

### Re: Game Technologies: General Need help with calculating movement

Inaki Ayucar

Ok, you have the character position, stored in a variable, letˇŻs say c.Position, and looking in a certain direction, letˇŻs say c.LookatDir.

What you can do is translate the position of the character a certain distance in the direction of looking (backward or forward, that would be the "radius of rotation" or distance to the pivot point) and apply the rotation after the translation. That will give you the "translated rotation" you want.

Or, if you prefer, a simpler way:

r = distance to pivot point (mouse )

angle = angle of rotation (incremented or decremented as player presses "a" or "d")

c.Position = ( - c.LookatDir * r );

Matrix m = Matrix.RotationY ( angle );

c.Position.Transform( m );

c.Position += Mouse.Position

ThatˇŻs it !

### Re: Game Technologies: General Need help with calculating movement

Aistina

Hmm, so basically you're saying change the origin of the player texture That could work, but it would make other stuff like looking around and shooting (the bullets would appear where the mouse is) overly complicated :/

### Re: Game Technologies: General Need help with calculating movement

Inaki Ayucar

Ok, maybe I didnˇŻt understand you,

I thought you wanted the player to rotate around the mouse position. Anyway it doesnt matter, you can do the same at a "Matrix level":

1.- First, build a translation matrix that moves the player off the pivot point, the distance you want.

2.- Build a second matrix that rotates the player the degrees you want

3.- Multiply both matrix and transform playerˇŻs position with the final matrix.

### Re: Game Technologies: General Need help with calculating movement

Aistina

Hmm, unfortunately I am not familiar with how to use Matrices. Anyway, I asked a professional game developer and he helped me out. In case you want to know, here is the final code:

float oldX = v2Placement.X - Mouse.GetState().X;
float oldY = v2Placement.Y - Mouse.GetState().Y;

float r = 0.05f - (Vector2.Distance(v2Placement, new Vector2(Mouse.GetState().X, Mouse.GetState().Y)) / 8000);

moving.X = (oldX * (float)Math.Cos(r) - oldY * (float)Math.Sin(r)) + Mouse.GetState().X;
moving.Y = (oldY * (float)Math.Cos(r) + oldX * (float)Math.Sin(r)) + Mouse.GetState().Y;

v2Placement.X = moving.X;
v2Placement.Y = moving.Y;