News:

Function Finder  Find all the functions within source code files

Main Menu

Qix Collision / AI mock Up

Started by kevin, November 29, 2019, 07:14:10 PM

Previous topic - Next topic

kevin

  Qix Collision / AI mock Up

  This is a little mock up  showing a simple approach of how you might start writing your own Qix game AI.  



 Video:

 




 Source Code:

 
PlayBASIC Code: [Select]
` *=---------------------------------------------------------------------=*
` *=---------------------------------------------------------------------=*
` *=---------------------------------------------------------------------=*
`
` >> Qix Collision Detection Mock Up <<
`
` Example By: Kevin Picone
`
` *=---------------------------------------------------------------------=*
` *=---------------------------------------------------------------------=*
` *=---------------------------------------------------------------------=*




Type tVert
x#,y#
SpeedX#,SpeedY#
EndType

Type tObject
P1 as tVert
P2 as tVert
Impact as tVert
EndType



dim qix as tObject
qix = new tObject


setfps 30

Do

if spacekey()=true or initstartup=false

if Collision_Map_Image<>0 then deleteimage Collision_Map_Image
Collision_Map_Image=Make_Collision_Map_Image()
initstartup=true
init_qix()
endif



drawimage Collision_Map_Image,0,0,false
setcursor 0,0


// move qix
line qix.p1.x,qix.p1.y,qix.p2.x,qix.p2.y

x1#=qix.p1.x+qix.p1.speedx
y1#=qix.p1.y+qix.p1.speedy

x2#=qix.p2.x+qix.p2.speedx
y2#=qix.p2.y+qix.p2.speedy

clipX=0
if x1#<ClipX then X1#=ClipX : qix.p1.x =X1#: qix.p1.speedx*= -1
if x2#<ClipX then X2#=ClipX : qix.p2.x =X2#: qix.p2.speedx*= -1

clipX=GetScreenWidth()
if x1#=>ClipX then X1#=ClipX-1 : qix.p1.x =X1#: qix.p1.speedx*= -1
if x2#=>ClipX then X2#=ClipX-1 : qix.p2.x =X2#: qix.p2.speedx*= -1


clipY=0
if y1#<ClipY then y1#=ClipY : qix.p1.y =y1#: qix.p1.speedy*= -1
if y2#<ClipY then y2#=ClipY : qix.p2.y =y2#: qix.p2.speedy*= -1

clipY=GetScreenHeight()
if y1#=>ClipY then y1#=ClipY-1 : qix.p1.y =y1#: qix.p1.speedy*= -1
if y2#=>ClipY then y2#=ClipY-1 : qix.p2.y =y2#: qix.p2.speedy*= -1


lockbuffer

rendertoimage Collision_Map_Image

if CheckLine(x1#,y1#,x2#,y2#,Qix.Impact)

dist# =getdistance2d(x1#,y1#,x2#,y2#)
dist2impact#=getdistance2d(x1#,y1#,qix.impact.x#,qix.impact.y#)

// check rebound end ?
if dist2impact# < (Dist#/2)
speed# =getdistance2d(0,0,qix.p1.speedx, qix.p1.speedy)
angle# =getangle2d(0,0,qix.p1.speedx, qix.p1.speedy)
angle# =wrapangle(Angle#,180)
qix.p1.SpeedX =cosradius(angle#,Speed#)
qix.p1.SpeedY =sinradius(angle#,Speed#)
x1# =qix.p1.x
y1# =qix.p1.y
else

speed# =getdistance2d(0,0,qix.p2.speedx, qix.p2.speedy)
angle# =getangle2d(0,0,qix.p2.speedx, qix.p2.speedy)
angle# =wrapangle(Angle#,180)
qix.p2.SpeedX =cosradius(angle#,Speed#)
qix.p2.SpeedY =sinradius(angle#,Speed#)
x2# =qix.p2.x
y2# =qix.p2.y

endif


// check the
;print "HIT"
endif
unlockbuffer


rendertoscreen
qix.p1.x=x1#
qix.p1.y=y1#
qix.p2.x=x2#
qix.p2.y=y2#

linec x1#,y1#,x2#,y2#,$ff00ff

setcursor 0,100
print qix.impact.x
print qix.impact.y

Sync


loop

End





Function Init_Qix()

x =rndrange(100,700)
y =rndrange(100,500)
angle# =rnd(360)
Size = rndrange(50,200)
qix.p1.x =cosnewvalue(x,angle#-90,size)
qix.p1.y =sinnewvalue(y,angle#-90,size)
qix.p2.x =cosnewvalue(x,angle#+90,size)
qix.p2.y =sinnewvalue(y,angle#+90,size)

Login required to view complete source code




    Music by: https://bensound.com