当前位置: 首页>>代码示例>>Python>>正文


Python BulletWorld.attach_rigid_body方法代码示例

本文整理汇总了Python中panda3d.bullet.BulletWorld.attach_rigid_body方法的典型用法代码示例。如果您正苦于以下问题:Python BulletWorld.attach_rigid_body方法的具体用法?Python BulletWorld.attach_rigid_body怎么用?Python BulletWorld.attach_rigid_body使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在panda3d.bullet.BulletWorld的用法示例。


在下文中一共展示了BulletWorld.attach_rigid_body方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BulletRigidBodyNode

# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import attach_rigid_body [as 别名]
debug_node.showNormals(False)
debug_np = s.render.attach_new_node(debug_node)
bullet_world.set_debug_node(debug_node)
debug_np.show()


# The object in question
mass = BulletRigidBodyNode()
mass.set_mass(1)
mass.setLinearSleepThreshold(0)
mass.setAngularSleepThreshold(0)
shape = BulletSphereShape(1)
mass.add_shape(shape)
mass_node = s.render.attach_new_node(mass)
mass_node.set_hpr(1, 1, 1)
bullet_world.attach_rigid_body(mass)
model = s.loader.load_model('models/smiley')
model.reparent_to(mass_node)
model_axis = loader.load_model('models/zup-axis')
model_axis.reparent_to(model)
model_axis.set_pos(0, 0, 0)
model_axis.set_scale(0.2)


# The orientation to reach
target_node = s.loader.load_model('models/smiley')
target_node.reparent_to(s.render)
target_node.set_hpr(0,0,0)
target_node.set_scale(1.5)
target_node.set_render_mode_wireframe()
target_node.set_two_sided(True)
开发者ID:TheCheapestPixels,项目名称:panda_examples,代码行数:33,代码来源:toy_problem_quat.py

示例2: __init__

# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import attach_rigid_body [as 别名]
class PhysicsManager:

    def __init__(self, root_nodepath, world):
        self.world = BulletWorld()
        self.world.setGravity((0, 0, -9.81))

        self._timestep = 1 / world.tick_rate

        # # Seems that this does not function
        # on_contact_added = PythonCallbackObject(self._on_contact_added)
        # self.world.set_contact_added_callback(on_contact_added)
        # on_filter = PythonCallbackObject(self._filter_collision)
        # self.world.set_filter_callback(on_filter)

        self.listener = DirectObject()
        self.listener.accept('bullet-contact-added', self._on_contact_added)
        self.listener.accept('bullet-contact-destroyed', self._on_contact_removed)

        self.tracked_contacts = defaultdict(int)
        self.existing_collisions = set()

        # Debugging info
        debug_node = BulletDebugNode('Debug')
        debug_node.showWireframe(True)
        debug_node.showConstraints(True)
        debug_node.showBoundingBoxes(False)
        debug_node.showNormals(False)

        # Add to world
        self.debug_nodepath = root_nodepath.attachNewNode(debug_node)
        self.world.set_debug_node(debug_node)
        self.debug_nodepath.show()

    def _on_contact_removed(self, node_a, node_b):
        self.tracked_contacts[(node_a, node_b)] -= 1

    def _on_contact_added(self, node_a, node_b):
        self.tracked_contacts[(node_a, node_b)] += 1

    def _dispatch_collisions(self):
        # Dispatch collisions
        existing_collisions = self.existing_collisions

        for pair, contact_count in self.tracked_contacts.items():
            # If is new collision
            if contact_count > 0 and pair not in existing_collisions:
                existing_collisions.add(pair)

                # Dispatch collision
                node_a, node_b = pair

                entity_a = node_a.get_python_tag("entity")
                entity_b = node_b.get_python_tag("entity")

                if not (entity_a and entity_b):
                    continue

                contact_result = ContactResult(self.world, node_a, node_b)
                entity_a.messenger.send("collision_started", entity=entity_b, contacts=contact_result.contacts_a)
                entity_b.messenger.send("collision_started", entity=entity_a, contacts=contact_result.contacts_b)

            # Ended collision
            elif contact_count == 0 and pair in existing_collisions:
                existing_collisions.remove(pair)

                # Dispatch collision
                node_a, node_b = pair

                entity_a = node_a.get_python_tag("entity")
                entity_b = node_b.get_python_tag("entity")

                if not (entity_a and entity_b):
                    continue

                entity_a.messenger.send("collision_stopped", entity_b)
                entity_b.messenger.send("collision_stopped", entity_a)

    def add_entity(self, entity, component):
        body = component.body
        self.world.attach_rigid_body(body)
        body.set_python_tag("entity", entity)

    def remove_entity(self, entity, component):
        body = component.body
        self.world.remove_rigid_body(body)
        body.clear_python_tag("entity")

    def tick(self):
        self.world.do_physics(self._timestep)
        self._dispatch_collisions()

    def resimulate_pawn(self, pawn):
        pass
开发者ID:agoose77,项目名称:PyAuthServer,代码行数:95,代码来源:physics.py

示例3: BlockTutorial

# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import attach_rigid_body [as 别名]
class BlockTutorial(ShowBase):

    def __init__(self):
        # Step 2: Set up graphics
        ShowBase.__init__(self)
        self.setup_camera()
        self.load_block_model()

        # Step 3: Set up physics
        self.create_physics()
        self.load_block_physics()

        # Step 4: Link graphics and physics
        self.link()

        # This lets us see the debug skeletons for the physics objects.
        self.setup_debug()

        # Setup keybindings
        print
        print "Keybindings"
        print "-----------"

        # Turn on/off the debug skeletons by pressing 'd'
        self.accept('d', self.toggle_debug)
        print "d\t: toggle debug mode"

        # Turn on/off physics simulation by pressing 'space'
        self.accept('space', self.toggle_physics)
        print "space\t: toggle physics"

        # Exit the application by pressing 'esc'
        self.accept('escape', sys.exit)
        print "esc\t: exit"

    def setup_camera(self):
        """Position the camera so we can see the objects in the scene"""

        self.cam.set_pos(-8, -6, 2.75)
        self.cam.look_at((0, 0, 0))

    def load_block_model(self):
        """Load the 3D model of a block, and tell Panda3D to render it"""

        self.block_graphics = self.loader.loadModel("wood_block.egg")
        self.block_graphics.reparent_to(self.render)
        self.block_graphics.set_scale(0.2, 0.2, 0.2)

    def create_physics(self):
        """Create the physical world, and start a task to simulate physics"""

        self.world = BulletWorld()
        self.world.set_gravity((0, 0, -9.81))

        self.physics_on = False
        self.taskMgr.add(self.step_physics, "physics")

    def step_physics(self, task):
        """Get the amount of time that has elapsed, and simulate physics for
        that amount of time"""

        if self.physics_on:
            dt = globalClock.get_dt()
            self.world.do_physics(dt)
        return task.cont

    def toggle_physics(self):
        """Turn physics on or off."""

        self.physics_on = not(self.physics_on)

    def load_block_physics(self):
        """Create collision geometry and a physical body for the block."""

        self.block_body = BulletRigidBodyNode('block-physics')
        self.block_body.add_shape(BulletBoxShape((0.2, 0.6, 0.2)))
        self.block_body.set_mass(1.0)
        self.world.attach_rigid_body(self.block_body)

    def link(self):
        """Tell Panda3D that the block's physics and graphics should be
        linked, by making the physics NodePath be the parent of the
        graphics NodePath.

        """

        self.block_physics = self.render.attach_new_node(self.block_body)
        self.block_graphics.reparent_to(self.block_physics)

    def setup_debug(self):
        """Set up a debug node, which will render skeletons for all the
        physics objects in the physics world."""

        debug_node = BulletDebugNode('Debug')
        debug_node.show_wireframe(True)
        debug_node.show_constraints(True)
        debug_node.show_bounding_boxes(True)
        debug_node.show_normals(True)
        self.world.set_debug_node(debug_node)
        self.debug_np = self.render.attach_new_node(debug_node)
#.........这里部分代码省略.........
开发者ID:clebio,项目名称:pycon-2014-talk,代码行数:103,代码来源:tutorial.py

示例4: World

# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import attach_rigid_body [as 别名]
class World (object):
    """
    The World models basically everything about a map, including gravity, ambient light, the sky, and all map objects.
    """

    def __init__(self, camera, debug=False, audio3d=None, client=None, server=None):
        self.objects = {}

        self.incarnators = []

        self.collidables = set()

        self.updatables = set()
        self.updatables_to_add = set()
        self.garbage = set()
        self.scene = NodePath('world')


        # Set up the physics world. TODO: let maps set gravity.
        self.gravity = DEFAULT_GRAVITY
        self.physics = BulletWorld()
        self.physics.set_gravity(self.gravity)

        self.debug = debug

        if debug:
            debug_node = BulletDebugNode('Debug')
            debug_node.show_wireframe(True)
            debug_node.show_constraints(True)
            debug_node.show_bounding_boxes(False)
            debug_node.show_normals(False)
            np = self.scene.attach_new_node(debug_node)
            np.show()
            self.physics.set_debug_node(debug_node)


    def get_incarn(self):
        return random.choice(self.incarnators)


    def attach(self, obj):
        assert hasattr(obj, 'world') and hasattr(obj, 'name')
        assert obj.name not in self.objects
        obj.world = self
        if obj.name.startswith('Incarnator'):
            self.incarnators.append(obj)
        if hasattr(obj, 'create_node') and hasattr(obj, 'create_solid'):
            # Let each object define it's own NodePath, then reparent them.
            obj.node = obj.create_node()
            obj.solid = obj.create_solid()
            if obj.solid:
                if isinstance(obj.solid, BulletRigidBodyNode):
                    self.physics.attach_rigid_body(obj.solid)
                elif isinstance(obj.solid, BulletGhostNode):
                    self.physics.attach_ghost(obj.solid)
            if obj.node:
                if obj.solid:
                    # If this is a solid visible object, create a new physics node and reparent the visual node to that.
                    phys_node = self.scene.attach_new_node(obj.solid)
                    obj.node.reparent_to(phys_node)
                    obj.node = phys_node
                else:
                    # Otherwise just reparent the visual node to the root.
                    obj.node.reparent_to(self.scene)
            elif obj.solid:
                obj.node = self.scene.attach_new_node(obj.solid)
            if obj.solid and obj.collide_bits is not None:
                obj.solid.set_into_collide_mask(obj.collide_bits)
        self.objects[obj.name] = obj
        # Let the object know it has been attached.
        obj.attached()
        return obj



    def create_hector(self, name=None):
        # TODO: get random incarn, start there
        h = self.attach(Hector(name))
        h.move((0, 15, 0))
        return h

    def register_updater(self, obj):
        assert isinstance(obj, WorldObject)
        self.updatables.add(obj)

    def register_updater_later(self, obj):
        assert isinstance(obj, WorldObject)
        self.updatables_to_add.add(obj)



    def update(self, task):
        """
        Called every frame to update the physics, etc.
        """
        dt = globalClock.getDt()
        for obj in self.updatables_to_add:
            self.updatables.add(obj)
        self.updatables_to_add = set()
        for obj in self.updatables:
