Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

            mousex = input.getAbsoluteMouseX();
            mousey = input.getAbsoluteMouseY();

            if(mousex<= gc.getWidth()/2f)
                mousex = x - (gc.getWidth()/2f - mousex);
                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
                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);
            texture.setRotation((float) rot+90);                

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

share|improve this question
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
up vote 3 down vote accepted

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);  
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.