Notifications
Clear all

[Closed] Collision creation script issue

Hey all, I hope everyone is doing well. I have an issue with this script I’m working on, and it is boggling my mind. It is used to take your whole selection and create individual UCX collision hulls around each, constrained to the parent object’s dimensions, and named after it.

The thing is, it does all of this just fine. But then it seems to condense all of these new models down to one single point (all verts are stacked one on top of each other at the origin). If someone could take a look at it and tell me what I am missing, it would be much appreciated.


(
------------------------Design---------------------------------------------------
rollout collCreation "Collision Creator"
	(
		group "Create Collision"
		(
		button coll_btn "Create Collision" align:#center
		)
		on coll_btn pressed do
		(
			selectedObjs = getCurrentSelection()
			for i = 1 to selectedObjs.Count do
			(
----------------------------FUNCTIONS------------------------------------------------
				fn maximum a b = 
				(
					if a > b then return b
						return a
				)
				fn minimum a b = 
				(
					if a < b then return b
						return a
				)
-------------End FUNCTIONS----------------------------------------------
				local objName = selectedObjs[i].name as string
				objFaces = polyop.getFaceSelection selectedObjs[i]
				objVerts = polyop.getVertsUsingFace selectedObjs[i] objFaces
				
				boxMin = point3 0 0 0
				boxMax = point3 0 0 0
				
				vertPrime == true
				
				for i = 1 to objVerts.count do
				(
					if objVerts[i] == true do
					(
						cV = objVerts[i]
						if vertPrime == true then
						(
							boxMin.x = cV.pos.x
							boxMin.y = cV.pos.y
							boxMin.z = cV.pos.Z
							boxMax.x = cV.pos.x
							boxMax.y = cV.pos.y
							boxMax.z = cV.pos.z
							vertPrime = false
						)
						else 
						(
							boxMin.x = minimum cV.pos.x boxMin.x
							boxMin.y = minimum cV.pos.y boxMin.y
							boxMin.z = minimum cV.pos.z boxMin.z
							boxMax.x = maximum cV.pos.x boxMax.x
							boxMax.y = maximum cV.pos.y boxMax.y
							boxMax.z = maximum cV.pos.z boxMax.z
						)
					)
				)
				center = point3 0 0 0
				center.x = (boxMax.x - boxMin.x) * .5 + boxMin.x
				center.y = (boxMax.y - boxMin.y) * .5 + boxMin.y
				center.z = (boxMax.z - boxMin.z) * .5 + boxMin.z
				(
					bW = (boxMax.x - boxMin.x)
					bL = (boxMax.y - boxMin.y)
					bH = (boxMax.z - boxMin.z)
					bC = center - [0 , 0 , (bH/2)]
					
					newColl = Box name:("UCX_" + objName) length:bL width:bW height:bH pos:bC
					convertToPoly newColl
				)
			)
		)
	)
------End Design---------------------------------------------------
createDialog collCreation width:200
cui.RegisterDialogBar collCreation style:#(#cui_floatable,#cui_dock_left,#cui_dock_right,#cui_handles)
)

1 Reply

I think your code has so many error

  1. Min, max function; wrong understand about initial value of min, max
  2. Wrong understand about array and bitarray
  3. Try polyOp.getVert function to get vertex position

But if you want to make make simple collision box just just this code

for obj in selection do
	Box name:("UCX_" + obj.name) length:(obj.max.y-obj.min.y) width:(obj.max.x-obj.min.x) height:(obj.max.z-obj.min.z) pos:([(obj.max.x+obj.min.x)/2,(obj.max.y+obj.min.y)/2,obj.min.z])