zondag 24 april 2011

Get Angle between 2 points Method

public double getangle( double x1, double y1, double x2 , double y2 ){
  double at = Math.toDegrees(Math.atan2( x1 - x2 , y1 - y2 ));
  at = at - 180;
  if (at > 360 ){
at = at - 360;
  }else if ( at < 0) {
  at = at + 360;
  }
  return at;
}

to move from the x1 and y1 coordinates to the x2 to the y2 coordinates then.


double movement x per step  = Math.sin(math.toRadians(ang)); // ang is the at value returned by the method above.
double movement y per step  = Math.cos(Math.toRadians(ang));



I used this code for shooting lasers towards mouse coordinates. It was a sort of missile defender game.

Geen opmerkingen:

Een reactie posten