blua tigro
Gebruiker
- Lid geworden
- 21 apr 2009
- Berichten
- 48
voor t dedecteren van botsingen tussen
bolllen - bol en aabb - aabb's
bolllen - bol en aabb - aabb's
Code:
'' bluatigro 1 sept 2017
'' collision detection
'' aabb and sphere
public class zone
public x as double
public y as double
public z as double
public dx as double
public dy as double
public dz as double
public r as double
public issphere as bool
public sub fill_as_sphere(a as double,b as double,c as double,radius as double)
x=a
y=b
z=c
r=radius
issphere = true
end sub
public sub fill_as_aabb(a as double,b as double,c as double _
,da as double,db as double,dc as double)
x=a
y=b
z=c
dx=da
dy=db
dz=dc
issphere = false
end sub
public function collision( zn as zone ) as bool
''zone 1 and zone 2 = block
if not( zn.issphere ) and not( issphere ) then
dim bx as double = abs( zn.x - x )
dim by as double = abs( zn.y - y )
dim bz as double = abs( zn.z - z )
dim ax as double = zn.dx + dx
dim ay as double = zn.dy + dy
dim az as double = zn.dz + dz
return bx < ax and by < ay and bz < ay
end if
''zone 1 and zone 2 = sphere
if zn.issphere and issphere then
dim dis as double = math.sqrt( ( zn.x - x ) ^ 2 _
+ ( zn.y - y ) ^ 2 _
+ ( zn.z - z ) ^ 2 )
zonehit = dis < ( zn.r + r )
end if
''zone 1 = sphere zone 2 = block
if zn.isphere and not( issphere ) then
swap me , zn
end if
''zone 1 = block zone 2 = sphere
dim diss as double = 0
if x < ( zn.x - zn.dx ) then
diss += ( x - ( zn.x - zn.dx ) ) ^ 2
else
if x > ( zn.x + z.dx ) then
diss += ( x - ( zn.x + zn.dx ) ) ^ 2
end if
end if
if y < ( zn.y - zn.dy ) then
diss += ( y - ( zn.y - zn.dy ) ) ^ 2
else
if y > ( zn.y + z.dy ) then
diss += ( y - ( zn.y + zn.dy ) ) ^ 2
end if
end if
if z < ( zn.z - zn.dz ) then
diss += ( z - ( zn.z - zn.dz ) ) ^ 2
else
if z > ( zn.z + z.dz ) then
diss += ( z - ( zn.z + zn.dz ) ) ^ 2
end if
end if
return diss < r * r
end function
end class