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?
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?
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
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.
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.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;
Also the angle is increasing in counter-clockwise direction but generally should do so in clockwise direction.
If I change my code to :
I'm getting correct results and the angle is at 0 degrees when mouse cursor is directly left of the center point.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;
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.
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 :
Nothing fancy... the Playsound procedure Looks like this :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;
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 notCode: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;
sure if it's that same bug still present. Or I am just doing something totaly wrong here.
Thank you
Solved it I think :
BASS_STREAM_AUTOFREE was needed , note though it stops it correctly but it was not freed no idea why....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} or BASS_STREAM_AUTOFREE); // BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 4000); BASS_ChannelPlay(mySound, False); if BASS_ChannelIsActive(mySound) = 0 then begin BASS_ChannelStop(mySound); BASS_Streamfree(mySound); end; end;
Thanx
It's been a while since I used BASS, but unless the workflow has changed, you shouldn't really free streams while the program is running. Calling StreamCreateFile basically reads the file again from the disk, and that's bad. Instead, what you want is:
1. Load file at program start
2. Use channels to play the sound (the BASS Stream). If you want to change the volume, stop, pause, or however manipulate the playing sound, keep the channel number and use the BASS_ChannelSomething() functions. You don't have to free channels manually, as managing memory for channels is done automatically by BASS.
3. Only free the Stream when you're closing the application, or for some reason need to load a different file to the same stream handle (say, your game hero underwent a transformation and now uses a different sound set)
Bookmarks