Working code, re-distill into functions/classes?
I know this code if hard to read,
<em><strong>
I have not really taken any classes on python,
if I comment out my code completely can some one condense it into functions, and or classes that can then be ran as modules?
My code could be used as a starting point , or a total re-code that has the same end result is fine as well.
Part 1
Each component has 2 “layers”
a physics layer, (the actual component) and a Tmesh “Jack Map”
Basically I found that triangle mesh can not be compound parented,
and regular bound’s don’t return a material.
So in order to have mapped jacks, a item spawns a Tmesh
this item is encoded with ‘Component’ by the item which is it’s own GameObject.
So
add=scene.addObject(‘Tmesh’,own,0)
add[‘Component’]=own
then the Tmesh does
Tmesh.worldPosition=Component.worldPosition
Tmesh.worldOrientation=Comonent.worldOrientation
each frame. (the tmesh are on a seperate collision layer from physics)
so then, when attempting assembly, the player casts a ray, if it hits a component, it then casts a ray out of it’s Y axis.
if the ray hits a ‘Tmesh’ it looks up the 'Component" associate with the Tmesh, then it checks a property based on the Tmesh material it hit, to check if the jack is empty,
if it’s empty (Component[‘MAJack_x’]=False:)
Set true, then get a list of all of Component’s children - Component[‘Children’]
if it’s empty then
List1=Component_a+Component_b
Component_a[‘Parent’]=Component_b
get center of mass,
get mass of both items,
Parent kist to each items Component[‘Parent’]
Do Component_a[‘Children’]=Component_a.childrenRecurisve
(storying the hiarchy of assembly)
then I libNew A compound object center, and parent the list to it.
this makes a compound object with correct mass, center of mass and physics bound.
Step 2- Disassembly,
Create two lists when striking a component with “split” command
Component[‘Children’] and everything above Component in the hierarchy.
So then we build 2 Compound objects using the lists,
here is my code such as it is.
##Parent check
import bge
from mathutils import Vector
from bge import render
scene=bge.logic.getCurrentScene()
counter=scene.objects['Counter']
def get_centroid_and_mass(objects):
centroid = Vector()
mass = 0.0
for obj in objects:
if 'Mass' in obj and obj.name!="Parent":
mass += obj['Mass']
print(mass,obj)
for obj in objects:
if 'Mass' in obj and obj.name!="Parent":
influence = obj['Mass'] / mass
centroid += obj.worldPosition * influence
return centroid, mass
def join_objects(obj,target):
children = obj['target_objects']
if not hasattr(bge.logic, 'num_libnews'):
bge.logic.num_libnews = 0
parent_name = "Cube.005"
targetx = obj
parent = scene.addObject('Parent', targetx)
new_mesh = bge.logic.LibNew(parent_name + str(bge.logic.num_libnews), 'Mesh', [parent_name])[0]
parent.replaceMesh(new_mesh)
bge.logic.num_libnews += 1
counter['x']+=1
centroid, mass = get_centroid_and_mass(children)
print(centroid, mass)
parent.worldPosition = centroid
parent.mass=mass
obj['Root']=parent
obj['RootName']=parent.name
target['Root']=parent
target['RootName']=parent.name
P2=target['Parent']
target.setParent(parent,1,0)
for objects in children:
if objects.name!="Parent" and objects.name!=target:
objects.removeParent()
if objects['Parent']!="Empty":
p=objects['Parent']
objects.setParent(p,0,0)
R=objects['Root']
objects['Root']=parent
objects['RootName']=parent.name
for objects in children:
objects['Children']=objects.childrenRecursive
objects['ChildrenNames']=str(objects['Children'])
objects.removeParent()
objects.setParent(parent,1,0)
if obj.name!="Parent":
obj.setParent(parent, 1, 0)
else:
obj.applyMovement((0,0,-100),0)
##obj.endObject()
parent.worldLinearVelocity=(0,0,0)
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
scene=bge.logic.getCurrentScene()
Mouse = cont.sensors['Mouse']
Timer = cont.sensors['Property1']
##GetComponent A
if Mouse.positive and Timer.positive:
St = Vector ([0,1,0])
Start = own.worldPosition+(own.worldOrientation * St)
##add= scene.addObject("HL",own,10)
##add.worldPosition=Start
St2 = Vector ([0,7.5,0])
End = own.worldPosition+(own.worldOrientation * St2)
##add2= scene.addObject("HL",own,10)
##add2.worldPosition=End
hitOb, hitPoint, hitNormal, hitPoly = own.rayCast(End, Start, 0, 'Tmesh', 0, 1, 1)
render.drawLine(Start,End,(255,0,0))
if hitOb:
H=hitOb
print(H.name)
if 'Tmesh' in H:
if 'Component' in H:
if H.invalid==False:
T=H['Component']
if T.invalid==False:
target=H['Component']
print(target.name)
own['PtargetA']=target
##GetComponent B
t = Vector ([0,1.5,0])
Start2 = target.worldPosition+(target.worldOrientation * St)
##add= scene.addObject("HL",own,10)
##add.worldPosition=Start
St2 = Vector ([0,7.5,0])
End2 = hitOb.worldPosition+(hitOb.worldOrientation * St2)
hitOb2, hitPoint2, hitNormal2, hitPoly2 = hitOb.rayCast(End2, Start2, 0, 'Tmesh', 0, 1, 1)
render.drawLine(Start2,End2,(0,0,255))
if hitOb2:
if 'Tmesh' in hitOb2:
H2=hitOb2
Component_b=H2['Component']
own['PTargetB']=Component_b
## Get jack and check if occupied
if hitPoly2 and hitOb2:
hitMat = hitPoly2.material_name
own['picup']=False
own['HitMat']=hitMat
if 'Jack_' in hitMat:
print(hitMat)
if 'hitMat' in Component_b:
if Component_b[hitMat]==False:
target['hitMat']=hitMat
target['Parent']=Component_b
target['ParentName']=Component_b.name
print("Jack is empty")
Component_b[hitMat]=True
hitObPos=Component_b.worldPosition
spot=hitObPos+hitNormal2
target.alignAxisToVect(-hitNormal2, 1, 1)
list=[]
target.worldPosition=spot
hOffset = Vector([0,-1 ,0])
target.worldPosition += target.worldOrientation * hOffset
if Component_b['Root']!="Empty":
p=Component_b['Root']
if p.invalid==False:
Component_b=Component_b['Root']
list=Component_b.childrenRecursive
else:
list=[]
list=list+[target]
list=list+[Component_b]
Component_b['target_objects']=list
join_objects(Component_b,target)
main()
Attachments
CompoundAssemblyV101.blend (830 KB)