def __init__(self, model, world, worldNP, pos, scale, hpr):
		self.worldNP = worldNP
		bulletWorld = world
		
		# Initialize the model.
		self.AIModel = loader.loadModel(model)
		self.AINode = BulletRigidBodyNode('AIChar')
		self.AIModel.setScale(scale)
		self.AIModel.flattenLight() # Combines all geom nodes into one geom node.

		# Build the triangle mesh shape and attach it with the transform.
		geom = self.AIModel.findAllMatches('**/+GeomNode').getPath(0).node().getGeom(0)
		mesh = BulletTriangleMesh()
		mesh.addGeom(geom)
		shape = BulletTriangleMeshShape(mesh, dynamic=False)
		self.AINode.addShape(shape, TransformState.makePosHprScale(Point3(0,0,0), hpr, scale))
		self.AINode.setMass(0)
		bulletWorld.attachRigidBody(self.AINode)
		
		# Apply the same transforms on the model being rendered.
		self.AIModel.reparentTo(render)
		self.AIModel.setH(hpr.getX())
		self.AIModel.setP(hpr.getY())
		self.AIModel.setR(hpr.getZ())
		self.AINode.setAngularFactor(Vec3(0,0,0))
		
		# Attach it to the world.
		self.AIChar = self.worldNP.attachNewNode(self.AINode)
		self.AIModel.reparentTo(self.AIChar)
		self.AIChar.setPos(pos)
    def __init__(self, model, world, worldNP, pos, scale, hpr):
        self.worldNP = worldNP
        bulletWorld = world

        # Initialize the model.
        self.AIModel = loader.loadModel(model)
        self.AINode = BulletRigidBodyNode("AIChar")
        self.AIModel.setScale(scale)
        self.AIModel.flattenLight()  # Combines all geom nodes into one geom node.

        # Build the triangle mesh shape and attach it with the transform.
        geom = self.AIModel.findAllMatches("**/+GeomNode").getPath(0).node().getGeom(0)
        mesh = BulletTriangleMesh()
        mesh.addGeom(geom)
        shape = BulletTriangleMeshShape(mesh, dynamic=False)
        self.AINode.addShape(shape, TransformState.makePosHprScale(Point3(0, 0, 0), hpr, scale))
        self.AINode.setMass(0)
        bulletWorld.attachRigidBody(self.AINode)

        # Apply the same transforms on the model being rendered.
        self.AIModel.reparentTo(render)
        self.AIModel.setH(hpr.getX())
        self.AIModel.setP(hpr.getY())
        self.AIModel.setR(hpr.getZ())
        self.AINode.setAngularFactor(Vec3(0, 0, 0))

        # Attach it to the world.
        self.AIChar = self.worldNP.attachNewNode(self.AINode)
        self.AIModel.reparentTo(self.AIChar)
        self.AIChar.setPos(pos)
Beispiel #3
0
def getModelBaseDimensions(model):

    origTransform = model.getNetTransform()

    model = model.copyTo(model.getParent())
    model.detachNode()
    model.setTransform(
        TransformState.makePosHprScale(origTransform.getPos(),
                                       LVector3f(0, 0, 0),
                                       origTransform.getScale()))
    minRefBounds, maxRefBounds = model.getTightBounds()
    model.removeNode()

    refDims = maxRefBounds - minRefBounds
    width, depth, heigth = refDims.x, refDims.y, refDims.z
    return width, depth, heigth
