Avoiding overlapping objects

Everything related to 3D programming
kns
User
User
Posts: 54
Joined: Sat Apr 26, 2003 2:06 am

Avoiding overlapping objects

Post by kns »

First, though I have owned PB for ages, it's only been recently that I have decided to give it serious attention. Essentially, I am brand new to the environment. With that caveat in mind I am attempting to randomly place N spheres without any overlap.

Rather basic.

Code: Select all

Define.l NumObj=10
  Dim prop.POS(NumObj)
  Define.l PlaneNum
  
   CreateMaterial(#Material0, LoadTexture(#Texture0, "RustySteel.jpg"))
   
   ;SkyBox("stevecube.jpg")
  
    For i=1 To NumObj
        
      prop(i)\X = Random(150, 0)
      prop(i)\Y= Random(150, 0)
      prop(i)\Z = Random(150, 0)
      
      prop(i)\meshnum = CreateSphere(#PB_Any, 10, 50, 50)
      prop(i)\entnum = CreateEntity(#PB_Any,MeshID(prop(i)\meshnum), MaterialID(#Material0), prop(i)\X-75, prop(i)\Y-75, -prop(i)\Z)
      ;EntityPhysicBody(prop(i)\entnum, #PB_Entity_StaticBody, 1)
      EntityPhysicBody(prop(i)\entnum, #PB_Entity_SphereBody, 1)
      
    Next
Experimenting with the following to separately generate coordinates FAILS. I am using an arbitrary number as the minimum distance between objects.

Code: Select all


#Max = 2147483647

Structure POS
  X.l
  Y.l
  Z.l
EndStructure

 Define.l NumObj=10
  Dim prop.POS(NumObj)
  Define.l a
  Define.f out1, out2
  Define.l seed
  
  seed = Random(#Max)
  RandomSeed(seed)
  
  For i=1 To 10
        
      prop(i)\X = Random(150, 0)
      prop(i)\Y = Random(150, 0)
      prop(i)\Z = Random(150, 0)
      
        a=1
        While a < i
          out1 = Sqr((Pow((prop(a)\X - prop(i)\X), 2.0) + Pow((prop(a)\Y - prop(i)\Y), 2.0) + Pow((prop(a)\Z - prop(i)\Z), 2.0)))
          If out1 < 100
            Repeat
              Debug "REPEAT"
              prop(a)\X = Random(150, 0)
              prop(a)\Y= Random(150, 0)
              prop(a)\Z = Random(150, 0)
              out2 = Sqr((Pow((prop(a)\X - prop(i)\X), 2.0) + Pow((prop(a)\Y - prop(i)\Y), 2.0) + Pow((prop(a)\Z - prop(i)\Z), 2.0)))
            Until out2 > 100
            Debug out1
            Debug out2
            ;a = 1 ;??
          Else
            a = a + 1
         EndIf 
        Wend
    Next
Overall, any better/more efficient method to place non-overlapping primitives would be most helpful.

Thanks.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Avoiding overlapping objects

Post by applePi »

Hi
without giving physics to the balls it is hard to do what you want but it is possible since then it is a mathematics problem. but here i will go the easy way by giving physics to the ball and the plane and its sides.
you can disable the gravity by WorldGravity(0) in line 71, and if it is positive such as WorldGravity(2) then it is antigravity and the spheres will go up

Code: Select all

Enumeration
   #MAT
   #MAT_plane
   #plane
   #LIGHT
   #CAMERA
   #mainwin
   #ball
   #cube
   #cube2
   #cube3
   #cube4
EndEnumeration
Global Quit.b = #False
Speed = 0.5
If OpenWindow(#mainwin, 0, 0, 500, 480, "press ESC to exit...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;Initialize environment
InitEngine3D()
InitSprite()
InitKeyboard()
OpenWindowedScreen(WindowID(#mainwin), 0, 0, 500, 400, 0, 0, 0)
;WorldShadows(#PB_Shadow_Modulative, -1, RGB(100, 250, 100))
  
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)
SetFrameRate(60)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)


CreateMaterial(#MAT_plane, LoadTexture(#MAT_plane, "Geebee2.bmp"))
;CreatePlane(#Mesh, TileSizeX, TileSizeZ, TileCountX, TileCountZ, TextureRepeatCountX, TextureRepeatCountZ)
CreatePlane(#plane, 10, 10, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(#MAT_plane))
EntityPhysicBody(#plane, #PB_Entity_StaticBody )  

CreateLight(0,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(100,100,100))

CreateCamera(#CAMERA, 0, 0, 400, 400)
MoveCamera(#CAMERA, 0, 4, 13)
CameraLookAt(#CAMERA, 0, 2, 0)

RotateCamera(#CAMERA, -15, 0, 0)
CreateSphere(#ball, 1)
CreateEntity(#ball, MeshID(#ball),#PB_Material_None )
ScaleEntity(#ball, 0.2, 0.2, 0.2)
MoveEntity(#ball, 0,2,3) 
EntityPhysicBody(#ball, #PB_Entity_SphereBody  , 1, 1, 1)

For i=0 To 300
  CopyEntity(#ball , 100+i)
      x.f = Random(5, 0)
      y.f= Random(5, 0)
      z.f = Random(5, 0)
  MoveEntity(100+i, x,y,z)
  EntityPhysicBody(100+i, #PB_Entity_SphereBody  , 1, 0.1, 1)
Next

CreateCube(#cube, 1)
CreateEntity(#cube, MeshID(#cube),#PB_Material_None, 0,0,-5 )
ScaleEntity(#cube, 10,2,2)
CopyEntity(#cube, #cube2)
MoveEntity(#cube2, -6,0,-1)
RotateEntity(#cube2, 0,90,0)
EntityPhysicBody(#cube, #PB_Entity_StaticBody ) 
EntityPhysicBody(#cube2, #PB_Entity_StaticBody ) 
EndIf
WorldGravity(-9.8)
;WorldGravity(0)

;Main loop
Repeat
  Event = WindowEvent()
     
  RenderWorld()
   
  FlipBuffers()

   ExamineKeyboard()
   If KeyboardReleased(#PB_Key_Escape)
      Quit = #True
   EndIf

Until Quit = #True Or Event = #PB_Event_CloseWindow

   
    
kns
User
User
Posts: 54
Joined: Sat Apr 26, 2003 2:06 am

Re: Avoiding overlapping objects

Post by kns »

Thank you very much.
Post Reply