Ooops
November 25, 2020, 05:39:27 AM

Author Topic: [bb] Point Inside Oriented Bounding Box by sswift [ 1+ years ago ]  (Read 660 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Point Inside Oriented Bounding Box
Author : sswift
Posted : 1+ years ago

Description : This function does an oriented bounding box check.  

An oriented bounding box is a box which is rotated with an entity and aligned with it's axes, and not with the axes of the world.

An oriented bounding box usually will be smaller and fit an entity more snugly than an axis aligned bounding box will, and thus the check is more accurate.

Also, since an oriented bounding box is in object space, the locations of the object's vertices realtive to the box never change, so the box is a fixed size.  That means you can precalculate the shape of the box for every object in your game.  

Calcualting the shape of the box is the most costly part of the algorithm, and this makes an oriented bounding box check better than an axis aligned bounding box check for two reasons... one is the afformentioned increased accuracy, and the other is that you can precalculate the size and shape of the box to make the check really fast.  You can't do that with an axis aligned bounding box check because the box's size and shape changes as the object rotates in space.


Code :
Code: BlitzBasic
  1. ; -------------------------------------------------------------------------------------------------------------------
  2. ; This function returns true if a point is inside an object's oriented bounding box, and false if it is not.
  3. ; -------------------------------------------------------------------------------------------------------------------
  4. Function Point_Inside_Oriented_Bounding_Box(ThisEntity, Px#, Px#, Pz#)
  5.        
  6.        
  7.         ; Transform point from global space to object space.
  8.         TFormPoint Px#, Py#, Pz#, 0, ThisEntity
  9.         Px# = TFormedX#()
  10.         Py# = TFormedY#()
  11.         Pz# = TFormedZ#()
  12.        
  13.        
  14.         ; Calculate the bounding box (in object space) for this object.
  15.         ; You can precalculate this for every object in your game for a huge speed increase!
  16.         Surfaces = CountSurfaces(ThisEntity)
  17.  
  18.         For LOOP_Surface = 1 To Surfaces
  19.  
  20.                 Surface_Handle = GetSurface(ThisEntity, LOOP_Surface)
  21.                        
  22.                 Verts = CountVertices(Surface_Handle) - 1
  23.                 For LOOP_Verts = 0 To Verts-1
  24.                        
  25.                         Vx# = VertexX#(Surface_Handle, LOOP_Verts)
  26.                         Vy# = VertexY#(Surface_Handle, LOOP_Verts)
  27.                         Vz# = VertexZ#(Surface_Handle, LOOP_Verts)
  28.                        
  29.                         If (Vx# > Max_X#) Then Max_X# = Vx#
  30.                         If (Vy# > Max_Y#) Then Max_Y# = Vy#
  31.                         If (Vz# > Max_Z#) Then Max_Z# = Vz#
  32.                                
  33.                         If (Vx# < Min_X#) Then Min_X# = Vx#
  34.                         If (Vy# < Min_Y#) Then Min_Y# = Vy#
  35.                         If (Vz# < Min_Z#) Then Min_Z# = Vz#
  36.                                
  37.                 Next
  38.                
  39.         Next
  40.        
  41.        
  42.         ; Determine if the point (in object space) is inside the bounding box (in object space).
  43.         If (Px# > Min_X#) And (Px# < Max_X#) And (Py# > Min_Y#) And (Py# < Max_Y#) And (Pz# > Min_Z#) And (Pz# < Max_Z#)
  44.                 Return True
  45.         Else
  46.                 Return False
  47.         EndIf
  48.  
  49.  
  50. End Function


Comments :


Wayne(Posted 1+ years ago)

 How did the above code ever work with two Px# references ?Now I have to test it.8)Function Point_Inside_Oriented_Bounding_Box(ThisEntity, Px#, Px#, Pz#)should be:Function Point_Inside_Oriented_Bounding_Box(ThisEntity, Px#, Py#, Pz#)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal