Main Menu

Line /Shape Intersection

Started by kevin, January 15, 2006, 01:00:44 AM

Previous topic - Next topic

kevin

Added ray intersection to the vector shapes command set (through LineHitShape).  So now you can find the closest impact point and the normal at the point of the impact.

This can be used to calc reflections or perhaps attachments at that point.  While this was possible before, previously you had to use sprites.

also added a new Shape Draw mode, Mode 3.  This mode will draw the shape outline and edge normals.

Rembrandt Q Einstein

Is there a way to get shape normals going away from the filled in parts?  Something Like a GetEdgeNomalX# or so?

kevin

#2
 See GetNormalX#


PlayBASIC Code: [Select]
  setfps 30

; Create a convert shape to check collisions against
MyShape=NewConvexShape(50,7)

; Init some variables to hold our shapes position
ShapeX#=100
ShapeY#=100

; Randomly init the test lines end point

x2#=Rnd(GetScreenWidth())
y2#=Rnd(GetScreenHeight())


; Start a So/Loop
Do
; Clear the sreen
Cls RGB(0,0,0)

; Display a message
Print "Testing Line To Shape Collision"

; Rotate our test shape
RotateShape MyShape,Angle#,1

; Bump the Rotation angle
Angle#=WrapAngle(angle#,1)

; Move and clip the Shapes position
Shapex#=Mod(Shapex#+1,GetScreenWidth())

; Read the mouse pointers position
x2#=MouseX()
y2#=MouseY()

; If the right mouse button is press
; Then change the test lines END position
If RightMouseButton()
x1#=x2#
y1#=y2#
EndIf

; Draw the Test Line
LineC x1#,y1#,x2#,y2#,RGB(100,100,100)


; Check if a line hits the shape
If LineHitShape(x1#,y1#,x2#,y2#,MyShape,Shapex#,Shapey#,True)


; Set the pen's ink colour to yellow
Ink RGB(255,255,0)

; DRaw the impact point
Circle GetIntersectX#(0),GetIntersectY#(0),5,True


ix1#=GetIntersectX#(0)
iy1#=GetIntersectY#(0)
ix2#=GetIntersectX#(0)-GetNormalX#(0)*100
iy2#=GetIntersectY#(0)-GetNormalY#(0)*100

Ink RGB(0,255,255)
line ix1#,iy1#,ix2#,iy2#


; display a message
Print "Line Hit Shape"

; set the ink colour to RED
Ink RGB(255,0,0)
Else

; If the line didn't hit the shape, then
; Set then ink colour to White
Ink RGB(255,255,255)
EndIf


; Draw the filled shape
DrawShape MyShape,ShapeX#,ShapeY#,2


; Display the screen
Sync
; loop back to the do
Loop



ATLUS