Beispiel #4
0
def getApproximationForModel(model, mode='box'):

    if mode == 'mesh':
        transform = model.getNetTransform()
        approxModel = model.copyTo(model.getParent())
        approxModel.detachNode()
        approxModel.setTransform(transform)

    elif mode == 'box':
        # Bounding box approximation
        # FIXME: taking the tight bounds after the transform does not fit well models
        #        that are rotated (e.g. diagonal orientation).
        minRefBounds, maxRefBounds = model.getTightBounds()
        refDims = maxRefBounds - minRefBounds
        refPos = model.getPos()
        refCenter = minRefBounds + (maxRefBounds - minRefBounds) / 2.0
        refDeltaCenter = refCenter - refPos

        approxModel = loadModel(os.path.join(MODEL_DATA_DIR, 'cube.egg'))

        # Rescale the cube model to match the bounding box of the original
        # model
        minBounds, maxBounds = approxModel.getTightBounds()
        dims = maxBounds - minBounds
        pos = approxModel.getPos()
        center = minBounds + (maxBounds - minBounds) / 2.0
        deltaCenter = center - pos

        position = refPos + refDeltaCenter - deltaCenter
        scale = LVector3f(refDims.x / dims.x, refDims.y / dims.y,
                          refDims.z / dims.z)

        # NOTE: remove the local transform used to center the model
        transform = model.getNetTransform().compose(
            model.getTransform().getInverse())
        transform = transform.compose(
            TransformState.makePosHprScale(position, LVector3f(0, 0, 0),
                                           scale))
        approxModel.setTransform(transform)
    else:
        raise Exception('Unknown mode type for object shape: %s' % (mode))

    approxModel.setName(model.getName())

    return approxModel
    def __addConvexHull(self, body, model, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1) ):        
        
        def one():
            geom = model.node().getGeom(0)          
            shape = BulletConvexHullShape()
            shape.addGeom(geom)
            body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )
            return []

        children =  model.findAllMatches('**/+GeomNode') or one()

        model.flattenLight()

        for piece in children:
            shape = BulletConvexHullShape()
            geom = piece.node().getGeom(0)
            shape.addGeom(geom)
            body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )
    def __addTriangleMesh(self, body, model, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1), dynamic=True):        
        
        mesh = BulletTriangleMesh()
      
        def one():
            geom = model.node().getGeom(0)            
            mesh.addGeom(geom)
            return []

        children =  model.findAllMatches('**/+GeomNode') or one()

        model.flattenLight()

        for piece in children:
            geom = piece.node().getGeom(0)
            mesh.addGeom(geom)

        shape = BulletTriangleMeshShape(mesh, dynamic=dynamic )
        body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )
 def __addCapsule(self, body, model, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1) ):
     size = self.getSize(model)
     diam = max(size.x,size.y)
     shape = BulletCapsuleShape(diam/2, size.z-diam, ZUp)       
     body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )
 def __addCylinder(self, body, model, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1) ):
     size = self.getSize(model)
     shape = BulletCylinderShape(max(size.x,size.y)/2, size.z, ZUp)
     body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )
 def __addBox(self, body, model, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1) ):
     size = self.getSize(model)
     shape = BulletBoxShape( size/2 )
     body.addShape(shape, TransformState.makePosHprScale(pos,hpr,scale) )
 def __addPlane(self, body, size=(0,0,1), pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1) ):
     shape = BulletPlaneShape(Vec3(size), 0)
     body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale)  )
 def one():
     geom = model.node().getGeom(0)          
     shape = BulletConvexHullShape()
     shape.addGeom(geom)
     body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )
     return []
 def __addCone(self, body, size, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1) ):
     shape = BulletConeShape(max(size.x,size.y)/2, size.z, ZUp)
     body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )
 def __addSphere(self, body, size, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1) ):
     shape = BulletSphereShape( max(size)/2 )
     body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )
 def __addTriangleMesh(self, body, geom, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1), dynamic=True):
     mesh = BulletTriangleMesh()
     mesh.addGeom(geom)
     shape = BulletTriangleMeshShape(mesh, dynamic=dynamic )
     body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )
 def __addConvexHull(self, body, geom, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1) ):
     shape = BulletConvexHullShape()
     shape.addGeom(geom)
     body.addShape( shape, TransformState.makePosHprScale(pos,hpr,scale) )