When Distance to line Won't cut it
.
Next step: FAST Shape Based 2d Collision system , let's optimize this sucker...
REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART
-------------------------------- www.english.pharosfx.com -------------------------------
___
/ / / _ __ __ __ __ _ _
/__/ /_ __/ /_/ / / /_ /_ /
/ / / /_/ / /_/ __/ / _/_
...presents...
------------------------------= Instant Bullets & rectangles =--------------------------
...When lines intercect...
----------------------------------------------------------------------------------------
REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REMSTART REREMEND
`The Basics of Dark Basic
Sync on:Randomize Timer():set display mode 800,600,32:set window on:set window size 800,600
`Vector Struct
Type vec
x as float
y as float
endtype
`Vertex array
dim Corners(4) as vec
`The Rectangle (It could be something else too,few modifications are needed)
ox# = 400.0 : `Position X
oy# = 300.0 : `Position Y
width# = 50.0
height# = 50.0
angle# = 45
anglexy#= atanfull(width#,height#)
distxy# = sqrt(width#*width# + height#*height#)
`Get the corners
corners(1).x = ox# + cos(angle#-anglexy#)* distxy#
corners(1).y = oy# + sin(angle#-anglexy#)* distxy#
corners(2).x = ox# + cos(angle#+anglexy#)* distxy#
corners(2).y = oy# + sin(angle#+anglexy#)* distxy#
corners(3).x = ox# + cos(angle# +180-anglexy#)* distxy#
corners(3).y = oy# + sin(angle# +180-anglexy#)* distxy#
corners(4).x = ox# + cos(angle# +180+anglexy#)* distxy#
corners(4).y = oy# + sin(angle# +180+anglexy#)* distxy#
`1-2 = height
`2-3 = width
`3-4 = height
`4-1 = width
`Player Stuff
px# = 640.0
py# = 480.0
Range# = 400.0
shangle as vec :`Shooting direction
buff as vec :`temp vector
do
set cursor 0,0
`Draw Rect
ink rgb(255,255,255),0
for t = 1 to 3
line corners(t).x,corners(t).y,corners(t+1).x,corners(t+1).y
next t
line corners(4).x,corners(4).y,corners(1).x,corners(1).y
`Input
if leftkey() then dec Pangle#,0.5
if rightkey() then inc Pangle#,0.5
`Calculate the player's direction
shangle.x = cos(Pangle#)
shangle.y = sin(Pangle#)
if upkey()
px# = px# + shangle.x
py# = py# + shangle.y
endif
if downkey()
px# = px# - shangle.x
py# = py# - shangle.y
endif
`trace it...
line px#,py#,px#+shangle.x*Range#,py#+shangle.y*Range#
for t = 1 to 4
`Coloured Verts! Don't laught, it was real usefull for debugging
select t
case 1
ink rgb(255,255,255),0
endcase
case 2
ink rgb(255,0,0),0
endcase
case 3
ink rgb(0,0,255),0
endcase
case 4
ink rgb(0,255,0),0
endcase
EndSelect
circle corners(t).x,corners(t).y,3
ink rgb(255,255,255),0
`Get the "face" vector
if (t<4)
`till 3
buff.x = corners(t+1).x -corners(t).x
buff.y = corners(t+1).y -corners(t).y
else
`4
buff.x = corners(1).x - corners(4).x
buff.y = corners(1).y - corners(4).y
endif
`d# == ZERO <=> Vectors are parallel
d# = buff.y * shangle.x - shangle.y * buff.x
if d# <> 0
`Beta represents the distance from the player to the intersection with the line...
Beta# = (( corners(t).x - px#) * buff.y)/d# + ((py# - corners(t).y)* buff.x)/d#
if (Beta# >0) and (Beta# < Range#)
Ix# = px#+shangle.x * Beta#
Iy# = py#+shangle.y * Beta#
if Ix#<800 and Ix#>0
if Iy#<600 and Iy#>0
circle Ix#,Iy#,5
endif
endif
`Lambda is the "distance" between the current corner and the point of intersection
`NOTE THAT the distance is measured in "face length"
Lambda# = ( Ix# - corners(t).x ) / buff.x
If (Lambda#=0) then Lambda# = ( Iy# -corners(t).y ) / buff.y
`print "Beta ";t;t+1;" , ";beta#
`print "Lambda ";t;t+1;" , ";lambda#
`So if the distance lies withing [0,1] then it's a Hit
If Lambda# >= 0 and Lambda# =< 1
ink rgb(255,0,0),0
circle Ix#,Iy#,5
endif
endif
endif
next t
`Sync It
sync
cls
loop