# Incorrect angle calculation?

I'm trying to create a simple topdown game, in which you control the player by WASD keys and use mouse to aim and shoot. So far, I have a player moving and firing, but I think there is something wrong with the shooting.

Accuracy gets lower as the mouse cursor move away from the player.

Please try the following SWF to see more clearly what I'm trying to say.

http://dl.dropbox.com/u/24777511/gdstack.swf

Here is a chunk of relevant code:

private function fireBullet(x:Number, y:Number, dFireAngle:Number):void {
var b:Bullet = recycleBullet();
var rFireAngle:Number;
b.reset(x, y);
b.angle = dFireAngle;
rFireAngle = (dFireAngle * (Math.PI / 180));

b.velocity.x = Math.cos(rFireAngle) * 385;
b.velocity.y = Math.sin(rFireAngle) * 385;
}


...

if (FlxG.mouse.pressed()) {

var p:FlxPoint = new FlxPoint(FlxG.mouse.screenX, FlxG.mouse.screenY);
var angle:Number = FlxU.getAngle(new FlxPoint(player.x, player.y), p) - 90;
fireBullet(player.x, player.y, angle);
...

• +1 Love it when people can articulate there question, and have code samples, AND an example. Very refreshing :) – Adam Harte Aug 1 '11 at 12:11

var dx:Number = x2 - x1;

The Flixel code just calls Math.atan2(Y,X) * 180 / Math.PI, which looks ok. Have you checked that you're not accidentally rounding floats down to ints at any point in those calculations?