Point - Box intersection routine?

Blitz3D Forums/Blitz3D Programming/Point - Box intersection routine?

Kryzon(Posted 2009) [#1]
Does anyone have a fast "Point to Box" intersection routine lying around?

(I already have Sphere to Box and Ray to Box, but couldn't find a Point to Box algorithm already converted to a B3D easy to use function)

I want to make trigger zones for my game and I'd use the player's central pivot as a source for the XYZ coordinates for the Point, and the the trigger meshes throughout the level as the zones.


Gabriel(Posted 2009) [#2]
Point to box is just six comparisons, surely? Something like:

If PointX>=BoxMinX
   If PointX<=BoxMaxX
      If PointY>=BoxMinY
         If PointY<=BoxMaxY
            If PointZ>=BoxMinZ
               If PointZ<=BoxMaxZ
                  ' COLLISION!
               End If
            End If
         End If
      End If
   End If
End If


Can't really make a function out of it without knowing precisely what the parameters you want to supply will be. If the box is static, I assume you'll have the BoxMax and BoxMin variables precomputed. If it's dynamic, you will presumably be passing in position and dimensions of the box, and will thereofore want to dynamically recalculate the Min and Max Values. Best to do it only once per frame though, so don't bury it in the comparison function if you can avoid it.


Stevie G(Posted 2009) [#3]
Something like this should work. Width, height and depth are those of the zone :

Function PointInCube( player, zone , width#, height#, depth# )

	Local Collision = False

	TFormPoint 0,0,0, player, zone

	If Abs( TFormedX() ) < Width
		If Abs( TFormedY() ) < Height
			If Abs( TFormedZ() ) < Depth
				Collision = True
			EndIf
		EndIf
	EndIf
		
	Return Collision
	
End Function 



Zethrax(Posted 2009) [#4]
Firstly, are we talking about axially aligned bounding boxes here (AABBs), or arbitrarily oriented bounding boxes (OBBs).

Gabriel's code should work for an AABB, and Stevie G's for an OBB.

I've also got some OBB code at http://www.blitzbasic.com/codearcs/codearcs.php?code=1920 and some code for triggerplates at http://www.blitzbasic.com/codearcs/codearcs.php?code=1901 which may be useful.


Kryzon(Posted 2009) [#5]
That's some great stuff.
I guess I'll keep checking it once in a 60 FPS cycle.

Thank you all for the help.