Line intersect Circle

Started by kevin, June 26, 2003, 10:35:26 AM

Previous topic - Next topic

kevin



` *=---------------------------------------------------------------------=*
      Title$= "2D Line Intersect":       Version$= "V0.01"
` *=---------------------------------------------------------------------=*
`
`                              By Kevin Picone
`
`                         Last Update: 27th,June,2003
`
`         (c) Copyright 2003, By Kevin Picone, All Rights Reserved.
`
` *=---------------------------------------------------------------------=*
`                          URL: www.underwaredesign.com
` *=---------------------------------------------------------------------=*
`
`
`  So WHAT does this do ?:
`  =======================
`
`    Detects if a line intersects a circle.
`
`
` *=---------------------------------------------------------------------=*





` Init DB environment stuff.
` ==========================


sync on
sync rate 0
 Hide Mouse


ScreenWidth=640
ScreenHeight=480




` =================================
` Create Wall Section Arrays
` =================================

 Max_of_walls_segments=10

 Dim Max_numb_of_wall_Segments(1)
 MAx_numb_of_wall_Segments(1)=Max_of_walls_segments

 Dim WallSection_Xpos1#(Max_of_walls_segments)
 Dim WallSection_Ypos1#(Max_of_walls_segments)
 Dim WallSection_Xpos2#(Max_of_walls_segments)
 Dim WallSection_Ypos2#(Max_of_walls_segments)



 For lp =0 to Max_of_walls_Segments
   WallSection_Xpos1#(lp)=rnd(639)
   WallSection_Ypos1#(lp)=rnd(479)
   WallSection_Xpos2#(lp)=rnd(639)
   WallSection_Ypos2#(lp)=rnd(479)
 Next lp





` Players Variables
` -----------------

 PlayerDir#=90
 PlayerX#=500
 PlayerY#=200
 PlayerRadius#=20
 PlayerSpeed=5


do

 cls 0

  if title_toggle=0
   Display_UWProgramInfo(Title$,version$,1)
  else
   text 0,0,str$(screen fps())
  endif



  ` --------------------------------------
  `  Handle the PLayers/Object Movement
  ` --------------------------------------


  move#=0

  if upkey() then move#=playerspeed
  if downkey() then move#=playerspeed*-1
  if Leftkey() then playerdir#=wrapvalue(playerdir#-3)
  if Rightkey() then playerdir#=wrapvalue(playerdir#+3)


  if move#<>0

  ` Clip PLayers position to screen limits

   if playerx#<0 then playerx#=screenwidth
   if playerx#>screenwidth then playerx#=0
   if playery#<0 then playery#=screenHeight
   if playery#>screenheight then playery#=0

   playerx#=playerx#+(cos(playerdir#)*move#)
   playery#=playery#+(sin(playerdir#)*move#)

  endif


 
  ` --------------------------------------
  `  Render the Player/Object
  ` --------------------------------------

  ink rgb(255,255,255),0

  Circle playerx#,playerY#,playerRadius#
   
  playerx2#=playerx#+(cos(playerdir#)*50)
  playery2#=playery#+(sin(playerdir#)*50)
  ink rgb(255,0,0),0
  line playerx#,playery#,playerx2#,playery2#



  ` -----------------------------------------------------
  `  Render Lines and Check For Intersections with player
  ` -----------------------------------------------------


  For lp =0 to Max_of_walls_Segments
    x1#=WallSection_Xpos1#(lp)
    y1#=WallSection_Ypos1#(lp)
    x2#=WallSection_Xpos2#(lp)
    y2#=WallSection_Ypos2#(lp)

    result=Line_Intersect_Circle(x1#,y1#,x2#,y2#,playerx#,playery#,playerRadius#)
    if result=1
     ink rgb(255,0,0),0
    else
     ink rgb(155,155,155),0
    endif
    line x1#,y1#,x2#,y2#
  Next lp




  ` --------------------------------------
  `  Space Key Toggles The Title
  ` --------------------------------------

  if KeyPressed=0
   if spacekey()=1 then title_toggle=1-title_toggle
   KeyPressed=5
  else
   dec KeyPressed
  endif

 sync

loop





Function Line_Intersect_Circle(x1#,y1#,x2#,y2#,cx#,cy#,Radius#)

 ` Calc Closest Point to circle center

  dx31#=cx#-x1#
 dx21#=x2#-x1#

 dy31#=cy#-y1#
   dy21#=y2#-y1#

 d#=((dx21#*dx21#)+(dy21#*dy21#))

 if d#<>0 then  d#=((dx31#*dx21#)+(dy31#*dy21#))/d#

 ` Clip to the line segments legal bounds
 if d#<0.0 then d#=0
 if d#>1.0 then d#=1

  dx#=cx#-(x1#+(dx21#*d#))
  dy#=cy#-(y1#+(dy21#*d#))
  if Radius#=>sqrt((dx#*dx#)+(dy#*dy#))
    ` Line intersects circle
    exitfunction 1  
  endif
EndFunction 0



` *=----------------------------------------------------------------=*
` *=---------- Display Program TITLE & UWdesign BLURB INFO ---------=*
` *=----------------------------------------------------------------=*

Function Display_UWProgramInfo(Title$,version$,clearflag)
 if clearflag=1 then ink 0,0 : box 0,60,45,76
  ink rgb(255,255,255),rgb(255,255,255)
 x=0: y=0: yh=15
 text x,y,title$+" "+version$: y=y+yh
 text x,y,"Code By:Kevin Picone": y=y+yh
 text x,y,"Url: www.underwaredesign.com":y=y+yh
 text x,y,"Fps:"+str$(screen fps()): y=y+yh
EndFunction


BinaryMoon

Is it easy to modify this so that it works out where to reset the circle to so that the circle does not go through the line and slides along instead?
Ben aka Mop

BinaryMoon
BinarySun

kevin

#2
Have a look through the collisions and intersections bit on the site, there's sliding code already posted.

You'll have jump directly to that page since the site is meant to be offline at the moment.

Dark BASIC Source Codes

BinaryMoon

Note the word easy :) I've seen the angular sliding collision snippet before but I really don't want to convert it since it is so long :blink:

I don't need it at the moment anyway. Tile collision is plenty for me to be getting on with at the moment   :P
Ben aka Mop

BinaryMoon
BinarySun

kevin

#4
PlayBASIC Version  of  this example..


PlayBASIC Code: [Select]
` *=---------------------------------------------------------------------=*
Title$= "2D Line Intersect": Version$= "V0.01"
` *=---------------------------------------------------------------------=*
`
` By Kevin Picone
`
` PlayBASIC Port: 27th,Nov,2003
`
` (c) Copyright 2004, By Kevin Picone, All Rights Reserved.
`
` *=---------------------------------------------------------------------=*
` URL: www.underwaredesign.com
` *=---------------------------------------------------------------------=*
`

; So WHAT does this Do ?:
` =======================
`
` Detects if a line intersects a circle.
`
`
`
` This is the PLAY BASIC version of this example..
` Requires Play Basic V1.05 Or higher
`
`
`
` *=---------------------------------------------------------------------=*


ScreenWidth=GetScreenWidth()
ScreenHeight=GetScreenHeight()


` =================================
` Create Wall Section Arrays
` =================================

Max_of_walls_segments=25

Dim Max_numb_of_wall_Segments(1)
MAx_numb_of_wall_Segments(1)=Max_of_walls_segments

Dim WallSection_Xpos1#(Max_of_walls_segments)
Dim WallSection_Ypos1#(Max_of_walls_segments)
Dim WallSection_Xpos2#(Max_of_walls_segments)
Dim WallSection_Ypos2#(Max_of_walls_segments)


For lp =0 To Max_of_walls_Segments
WallSection_Xpos1#(lp)=Rnd(ScreenWidth)
WallSection_Ypos1#(lp)=Rnd(ScreenHeight)
WallSection_Xpos2#(lp)=Rnd(ScreenWidth)
WallSection_Ypos2#(lp)=Rnd(ScreenHeight)
Next lp





` Players Variables
` -----------------

PlayerDir#=90
PlayerX#=500
PlayerY#=200
PlayerRadius#=20
PlayerSpeed=5


Do
Cls 0
If title_toggle=0
Display_UWProgramInfo(Title$,version$,1)
Else
Text 0,0,FPS()
EndIf


` --------------------------------------
` Handle the PLayers/Object Movement
` --------------------------------------


move#=0

If UpKey() Then move#=playerspeed
If DownKey() Then move#=-playerspeed
If LeftKey() Then playerdir#=WrapAngle(playerdir#,-3)
If RightKey() Then playerdir#=WrapAngle(playerdir#,3)


If move#<>0

` Clip PLayers position to screen limits

If playerx#<0 Then playerx#=screenwidth
If playerx#>screenwidth Then playerx#=0
If playery#<0 Then playery#=screenHeight
If playery#>screenheight Then playery#=0

playerx#=playerx#+CosRadius(playerdir#,move#)
playery#=playery#+SinRadius(playerdir#,move#)

EndIf



` --------------------------------------
` Render the Player/Object
` --------------------------------------


LockBuffer
CircleC playerx#,playerY#,playerRadius#,0,$ffffff

playerx2#=playerx#+(Cos(playerdir#)*50)
playery2#=playery#+(Sin(playerdir#)*50)
LineC playerx#,playery#,playerx2#,playery2#,RGB(255,0,0)


` -----------------------------------------------------
` Render Lines and Check For Intersections with player
` -----------------------------------------------------

For lp =0 To Max_of_walls_Segments
x1#=WallSection_Xpos1#(lp)
y1#=WallSection_Ypos1#(lp)
x2#=WallSection_Xpos2#(lp)
y2#=WallSection_Ypos2#(lp)
If LineIntersectCircle(x1#,y1#,x2#,y2#,playerx#,playery#,playerRadius#)
LineC x1#,y1#,x2#,y2#,RGB(255,0,0)
Else
LineC x1#,y1#,x2#,y2#,RGB(155,155,155)
EndIf
Next lp
UnLockBuffer



` --------------------------------------
` Space Key Toggles The Title
` --------------------------------------

If KeyPressed=0
If SpaceKey()=1 Then title_toggle=1-title_toggle
KeyPressed=5
Else
Login required to view complete source code