Ive been trying to do some line-circle collision but its not working out lol. Im following this:
http://mathworld.wolfram.com/Circle-LineIntersection.html
This is what Ive come up with:
Sync On
Sync Rate 60
Set Display Mode 1024,768,32
Backdrop off
Global playerScore
` store some info in variables, so we don't have to calculate it every time
global PXMax : PxMax = screen width() - 33 : global PlayerY : PlayerY = screen height()/2 + 300
Global curWave = 1
waveTimer = (Timer()-T)/1000+3
Global direction
Global doIncWaveOnce = 0
Global waveIncoming = 0
Global xPosPlayer
Global doSetupOnce = 0
Global numBullets = 1
global TotalAliens : global AlienSpeed : AlienSpeed = 4 : Global enemiesLeft
global DropCount
`Bullets
Type bullet
curxpos# as float
curypos# as float
alive as integer
EndType
Dim bullets(0) as bullet
type AlienData
` you don't need all of these, but if you change to using sprites, you might need them
` they are all integers, which is the default
snum, kind, value, Image, MinImage, MaxImage
x#,y#, status
endtype
dim alien(50) as AlienData
gosub SetupAliens
`Start of loop
D3D_INIT
DO
cls
if doSetupOnce = 0
xPosPlayer = screen width()/2
doSetupOnce = 1
EndIf
`Timer for wave incoming
Elapsed = (Timer()-T)/1000
TimeLeft = waveTimer - Elapsed
if TimeLeft > 0
D3D_StartText
D3D_text 1,screen width()/2, screen height()/2,1,str$(TimeLeft)
D3D_EndText
EndIf
if TimeLeft = 0
waveIncoming = 1
D3D_StartText
D3D_text 1,screen width()/2, screen height()/2,1,"Wave Incoming!"
D3D_EndText
D3D_StartText
D3D_text 1,screen width()/2, screen height()/2 + 60,1,"Wave " + str$(curWave)
D3D_EndText
EndIf
`Movement
If leftkey() and xPosPLayer > 3 then dec xPosPlayer,3 : ` use xPosPLayer to track where the player is on
If rightkey() and xPosPLayer < PXMax then inc xPosPlayer,3 : ` the X axis
`Player
D3D_circle xPosPLayer,PlayerY , 20, 1
`Ai
if waveIncoming = 1
`There is a wave incoming and all enemies are not dead
DirFlag = 0
for g = 1 to TotalAliens
if alien(g).status = 1 : ` is alien still alive?
xadd = 0
if direction = 4 then xadd = AlienSpeed :` move it right
if direction = 8 then xadd = -AlienSpeed : ` move it left
alien(g).x# = alien(g).x# + xadd
if direction = 2 then alien(g).y# = alien(g).y# + AlienSpeed : ` move it down
D3D_circle alien(g).x#, alien(g).y#, 10,0
if alien(g).x# > PXMax and direction <> 2 then DirFlag = 8 : ` is the alien at the right edge of the screen?
if alien(g).x# < 20 and direction <> 2 then DirFlag = 4 : ` is alien at the left edge of the screen?
endif
next g
if DirFlag > 0 : ` have the aliens reached either side of the screen?
DropCount = 8 :` we update this here instead so that they all move down together
OldDirection = direction : ` save the current direction, so we know which way to
direction = 2 : ` go when they get done moving down
endif
if DropCount > 0 : ` keeps track of how many times the aliens have moved downward
dec DropCount
if DropCount = 0 : ` start moving back across the screen
if OldDirection = 4
direction = 8
else
direction = 4
endif
endif
endif
`Check for bullet collision
For x = 1 to array count(bullets())
For i = 1 to enemiesLeft
If bullets(x).alive = 1
`dx = x2 - x1 : x values of line
x# = (bullets(x).curxpos# + 2) - (bullets(x).curxpos# - 2)
`dy = y2 - y1 : y values of line
y# = (bullets(x).curypos# + 40) - (bullets(x).curypos# )
`d = sqrt(dx^2 + dy^2)
d# = sqrt((x#*x#) + (y#*y#))
`D = x1*y2 - x2*y1
lD# = ((bullets(x).curxpos# - 2) * (bullets(x).curypos# + 40)) - ((bullets(x).curxpos# + 2) * (bullets(x).curypos#) )
`Discriminant
dis# = (10*10)*(d#*d#) - (lD# * lD#)
if dis# >= 0
bullets(x).alive = 0
alien(i).status = 0
enemiesLeft = enemiesLeft - 1
EndIf
EndIf
Next i
Next x
EndIf
remstart
If enemiesLeft = 0
waveIncoming = 0
curWave = curWave + 1
D3D_StartText
D3D_text 1,screen width()/2, screen height()/2,1,"Wave Incoming!"
D3D_EndText
D3D_StartText
D3D_text 1,screen width()/2, screen height()/2 + 60,1,"Wave " + str$(curWave)
D3D_EndText
waveIncoming = 1
EndIf
remend
`Shooting with spacekey
If spacekey() = 0
spaceKeyHeld = 0
EndIf
If spacekey() = 1 and spaceKeyHeld = 0
spaceKeyHeld = 1
array insert at bottom bullets()
bullets(numBullets).curxpos# = xPosPlayer
bullets(numBullets).curypos# = screen height()/2 + 230
bullets(numBullets).alive = 1
numBullets = numBullets + 1
EndIf
For x = 1 to array count(bullets())
If bullets(x).alive = 1
bullets(x).curypos# = bullets(x).curypos# - 4
D3D_box bullets(x).curxpos# - 2, bullets(x).curypos#, bullets(x).curxpos# + 2, bullets(x).curypos# + 40
if bullets(x).curypos# < 0 then bullets(x).alive = 0
EndIf
Next x
`Player score
set text size 15
text 0,0,"Player score: " + str$(playerScore) : text 0,15,"Current Wave: " + str$(curWave)
text 0,45, str$(screen fps())
Sync
Loop
SetupAliens:
` this sets the inital data for the aliens
select CurWave
case 1
restore Wave1Data
endcase
case 2
restore Wave2Data
endcase
case default
restore Wave1Data
endcase
endselect
read TotalAliens
enemiesLeft = TotalAliens
read columns,rows
a = 1
for r = 1 to rows
for c = 1 to columns
alien(a).x# = c * 45.0 : ` set initial alien x and y positions
alien(a).y# = (r * 40.0) + 50.0
inc a
next c
next r
for a = 1 to TotalAliens
alien(a).status = 1 : ` 1 = active, 0 = dead
next a
DropCount = 0 : ` not moving downward
direction = 4 : ` moving right initially
return
Wave1Data:
` TotalAliens
data 12
` # columns, # of rows
data 4,3
Wave2Data:
` TotalAliens
data 15
` # columns, # of rows
data 5,3
I thought I've followed it correctly, but apparently I'm wrong