Notifications
Clear all

[Closed] create a line between centers of two spheres?

I would like to create a random array of spheres, and automatically create a renderable line object between the centers of these spheres, a sort of molecule structure effect…

Any tips on where to begin?

7 Replies
 j83

Perhaps this will get you started in the right direction?


 --just a quick script written for a discussion on CGtalk  -j83
 
 (
 	fn splineBetweenToPoints pointA pointB =
 	(
 		theSplineShape = SplineShape pos:pointA wirecolor: (random black red)
 		addNewSpline theSplineShape
 		addKnot theSplineShape 1 #corner #line PointA		
 		addKnot theSplineShape 1 #corner #line PointB
 		
 		updateShape theSplineShape
 		theSplineShape
 	)
 	
 		
 	for i = 1 to 50 do
 	(		
 		local randomPoint3 = [random -100 100, random -100 100, random -100 100]
 		sphereA = sphere pos:randomPoint3 wirecolor:(random black blue) radius:10 segments:16
 				
 		splineBetweenToPoints sphereA.pos [0,0,0]
 	)
 	
 	
 )
 	
 	
 	
 	
 	
 	
 	
 	
 

cool thats great thanks!

how can I stop spheres from intersecting? And how can I create some lines that go between different spheres too, not just from the origin

cool I got the lines working between spheres!

for i = 1 to 22 do
(
local randomPoint1 = [random -100 100, random -100 100, random -100 100]
local randomPoint2 = [random -50 100, random -50 100, random -100 50]

	sphereA = sphere pos:randomPoint1 wirecolor:(random black blue) radius:4 segments:32
	sphereB = sphere pos:randomPoint2 wirecolor:(random red yellow) radius:4 segments:32
			
	splineBetweenToPoints sphereA.pos sphereB.pos
)

now I just need intersection avoidance and we have a script!

 j83

Here is an example where I just use the distance function and determine if an object is within the radius of the sphere. You could use this logic to detect intersection using an embedded for loop.


   (
  	local mainSphereRadius = 100
  	mainSphere = sphere radius: (mainSphereRadius) xray:true segments:64
  	
  	numOfSmallSpheres = 1000
  	
  	local smallSphereArr = #()
  	for i = 1 to numOfSmallSpheres do
  	(
  		local randomPoint3 = [random -150 150, random -150 150, random -150 150]
  		aSphere = sphere radius: 8  segments:16 wirecolor:blue position:(randomPoint3)
  		append smallSphereArr aSphere
  	)
  	
  	for j = 1 to smallSphereArr.count do
  	(
  		if ((distance mainSphere.center smallSphereArr[j].center) < mainSphere.radius) then
  		(
  			smallSphereArr[j].wirecolor = green
  		)
  	)
  )
   

I think Bobo and other have posted more accurate wasy of doing it, but this is a rather quick one, if you don’t mind the overlap.

You could also just bias the distance comparison to remove any chance of overlap.

ok I will try and integrate this…I am just rendering an animation sequence I made with our script (more yours lol), I think ur gonna be surprised at the results

it’s like a digital jackson pollock

How could I parent the spline knots to the sphere centers, so when I move a sphere, the spline knot moves with it?

 j83

Add a skin modifier to the spline, and add the spheres as bones. For example,


  --skinned spline example for CGtalk  -j83
  
  (
 	 fn splineBetweenToPoints pointA pointB =
 	 (
 		 theSplineShape = SplineShape pos:pointA wirecolor: (random black red)
 		 addNewSpline theSplineShape
 		 addKnot theSplineShape 1 #corner #line PointA		
 		 addKnot theSplineShape 1 #corner #line PointB
 		 
 		 updateShape theSplineShape
 		 theSplineShape
 	 )
 	 
 		 
 	 
 	 local randomPoint3 = [random -100 100, random -100 100, random -100 100]
 	local randomPoint3b = [random -100 100, random -100 100, random -100 100]
 	 
 	sphereA = sphere pos:randomPoint3 wirecolor:(random black blue) radius:10 segments:16
 	 sphereB = sphere pos:randomPoint3b wirecolor:(random black blue) radius:10 segments:16
 	
 	 theSpline = splineBetweenToPoints sphereA.pos sphereB.pos
 	
 	addModifier theSpline (Skin())
 	
 	max modify mode
 	modPanel.setCurrentObject theSpline.skin
 	skinOps.addBone theSpline.modifiers[#Skin] sphereA 0
 	skinOps.addBone theSpline.modifiers[#Skin] sphereB 1
 		
 	with animate on at time 0
 	(
 		sphereA.pos = [random -100 100, random -100 100, random -100 100]
 		sphereB.pos = [random -100 100, random -100 100, random -100 100]
 	)
 	
 	with animate on at time 100
 	(
 		sphereA.pos = [random -100 100, random -100 100, random -100 100]
 		sphereB.pos = [random -100 100, random -100 100, random -100 100]
 	)
 	actionMan.executeAction 0 "50021"  -- Time: Play Animation
  )
 
 

Make sense?

how can i create a for loop to duplicate an object along the x, y and z axis, to form a cube

for example, I enter 4 and the object is duplicated in the formation of a cube 4 x 4 x 4 = 64 instances…