# 2D top down movement

I'm trying to make my first real time game.

I'm having problems with the movements using the mouse though. If close to theX axis, the player is moving fine, but if close to the Y axis(going southwards) he moves fast even though I'm taking into consideration the x coordinates.

Edit: I rewritten the code following your advice and the answer below. I hope it's better written now.

`````` if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON))
{
mousex = input.getAbsoluteMouseX();
mousey = input.getAbsoluteMouseY();

if(mousex<= gc.getWidth()/2f)
mousex = x - (gc.getWidth()/2f - mousex);
else
mousex = x + (mousex - gc.getWidth()/2f);

if(mousey<= gc.getHeight()/2f)
mousey = y - (gc.getHeight()/2f - mousey);      // calculate the x and y needed to move to
else
mousey = y + (mousey - gc.getHeight()/2f);      // mousex and mousey in relation to the map and not screen position
// because screen scrolls
System.out.println("mousex: "+mousex+"mousey: "+ mousey);
int delta_x=(int) (mousex-x);
int delta_y=(int) (mousey-y);
double rotangle = Math.atan2(delta_y,delta_x);
double rot = Math.toDegrees(rotangle);
if(rot==0)
rot=0.1;
texture.setRotation((float) rot+90);
}

if( (Math.abs(x-mousex)>1))
{
this.moveForward(0.2f, texture.getRotation(), delta, null);
}
else
if( (Math.abs(y-mousey)>1))
{
this.moveForward(0.2f, texture.getRotation(), delta, null);
}
else
this.setX(mousex);

}
``````
-
Your code is not consistent in how you treat movement in X and movement in Y. Is this deliberate? –  ChrisF Sep 7 '11 at 15:45
I'd go as far as to say that it is a mess, it is completely inconsistent, and you are mixing X values into what seems to be the Y part of the code. Exactly what you want it to be is not clear so I can't tell you what to change, but presumable you'll have to fix multiple issues. –  aaaaaaaaaaaa Sep 7 '11 at 16:28
All you are doing is obscuring your own code when you use getters and setters for such simple data members as x and y. Just make the members public and leave it at that. Stop worrying about access control until you know what you are doing, and then if you still want to worry about it, you can do so when you're refactoring for completeness. Access control is rarely of any importance anyway, unless you are writing a reusable engine or framework, and I guarantee you, you are not. –  Arcane Engineer Sep 7 '11 at 20:47
Thank you all for your feedback. Like I said I'm trying to do top down movement, as the answer below clarified. Thank you all for your criticism , I will take it into account and try and fix my code. –  Predanoob Sep 8 '11 at 4:38
In addition to @Nick Wiggill's comment about not always needing to use accessors for everything (+1; I completely agree with him), a good point with your code is that you're using primitives (instead of objects) for these often-used variables. By having public declarations (as Nick mentioned), you also eliminate the repetitive re-allocation of data since you'll be re-using those variables, and one bonus is optimization. For movement handling, keep it as simple as possible -- for everything else, try to simplify when it's sensible to do so (it usually is); it will make your life easier. –  Randolf Richardson Sep 9 '11 at 16:35

## 1 Answer

For this answer, I'm going to assume that you want the player avatar to move towards the mouse cursor at a fixed top speed. That's the best I could gleam from your explanation/code, if it is incorrect, please inform me as such and I'll rework the answer:

Note: `topSpeed` is a constant defined elsewhere.

``````int currentX = this.getX();
int currentY = this.getY();
int deltaX = mouseX-currentX;
int deltaY = mouseY-currentY;
float angle = atan2(deltaY, deltaX);
float magnitude = sqrt(deltaX*deltaX + deltaY*deltaY);
magnitude = Min(magnitude, topSpeed);
this.setX(currentX+cos(angle)*magnitude;
this.setY(currentY+sin(angle)*magnitude;
``````
-
I just realized that I had my bullets implemented somewhat like that and they were working! The problem remains however, when to stop the player. As bullets don't stop until they collided. I'm looking it up and will get back to you. –  Predanoob Sep 8 '11 at 4:40