#.........这里部分代码省略.........
开发者ID:airvoss,项目名称:pavara,代码行数:103,代码来源:world.py

示例5: World

# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import attach_rigid_body [as 别名]
class World (object):
    """
    The World models basically everything about a map, including gravity, ambient light, the sky, and all map objects.
    """

    def __init__(self, camera, debug=False, audio3d=None, client=None, server=None):
        self.objects = {}
        self.incarnators = []
        self.collidables = set()
        self.updatables = set()
        self.updatables_to_add = set()
        self.garbage = set()
        self.render = NodePath('world')
        self.camera = camera
        self.audio3d = audio3d
        self.ambient = self._make_ambient()
        self.celestials = CompositeObject()
        self.sky = self.attach(Sky())
        # Set up the physics world. TODO: let maps set gravity.
        self.gravity = DEFAULT_GRAVITY
        self.physics = BulletWorld()
        self.physics.set_gravity(self.gravity)
        self.debug = debug
        self.client = client
        self.server = server
        if debug:
            debug_node = BulletDebugNode('Debug')
            debug_node.show_wireframe(True)
            debug_node.show_constraints(True)
            debug_node.show_bounding_boxes(False)
            debug_node.show_normals(False)
            np = self.render.attach_new_node(debug_node)
            np.show()
            self.physics.set_debug_node(debug_node)

    def _make_ambient(self):
        alight = AmbientLight('ambient')
        alight.set_color(VBase4(*DEFAULT_AMBIENT_COLOR))
        node = self.render.attach_new_node(alight)
        self.render.set_light(node)
        return node

    def attach(self, obj):
        assert hasattr(obj, 'world') and hasattr(obj, 'name')
        assert obj.name not in self.objects
        obj.world = self
        if obj.name.startswith('Incarnator'):
            self.incarnators.append(obj)
        if hasattr(obj, 'create_node') and hasattr(obj, 'create_solid'):
            # Let each object define it's own NodePath, then reparent them.
            obj.node = obj.create_node()
            obj.solid = obj.create_solid()
            if obj.solid:
                if isinstance(obj.solid, BulletRigidBodyNode):
                    self.physics.attach_rigid_body(obj.solid)
                elif isinstance(obj.solid, BulletGhostNode):
                    self.physics.attach_ghost(obj.solid)
            if obj.node:
                if obj.solid:
                    # If this is a solid visible object, create a new physics node and reparent the visual node to that.
                    phys_node = self.render.attach_new_node(obj.solid)
                    obj.node.reparent_to(phys_node)
                    obj.node = phys_node
                else:
                    # Otherwise just reparent the visual node to the root.
                    obj.node.reparent_to(self.render)
            elif obj.solid:
                obj.node = self.render.attach_new_node(obj.solid)
            if obj.solid and obj.collide_bits is not None:
                obj.solid.set_into_collide_mask(obj.collide_bits)
        self.objects[obj.name] = obj
        # Let the object know it has been attached.
        obj.attached()
        return obj

    def get_incarn(self):
        return random.choice(self.incarnators)

    def create_hector(self, name=None):
        # TODO: get random incarn, start there
        h = self.attach(Hector(name))
        h.move((0, 15, 0))
        return h

    def set_ambient(self, color):
        """
        Sets the ambient light to the given color.
        """
        self.ambient.node().set_color(VBase4(*color))

    def add_celestial(self, azimuth, elevation, color, intensity, radius, visible):
        """
        Adds a celestial light source to the scene. If it is a visible celestial, also add a sphere model.
        """
        if not self.camera:
            return
        location = Vec3(to_cartesian(azimuth, elevation, 1000.0 * 255.0 / 256.0))
        if intensity:
            dlight = DirectionalLight('celestial')
            dlight.set_color((color[0] * intensity, color[1] * intensity,
#.........这里部分代码省略.........
开发者ID:jorjuato,项目名称:pavara,代码行数:103,代码来源:world.py


注:本文中的panda3d.bullet.BulletWorld.attach_rigid_body方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。