Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
...
share|improve this question
    
+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

2 Answers 2

up vote 3 down vote accepted

I used the following code instead of Flixel's getAngle and strangely, it works:

var dx:Number = x2 - x1;
var dy:Number = y2 - y1;
return Math.atan2(dy,dx);
share|improve this answer
    
Looks like you were converting from radians to degrees where you already had a value in degrees. –  bummzack Jul 28 '11 at 8:28
    
This is the way to go... anyway you have to debug to see the numbers that runs under the hood –  FxIII Jul 28 '11 at 9:15

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?

share|improve this answer
    
I checked my code again but I couldn't find any casting or int variable. –  frbry Jul 28 '11 at 8:21

Your Answer

 
discard

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.