Results 1 to 10 of 10

Thread: Shooting at the Player

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Shooting at the Player

    Hi All,

    I am trying to make the enemy shoot the bullet in the Players direction :

    Bullet.Direction:=90-radtodeg(Math.arctan2(PlayerSprite.Y - Enemy.Y, PlayerSprite.X - Enemy.X));

    I found this online, it was used to turn a turret to face the Player ... it almost works.... but I think this is probably not what I want.

    Can anynone tell me how can I calculate Direction ( which is FxVelocity ) for the bullet so that it goes in the direction of the Player ?

    I guess I Need to calculate somehow the Direction which is based on the length it must travel to the Player and the Velocity of the Bullet .

    I tried to draw the Problem . I am not even sure if that is the correct way . I would Need to calculate like this if the Player X is smaller then Enemy X,
    and reversed if the Player X is greater then enemy X .

    UPDATE :

    I created a test Level with only one enemy,

    1st stationary... 2nd moving straight down , 3rd... moving left and right

    Bullet.Direction:=(90-radtodeg(Math.arctan2(PlayerSprite.Y - Enemy.Y, PlayerSprite.X - Enemy.X)))/12;

    I dont know why but if I divide the number which I get by twelve I get perfect hit on every Position for 1st test and 3rd test , on 2nd test when It goes straight down... after a while it shoots at incorrect angel...
    So I guess this arctan2 could still be the correct way.. but I am missing something here... something that I should be calculating with...but what....

    UPDATE 2:

    Bullet.Direction:=(90-radtodeg(Math.arctan2(PlayerSprite.Y - Self.Y, PlayerSprite.X - Self.X)))/12;

    Self.X and Self.Y is the proper way... if I have multiple enemy ships.... this works... but the precision is still a bit off....

    Please help.

    THanks. Robert
    Attached Images Attached Images
    Last edited by robert83; 08-12-2015 at 02:41 PM.

  2. #2
    Hi Robert!

    Are you trying to simply shoot the bullet toward Player current position? Or are you perhaps also trying to implement some sort of "Target prediction" algorithm so that you can also accurately shoot at moving targets?

  3. #3
    Hi SilverWarrior!,

    I am trying to shoot toward the target... this works :

    Bullet.Direction:=(90-radtodeg(Math.arctan2(PlayerSprite.Y - Self.Y, PlayerSprite.X - Self.X)))/12;

    But I have no idea why.... why do I Need to divide by 12.... I guess 12 should be calculated but from what ?
    I would like a bit of Explanation... otherwise it is functioning perfectly.... I tried yesterday a complete Level... the Shooting was great....

    Thanx

  4. #4
    Quote Originally Posted by robert83 View Post
    But I have no idea why.... why do I Need to divide by 12.... I guess 12 should be calculated but from what ?
    I would like a bit of Explanation... otherwise it is functioning perfectly.... I tried yesterday a complete Level... the Shooting was great....
    I have no idea why you have to divide everything by 12. I guess the answer to this might be in your code that is using this information.
    Also after preparing a quick test case for your approach (calculating angle of y mouse cursor position to a fixed point on the form) I'm getting wrong results even when I remove division by 12.

    Code:
    procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var Direction: Single;
    begin
      Form1.Canvas.Brush.Color := clBtnFace;
      Form1.Canvas.FillRect(Form1.ClientRect);
      Form1.Canvas.Brush.Color := clBlack;
      Form1.Canvas.MoveTo(Form1.Width div 2, Form1.Height div 2);
      Form1.Canvas.LineTo(X,Y);
      Direction :=(90-radtodeg(Math.arctan2(Form1.Height div 2 - Y, Form1.Width div 2 - X)));
      Form1.Caption := Format('Direction = %n', [Direction]);
    end;
    It kinda works when mouse cursor is top left, bottom left or bottom right in relation to center position but when I move mouse cursor to top right position in relation to center position I get a negative value.
    Also the angle is increasing in counter-clockwise direction but generally should do so in clockwise direction.

    If I change my code to :

    Code:
    procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var Direction: Single;
    begin
      Form1.Canvas.Brush.Color := clBtnFace;
      Form1.Canvas.FillRect(Form1.ClientRect);
      Form1.Canvas.Brush.Color := clBlack;
      Form1.Canvas.MoveTo(Form1.Width div 2, Form1.Height div 2);
      Form1.Canvas.LineTo(X,Y);
      Direction :=(180-radtodeg(Math.arctan2(Form1.Height div 2 - Y, Form1.Width div 2 - X)));
      Form1.Caption := Format('Direction = %n', [Direction]);
    end;
    I'm getting correct results and the angle is at 0 degrees when mouse cursor is directly left of the center point.

  5. #5
    It must be in the code... because if I try it with your calculation it is not working. Anyway it does not matter. I'll figure it out sometime later It works. And it always does... so I managed to hit it somehow. Beginers Luck I guess.

    I have bigger Problem now... game is progressing nicely, two Levels are done. "AI" Functioning . Enemies have different Attack types. The BOSSes have multiple attack Patterns . Music Sound working... but after testing it yesterday
    I started out with 120 MB Memory... on Level 2 end I was at 600MB.... and I almost went out the window in my happynes .

    At first I thought the Framework is bugged... which would unfortunately mean utter failure again... since I am still not good enough to do this myself...

    But I may be lucky... because I checked an earlier Version... and the error is not present .... so I guess I am Clearing the map Array incorrectly.. or maybe the BASS.DLL is causing Memory leak with mp3... I'll Play with it later....

    Thank you.

  6. #6
    Hi,

    The good News is that I found the Memory leak... it is indeed BASS . For example I have in menu the Cursor with Sound. Simply by moving trough the menu... Memory goes higher by every Sound played.

    THis is what I am doing in the doMenu procedure :


    Code:
    procedure TMarsInvadors.doMenu;
    begin
        // up arrow
        if MarsInvadors.Keyboard.KeyPressed[DIK_UP] then
          begin
            dec(MenuChoice);
            if MenuChoice < 1 then MenuChoice := 3;
            PlaySound(ExtractFilePath(Application.Exename)+'sounds\choice.mp3');
          end;
        // down arrow
        if MarsInvadors.Keyboard.KeyPressed[DIK_DOWN] then
          begin
            inc(MenuChoice);
            if MenuChoice > 3 then MenuChoice := 1;
            PlaySound(ExtractFilePath(Application.Exename)+'sounds\choice.mp3');
          end;
        case MenuChoice of
            1 : MarsCursor.Y:= 320;
            2 : MarsCursor.Y:= 352;
            3 : MarsCursor.Y:= 416;
        end;
        // enter
        if MarsInvadors.KeyBoard.KeyPressed[DIK_RETURN] then
          begin
            case MenuChoice of
                1:
                 begin
                   // game
                   MarsCursor.Dead;
                   GameState:=gsReset;
                 end;
                2:
                 begin
                   // info
                   MarsCursor.Dead;
                   GameState:=gsInsLoad;
                 end;
                3:
                 begin
                   // exit
                 end;
            end;
          end;
    
    end;
    Nothing fancy... the Playsound procedure Looks like this :


    Code:
    procedure PlaySound(const SampleFilename : String);
    var mySound :  HStream;
    begin
      // free both MOD and stream, it must be one of them! :)
     BASS_MusicFree(mySound);
     BASS_StreamFree(mySound);
    
     mySound := BASS_StreamCreateFile(FALSE, PChar(SampleFilename), 0, 0, BASS_SAMPLE_FX {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF});
    // BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 4000);
     BASS_ChannelPlay(mySound, False);
    end;
    Is there some obvious error here ? I must confess the bass Thing I did in quiet a haste . I googled now... found someone else mentioning some Problems related to BASS.dll free Version with mp3 ... from year 2012.... I am not
    sure if it's that same bug still present. Or I am just doing something totaly wrong here.

    Thank you

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •