Positioning many 3D objects: I need help, please !

Blitz3D Forums/Blitz3D Beginners Area/Positioning many 3D objects: I need help, please !

etxtra(Posted 2011) [#1]
Hi, everyone ! I need some help in order to randomly position many complex objects (complex shapes). The point is that I don't want the objects to overlap. I would use polygon collision to achieve this because my objects are complex (houses, trees, rocks...).
Note that once positioned, I will perform sliding collision against these object (sphere (ellipsoid) to polygons).
-So How can I do ? I don't see what method to use nor what collision commands to use !

Please can you give me an idea, or a working example?
I've tried something but it don't works maybe you can see what's wrong... :
; How to avoid overlapping objects ?
Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()

; creates camera and position it on top and then turn it to look top-down;
camera = CreateCamera ()
RotateEntity camera, 90.0, 0.0, 0.0
PositionEntity camera, 50.0, 80.0, 50.0

; Loading: creates 300 cones ( I chose cones because it is a complex shape and box or ellipsoid collisions are not suitable) ;
For A = 0 To 300
	; Create a new cone
	Cone = CreateCone ()
	; set "type" to 3
	EntityType cone, 3
	
	; positionning, gives 1000 chances to object "cone" to be positioned at a "collision free" place ;
	For B = 0 To 1000
		X# = Rnd# ( 0.0, 100.0)
		Z# = Rnd# ( 0.0, 100.0 )
		Angle# = Rnd# ( 0.0, 360.0 )
		; position and rotate cone randomly ;
		PositionEntity Cone, X, 0.0, Z
		RotateEntity Cone, Angle, Angle, Angle
		
		; if there is no collision, exit;
		UpdateWorld
		If EntityCollided ( Cone, 2) = 0 Then Exit
		; else, there is a collision so loop again;
	Next
	
	; after 1000 chances, if the cone is again in collision: delete it, and go to the next step;
	If EntityCollided ( Cone, 2) <> 0
		FreeEntity Cone
		FreeEntity Cone
		Goto GOT_A_Next
	EndIf
	; set the "type" to 2 for the next time;
	EntityType Cone, 2

	.GOT_A_Next
Next

; main loop;
Repeat
	
	RenderWorld
	Locate 0,0
	Print "How can I avoid overlapping cones ?"
	Flip
Until KeyHit(1)

End


PS: I thought it would be easy: "if the object overlap then position it elsewhere, randomly..." but in fact, I feel a bit... "lost" !

Last edited 2011


Warner(Posted 2011) [#2]
You would need to use MeshesIntersect, or write some kind of BoxesOverlap function for this.
In order for this to work, you need to keep track of all cones you've created so far. Use either and array (Dim) or a Type to do that.
Here is some code to help you on your way:



etxtra(Posted 2011) [#3]
Thank you, your idea works ! here it is (replace "EntityColor" with "HideEntity"):
; How to avoid overlapping objects ?
Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()

; creates camera and position it on top and then turn it to look top-down;
camera = CreateCamera ()
RotateEntity camera, 90.0, 0.0, 0.0
PositionEntity camera, 50.0, 80.0, 50.0

; Create array
Dim Cone(300)

; Create a cone
orgCone = CreateCone ()
HideEntity orgCone

; Loading: creates 300 cones ( I chose cones because it is a complex shape and box or ellipsoid collisions are not suitable) ;
For A = 0 To 300
	; Create a new cone, and store in array
	Cone(A) = CopyEntity(orgCone) ;copyentity->takes less memory
	
	X# = Rnd# ( 0.0, 100.0)
	Z# = Rnd# ( 0.0, 100.0 )
	Angle# = Rnd# ( 0.0, 360.0 )

	; position and rotate cone randomly ;
	PositionEntity Cone(A), X, 0.0, Z
	RotateEntity Cone(A), Angle, Angle, Angle
	
	For Z = 0 To A-1
		If MeshesIntersect ( Cone(A), Cone(Z) ) = True
			EntityColor Cone(A), 255.0, 0.0, 0.0
			EntityColor Cone(Z), 255.0, 0.0, 0.0
		EndIf
	Next
Next


; main loop;
Repeat
	
	RenderWorld
	Locate 0,0
	Print "How can I avoid overlapping cones ?"
	Flip
Until KeyHit(1)

End