Point and click navigation

again, for GT6…2d overhead space where player is centered and the vacuum of space scrolls by.

click mouse on screen, if not a potential target, then player wants to move to that location.

What would be the best way to intiate rotation, and movement towards that point? AffineTransform or not?
SUggestions on math?

Should location be a general bounding box surrounding that click, instead of trying to move to that exact x,y location?

First you need to create a vector from the ship position to the mouse position:


float vx = mousex-shipx;
float vy = mousey-shipy;

normalize it and you’ve got your direction vector. Use this for movement.

You’ll also need to know the angle of the direction that you use to rotate the ship sprite in the direction of the mouse. You can get the angle from a vector using this piece of code:


      /** Gets the angle of the vector given by (xp, yp)
       */
      public static final double getAngleFromVector(double xp, double yp) {
            if ((xp!=0)&&(yp!=0)) {
                  //Calculate angle of vector
                  double angle=0;
                  if (xp!=0) {
                        if ((xp>=0)&&(yp>=0))
                              angle=Math.atan(yp/xp);
                        if ((xp<0)&&(yp>=0))
                              angle=Math.PI-Math.atan(yp/-xp);
                        if ((xp<0)&&(yp<0))
                              angle=Math.PI+Math.atan(-yp/-xp);
                        if ((xp>=0)&&(yp<0))
                              angle=Math.PI*2-Math.atan(-yp/xp);
                  } else {
                        if (yp>=0)
                              angle=Math.PI/2;
                        else
                              angle=Math.PI+Math.PI/2;
                  }
                  return angle;
            } else {
                  if (xp==0 && yp==0)      return 0;
                  if (xp==0 && yp>0) return Math.PI*0.5;
                  if (xp==0 && yp<0) return -Math.PI*0.5;
                  if (yp==0 && xp>0) return 0;
                  else return Math.PI;
            }
      }

Btw. How much math do know?

Math?? ;D Calculus in college(20 yrs ago!)…I was a Biology student back then.
anyway, thanks. The math doesn’t look too difficult after some refresher studies ont he web… :wink:

Now to take a poke at it! Thanks again!