[bb] Area Collision Lib by David Bird(Birdie) [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : Area Collision Lib
Author : David Bird(Birdie)
Posted : 1+ years ago

Description : As the title says..

Code :
Code (blitzbasic) Select
;
; Area Collision Library
; David Bird
; enquire@davebird.fsnet.co.uk
;
;syntax

;SetSphereRegion(posx,posy,posz,radius)
;SetCubeRegion(posx,posy,posz,size)
;SetRectRegion(posx,posy,posz,width,height,depth)
;PointInRegion(regType,checkx,checky,checkz)
;SphereInRegion(regType,checkx,checky,checkz,radius to check)
;EntityInRegion(regType,Entity,Radius) Problem No way of obtaining entity radius
;FreeAllRegions()
;FreeRegion(r.reg)
;GetEntityRegion.reg(ent,x#,y#,z#,rd#=1)
;GetSphereRegion.reg(x#,y#,z#,rad#)
;GetPointRegion.reg(x#,y#,z#)

Global AC_DEBUG=False ;set this this to true to show regions
Type reg
Field typ ;0-sphere 1-cube 2-rect
Field piv
Field rad#
Field sx#
Field sy#
Field sz#
Field lpiv
Field tx#
Field ty#
Field tz#
Field debugent
End Type
Function SetAC_DEBUG(d=False)
AC_DEBUG=d
End Function

Function MoveRegion(r.reg,x#,y#,z#)
MoveEntity rpiv,x,y,z
End Function

Function TurnRegion(r.reg,x#,y#,z#,Glob=0)
TurnEntity rpiv,x,y,z,glob
End Function

Function PositionRegion(r.reg,x#,y#,z#,Glob=0)
PositionEntity rpiv,x,y,z,Glob
End Function

Function TranslateRegion(r.reg,x#,y#,z#,Glob=0)
TranslateEntity rpiv,x,y,z,Glob
End Function

Function SetSphereRegion.reg(x#,y#,z#,rad#,parent=0)
r.reg=New reg
rpiv=CreatePivot(parent)
rlpiv=CreatePivot()
PositionEntity rpiv,x,y,z
rad=rad
r yp=0 ;only spheres unto now
If AC_DEBUG=True Then
t=CreateSphere(10,rpiv)
EntityAlpha t,.25
EntityFX t,16
ScaleEntity t,rad,rad,rad
rdebugent=t
End If
Return r
End Function

Function SetCubeRegion.reg(x#,y#,z#,size#,parent=0)
r.reg=New reg
rpiv=CreatePivot(parent)
rlpiv=CreatePivot()
PositionEntity rpiv,x,y,z
rsx=size
rsy=size
rsz=size
r yp=1 ;only spheres unto now
If AC_DEBUG=True Then
t=CreateCube(rpiv)
EntityAlpha t,.25
EntityFX t,16
ScaleEntity t,size,size,size
rdebugent=t
End If

Return r
End Function

Function SetRectRegion.reg(x#,y#,z#,w#,h#,d#,parent=0)
r.reg=New reg
rpiv=CreatePivot(parent)
rlpiv=CreatePivot()
PositionEntity rpiv,x,y,z
rsx=w
rsy=h
rsz=d
r yp=1
If AC_DEBUG=True Then
t=CreateCube(rpiv)
EntityAlpha t,.25
EntityFX t,16
ScaleEntity t,w,h,d
rdebugent=t
End If
Return r
End Function

Function PointInRegion(r.reg,x#,y#,z#)
typ=r yp
If typ=2 Then t=1
Select typ
Case 0 ;-point in sphere
dx#=EntityX(rpiv,True)-x
dy#=EntityY(rpiv,True)-y
dz#=EntityZ(rpiv,True)-z
rad#=Sqr(dx^2+dy^2+dz^2)
If rad<rad Then Return True
Case 1 ;- point in cube or rectangle
If x<EntityX(rpiv,True)+rsx And x>EntityX(rpiv,True)-rsx
If y<EntityY(rpiv,True)+rsy And y>EntityY(rpiv,True)-rsy
If z<EntityZ(rpiv,True)+rsz And z>EntityZ(rpiv,True)-rsz
Return True
End If
End If
End If
End Select
Return False
End Function

Function EntityInRegion(ent,r.reg,rd#=1)
Return SphereInRegion(r,EntityX(ent,True),EntityY(ent,True),EntityZ(ent,True),rd);??? radius
End Function

Function SphereInRegion(r.reg,x#,y#,z#,rd#)
typ=r yp
If typ=2 Then t=1
Select typ
Case 0 ;-Sphere in sphere
dx#=EntityX(rpiv,True)-x
dy#=EntityY(rpiv,True)-y
dz#=EntityZ(rpiv,True)-z
rad#=Sqr(dx^2+dy^2+dz^2)-rd
If rad<rad Then Return True
Case 1 ;-Sphere in cube
dx#=EntityX(rpiv,True)-x
dy#=EntityY(rpiv,True)-y
dz#=EntityZ(rpiv,True)-z
rad#=Sqr(dx^2+dy^2+dz^2)
dx=dx/rad
dy=dy/rad
dz=dz/rad
dx=EntityX(rpiv,True)-(dx*(rad-rd))
dy=EntityY(rpiv,True)-(dy*(rad-rd))
dz=EntityZ(rpiv,True)-(dz*(rad-rd))
If dx<EntityX(rpiv,True)+rsx And dx>EntityX(rpiv,True)-rsx
If dy<EntityY(rpiv,True)+rsy And dy>EntityY(rpiv,True)-rsy
If dz<EntityZ(rpiv,True)+rsz And dz>EntityZ(rpiv,True)-rsz
Return True
End If
End If
End If
End Select
Return False
End Function

Function GetPointRegion.reg(x#,y#,z#)
;returns the first region that a point is within
For r.reg=Each reg
If PointInRegion(r,x,y,z)=True Then Return r
Next
Return Null
End Function

Function GetSphereRegion.reg(x#,y#,z#,rad#)
;returns the first region that a point is within
For r.reg=Each reg
If SphereInRegion(r,x,y,z,rad)=True Then Return r
Next
Return Null
End Function

Function GetEntityRegion.reg(ent,rd#=1)
;returns the first region that a point is within
For r.reg=Each reg
If EntityInRegion(ent,r,rd)=True Then Return r
Next
Return Null
End Function

Function FreeAllRegions()
For r.reg=Each reg
If rpiv<>0 Then FreeEntity rpiv
If rlpiv<>0 Then FreeEntity rlpiv
Delete r
Next
End Function

Function FreeRegion(r.reg)
If rpiv<>0 Then FreeEntity rpiv
If rlpiv<>0 Then FreeEntity rlpiv
Delete r
End Function

Function UpdateRegions()
For r.reg=Each reg
r x=EntityX(rpiv,True)-EntityX(rlpiv,True)
r y=EntityY(rpiv,True)-EntityY(rlpiv,True)
r z=EntityZ(rpiv,True)-EntityZ(rlpiv,True)
PositionEntity rlpiv,EntityX(rpiv,True),EntityY(rpiv,True),EntityZ(rpiv,True)
Next
End Function

;only used in debug
Function HideRegions()
For r.reg=Each reg
HideEntity rdebugent
Next
End Function

Function ShowRegions()
For r.reg=Each reg
ShowEntity rdebugent
Next
End Function


Comments : none...