vectors, or something like that?

Blitz3D Forums/Blitz3D Beginners Area/vectors, or something like that?

Amon_old(Posted 2004) [#1]
Anybody have any hints on how I could approach coding a system that would fire a 2d bullet, from a point, to an area of the screen above determined by the angle picked using keydown?

Kinda like the Bubble Bobble game where a bubble is fired from a fixed position and goes in the direction the arrow faces.

I'm assuming I would have to find the vector path. I tried using sin/cos to achive it but it ended up getting messy and complicated.

Any help would be appreciated, and please, if a code snippet is posted can you leave a little description of whats going on.

Thanks

Amon.


Gabriel(Posted 2004) [#2]
You were on the right track with sin and cos. Maybe you had them the wrong way around?

bulletxvel#=cos(angle)*speed
bulletyvel#=sin(angle)*speed


Where angle is the direction the arrow faces in, and speed is the overall speed you want. Make speed the value you would want the yspeed to be if it was always going straight upwards.


GfK(Posted 2004) [#3]
Try this:
Graphics 800,600
Const X# = 400
Const Y# = 550
Global Angle# = 180
SetBuffer BackBuffer()
While Not KeyDown(1)
	Cls
	If KeyDown(203) Then Angle = Angle + 1
	If KeyDown(205) Then Angle = Angle - 1
	If KeyDown(57) Then AddBullet()
	If Angle < 145 Then Angle = 145
	If Angle > 215 Then Angle = 215
	DrawPlayer()
	UpdateBullets()
	Flip
Wend

Function DrawPlayer()
	Oval X-10,Y-5,20,10
	Line X,Y,Sin(Angle) * 20 + X,Cos(Angle) * 20 + Y
End Function

Function AddBullet()
	b.bullet = New bullet
	b\x = x
	b\y = y
	b\angle = angle
End Function

Function UpdateBullets()
	For b.bullet = Each bullet
		b\x = Sin(b\angle) * 5 + b\x
		b\y = Cos(b\angle) * 5 + b\y
		Line b\x,b\y,Sin(b\Angle) * 3 + b\x,Cos(b\Angle) * 3 + b\y
		If b\x < 0 Or b\x > 800 Or b\y < 0
			Delete b
		EndIf
	Next
End Function

Type Bullet
	Field X#
	Field Y#
	Field Angle#
End Type



Amon_old(Posted 2004) [#4]
.


Amon_old(Posted 2004) [#5]
Hey GFK,

Thats exactly what i need. Thx very much :)

Thx for your help also syb. :)


big10p(Posted 2004) [#6]
OK, I'm too slow but I've done the code so I'll post it anyway. :)

	Graphics 800,600,32
	SetBuffer BackBuffer()

	gun_angle#=-90.0
	firing% = False
	bullet_x# = 0
	bullet_y# = 0
	bullet_dx# = 0 
	bullet_dy# = 0
	Const BULLET_SPEED# = 5
	
	; Main loop
	While Not KeyHit(1)

		Cls
		
		If KeyDown(203) Then gun_angle = gun_angle - (1 And gun_angle > -180)
		If KeyDown(205) Then gun_angle = gun_angle + (1 And gun_angle < 0)
					
		; Calculate end of gun point.
		end_x# = 400+Cos(gun_angle)*100
		end_y# = 500+Sin(gun_angle)*100

		If KeyHit(57) And (Not firing)
			firing = True
			bullet_dx = Cos(gun_angle)*BULLET_SPEED
			bullet_dy = Sin(gun_angle)*BULLET_SPEED
			bullet_x = end_x
			bullet_y = end_y
		EndIf
		
		; Draw gun.
		Color 255,255,255
		Line 400,500,end_x,end_y
		
		; Draw bullet.
		Color 255,0,0
		If Not firing
			Oval end_x-5,end_y-5,10,10,1
		Else
			off_screen = (bullet_x<0)+(bullet_x>800)+(bullet_y<0)
			If off_screen
				firing = False
				Oval end_x-5,end_y-5,10,10,1
			Else
				Oval bullet_x-5,bullet_y-5,10,10,1
				bullet_x = bullet_x + bullet_dx
				bullet_y = bullet_y + bullet_dy
			EndIf
		EndIf
				
		Flip(1)
	Wend


	End



Amon_old(Posted 2004) [#7]
Thx for posting your code Big10p. I have 2 versions now :)