How do I make a 2d enemy fire in any Direction?
Blitz3D Forums/Blitz3D Beginners Area/How do I make a 2d enemy fire in any Direction?
| ||
Lol. The space you have for the topic title is SO TINY! I couldn't really describe accurately my question, so I will do so now. I am making a 2D Space Shooter game, and would like one of the enemys to be able to fire at the player no matter where he is positioned. I know how to make enemys fire projectile weapons, and have often done so in my games. But I don't know how to make the enemy fire at the player no matter where he is positioned, and to keep the bullet on track, and going in a straight line to its destination without moving from side to side at all! Like this, Key P = Player E = enemy - = bullet path ; = space ;P;;;;;;;;;;;;;;;;;; ;-;;;;;;;;;;;;;;;;;; ;;-;;;;;;;;;;;;;;;; ;;;-;;;;;;;;;;;;;; ;;;;E;;;;;;;;;;; I want the enemys bullet to go in a straight line to the player. ;;;;;;;;;;;;;P ;;;;;;;;;;;;-; ;;;;;;;;;;;-;; ;;;;;;;;;;-;;; ;;;;;;;;;E;;; No matter where the player might be positioned. So how do I move the bullet in the right direction? normally I would just use Bullet\x = Bullet\x + 1 or something like that. But I need the bullet to be able to move in any direction! If anyone could answer this, I would appreciate it. WERDNA |
| ||
;pseudocode BulletSpeed# = 5.0 ;could be any number dx# = PlayerPositionX - EnemyPositionX dy# = PlayerPositionY - EnemyPositionY dist# = sqr(dx*dx + dy*dy) bulletvelocityx#=0 bulletvelocityy#=0 if dist#>0 then bulletvelocityx# = BulletSpeed * (dx / dist) bulletvelocityy# = BulletSpeed * (dy / dist) endif ;then when you move the bullet simply do so like this: bulletpositionx# = bulletpositionx# + bulletvelocityx# bulletpositiony# = bulletpositiony# + bulletvelocityy# |
| ||
Matty's code should work. Here is an example.Graphics 640,480,0,2 Const towerx = 320 Const towery = 240 Const bspeed = 5 Type bul Field x#,y#,dx#,dy#,lif End Type SetBuffer BackBuffer() btimer = 0 While Not KeyDown(1) Cls Color 255,255,255 Oval 310,230,20,20,0 btimer = btimer + 1 If btimer = 30 Then btimer = 0 b.bul = New bul b\x# = towerx b\y# = towery distx = MouseX() - towerx disty = MouseY() - towery dist# = Sqr(distx*distx + disty*disty) b\dx# = (distx / dist#) * bspeed b\dy# = (disty / dist#) * bspeed b\lif = 300 EndIf For b.bul = Each bul Line b\x,b\y,b\x-b\dx#,b\y-b\dy# b\x# = b\x# + b\dx# b\y# = b\y# + b\dy# b\lif = b\lif - 1 If b\lif = 0 Then Delete b.bul Next Flip Wend End |
| ||
And here's yet another example for you:Const SHOT_speed = 5 ;pixels Const SHOT_Timer_Delay = 10 ;just to hold the update on every computer you run the game. Global SHOT_Timer = MilliSecs() Const GW=640 : GH=480 Graphics GW,GH SetBuffer BackBuffer() Type shots Field x#,y# Field HSpeed#,VSpeed# End Type Global Update_Bullets = False While Not KeyHit(1) Cls MX# = MouseX() MY# = MouseY() angle# = ATan2(MY-GH/2,MX-GW/2) Line MX-5,MY-5,MX+5,MY+5 Line MX-5,MY+5,MX+5,MY-5 Oval GW/2-10,GH/2-10,20,20 Line GW/2,GH/2, GW/2+Cos(angle)*30 , GH/2+Sin(angle)*30 If MouseHit(1) Then s.shots = New shots s\x = GW*1.0/2 s\y = GH*1.0/2 s\HSpeed = SHOT_Speed*Cos(angle) s\VSpeed = SHOT_Speed*Sin(angle) EndIf If SHOT_Timer + SHOT_Timer_Delay < MilliSecs() Then Update_Bullets = True SHOT_Timer = MilliSecs() Else Update_Bullets = False EndIf For s.shots = Each shots Rect s\x-2,s\y-2,4,4 If Update_Bullets Then s\x = s\x + s\HSpeed s\y = s\y + s\VSpeed Endif If (s\x < 0 Or s\x > GW) Or (s\y < 0 Or s\y > GH) Then Delete s Next Flip Wend End In this one I used Atan2 to get the "angle" that I used to extract the horizontal and vertical components of the vector. Having these 2 components, you just add them every iteration of the bullet update loop to the bullet's current position and voilą. |
| ||
Yep, simply get the angle via atan2 then, plug that angle into COS for the x speed and SIN for the y speed. |
| ||
plug that angle into COS for the x speed and SIN for the y speed Don't forget to multiply the result by the desired bullet speed :o) |
| ||
Thanks guys! this looks perfect ^_^ Just what I wanted. WERDNA |