Code archives/3D Graphics - Mesh/Attach Body Parts

This code has been declared by its author to be Public Domain code.

Download source code

Attach Body Parts by Leon Drake2007
I am working on a game where i have a base skeleton that will have all my characters animations but i wanted to keep the body parts seperate in case i may want to replace the mesh with like clothing or armor. Here is a good set of functions to do this without the trouble of manually lining parts up. Provided the Part your attaching has bones that have the same name as the skeletons.
Graphics3D 800,600,16,2





SetBuffer BackBuffer()


global skeleton,part

Type child

Field child,childcount,parentmesh

End Type

lit = createlight()
cam = createcamer()
skeleton = LoadAnimMesh(tbodyfile$)
addtoskeleton(tpartfile$,ttextfile$)

Repeat
Cls


UpdateWorld()
RenderWorld()
Flip

Until KeyHit(1)


End

Function addtoskeleton(tpartfile$,ttextfile$)

	
		part = LoadAnimMesh(tpartfile$)
		parttex = LoadTexture(ttextfile$,1)
		EntityTexture part,parttex

		PositionEntity part,EntityX(skeleton),EntityY(skeleton),EntityZ(skeleton)
		getchildren(part)
		getchildren(skeleton)
		For c.child = Each child
		If c\parentmesh = part Then
		For x.child = Each child
		If x\parentmesh = skeleton Then
		If EntityName(x\child) = EntityName(c\child) Then
		 DebugLog "Attaching "+EntityName(c\child)+" to "+EntityName(x\child)

		EntityParent(c\child,0)
			
			PositionEntity c\child,EntityX(x\child,True),EntityY(x\child,True),EntityZ(x\child,True)
			
			
			RotateEntity c\child,EntityPitch(x\child,True),EntityYaw(x\child,True),EntityRoll(x\child,True)
			
			EntityParent(c\child,x\child)
			

		Exit
		EndIf
		EndIf
		Next
		EndIf
		
		Next
		

		

	
For c.child = Each child 
Delete c
Next
End Function





Function getchildren(ent)
	entp = ent
	While ent
	c.child = New child
	c\child = ent
	c\parentmesh = entp
	ent = NextChild(ent)
	Wend

End Function

;NextChild() function by Beaker
Function NextChild(ent)
		Local siblingcnt
		If CountChildren(ent)>0
			Return GetChild(ent,1)
		EndIf
	
		Local foundunused=False
		Local foundent = 0, parent,sibling
		While foundunused=False And ent<>0
			parent = GetParent(ent)
			If parent<>0
				If CountChildren(parent)>1
					If GetChild(parent,CountChildren(parent))<>ent
						For siblingcnt = 1 To CountChildren(parent)
							sibling = GetChild(parent,siblingcnt)
							If sibling=ent
								foundunused = True
								foundent = GetChild(parent,siblingcnt+1)
							EndIf
						Next
					EndIf
				EndIf
			EndIf
			ent = parent
		Wend
		Return foundent
End Function

Comments

Leon Drake2007
woops forgot to change the type over to a regular var

PositionEntity ppart\mesh,EntityX(pself\mesh),EntityY(pself\mesh),EntityZ(pself\mesh)

should be

PositionEntity part,EntityX(skeleton),EntityY(skeleton),EntityZ(skeleton)


Wings2007
This is a good idea indeed.
Curently i am having to create multible animations.
ang if i want to change the animation i will have to Reexport all entitys.

this seams like a good sollution.


Code Archives Forum