Exemplo n.º 1
0
    def init_entity(self, filter, entity):
        if TerrainObject in entity:
            path = entity[TerrainObject]._root
        elif Camera in entity:
            path = entity[Camera]._root
        else:
            path = None

        if filter == 'geomcollider':
            path.get_child(0).set_collide_mask(entity[GeomCollider].into_mask)
            return

        collider = entity[Collider]

        if not collider.solid:
            bounds = path.get_child(0).get_bounds()
            collider.solid = core.CollisionSphere(bounds.center, bounds.radius)

        collider.solid.tangible = collider.tangible

        cnode = core.CollisionNode(entity._uid.name)
        cnode.add_solid(collider.solid)
        cnode.set_from_collide_mask(collider.from_mask)
        cnode.set_into_collide_mask(collider.into_mask)
        cpath = path.attach_new_node(cnode)
        #cpath.show()

        collider._cpath = cpath

        cnode.set_python_tag('entity', entity)

        if collider.from_mask:
            if entity._uid.name == "camera":
                self.traverser.add_collider(cpath, self.queue)
            else:
                self.traverser.add_collider(cpath, self.handler)

                if collider.tangible:
                    self.handler.add_collider(cpath, path)

                    if TerrainObject in entity:
                        self.player_obj = entity[TerrainObject]

        if collider.joint_from_mask and Character in entity:
            actor = entity[Character]._state_actors['fly']
            self.actor = actor
            root = actor.expose_joint(None, "butterfly", "root")
            self.root = root

            for joint in actor.get_joints():
                if joint.name.startswith("butterfly."):
                    np = actor.expose_joint(root.attach_new_node(joint.name), "butterfly", joint.name, localTransform=True)
                    cpath = np.attach_new_node(core.CollisionNode("butterfly"))
                    cpath.node().set_from_collide_mask(collider.joint_from_mask)
                    cpath.node().set_into_collide_mask(collider.into_mask)
                    cpath.node().add_solid(core.CollisionSphere((0, 0.4, 0.3), 0.7))
                    cpath.node().set_tag("joint", joint.name)
                    cpath.node().modify_solid(0).set_tangible(False)
                    #cpath.show()
                    self.joint_colliders.append(cpath)
Exemplo n.º 2
0
    def __init__(self, this_room, this_door, conn_room=None, conn_door=None):
        self.this_door = this_door
        self.this_room = this_room
        self.conn_door = conn_door
        self.conn_room = conn_room

        self.hinge = self.this_door.attach_new_node('hinge')
        self.hinge.set_h(180)
        self.outside_in = self.hinge.attach_new_node('lookin_in')

        self.buff = None

        self._active = False
        self.deactivate()
        self._collision_quad = core.CollisionPolygon(
            core.Point3(0.75, 0, 0), core.Point3(-0.75, 0, 0),
            core.Point3(-0.75, 0, 2.5), core.Point3(0.75, 0, 2.5))
        cnode = core.CollisionNode(
            f'{self.this_room.name}_{self.this_door.name}')
        cnode.add_solid(self._collision_quad)
        cnode.set_from_collide_mask(core.CollideMask.all_off())
        cnode.set_into_collide_mask(0x2)
        self.collider = self.this_door.attach_new_node(cnode)
        self.collider.set_pos(self.this_door, (0, 0.05, 0))
        self.collider.set_hpr(self.this_door, (0, 0, 0))
        #self.collider.show()
        self.collider.set_collide_mask(core.BitMask32(0x2))
Exemplo n.º 3
0
def setup_lighting(the_base):

    global PICKER_RAY, TRAVERSER, COLLISION_HANDLER
    # fancy_rendering = False

    alight = core.AmbientLight('alight')
    alight.setColor(core.VBase4(0.6, 0.6, 0.6, 1))
    alnp = the_base.render.attachNewNode(alight)
    the_base.render.setLight(alnp)
    slight = core.Spotlight('slight')
    slight.setColor(core.VBase4(1, 1, 1, 1))
    lens = core.PerspectiveLens()
    slight.setLens(lens)
    slnp = the_base.render.attachNewNode(slight)
    slnp.setPos(8, -9, 128)
    slnp.setHpr(0, 270, 0)
    the_base.render.setLight(slnp)

    # I have no idea what this does, so it must not be important...
    # if fancy_rendering:
    #     # Use a 512x512 resolution shadow map
    #     slight.setShadowCaster(True, 512, 512)
    #     # Enable the shader generator for the receiving nodes
    #     the_base.render.setShaderAuto()

    TRAVERSER = core.CollisionTraverser()
    COLLISION_HANDLER = core.CollisionHandlerQueue()

    picker_node = core.CollisionNode('mouseRay')
    picker_np = the_base.camera.attachNewNode(picker_node)
    picker_node.setFromCollideMask(core.GeomNode.getDefaultCollideMask())
    PICKER_RAY = core.CollisionRay()
    picker_node.addSolid(PICKER_RAY)
    TRAVERSER.addCollider(picker_np, COLLISION_HANDLER)
Exemplo n.º 4
0
def seq_property(*items):
    """ Returns a sequence property initialized with the given items. """

    # It doesn't matter which property we use; I just happened to pick
    # CollisionNode.solids.
    cn = core.CollisionNode("")
    append = cn.add_solid
    for item in items:
        append(item)
    assert len(cn.solids) == len(items)
    return cn.solids
Exemplo n.º 5
0
    def create_node(self):
        node = core.CollisionNode(self.name)
        node.set_from_collide_mask(self.from_collide_mask)
        node.set_into_collide_mask(self.into_collide_mask)

        for solid in self.get_solids():
            solid = solid.create_node()

            if solid:
                node.addSolid(solid)

        return node
Exemplo n.º 6
0
    def start(self):
        self.disableMouse()  # Disable mouse camera control
        self.camera.setPosHpr(0, -12, 8, 0, -35, 0)  # Set the camera

        # Since we are using collision detection to do picking, we set it up like
        # any other collision detection system with a traverser and a handler
        self.picker = p3dc.CollisionTraverser()  # Make a traverser
        self.pq = p3dc.CollisionHandlerQueue()  # Make a handler
        # Make a collision node for our picker ray
        self.pickerNode = p3dc.CollisionNode('mouseRay')
        # Attach that node to the camera since the ray will need to be positioned relative to it
        self.pickerNP = self.camera.attachNewNode(self.pickerNode)
        # Everything to be picked will use bit 1. This way if we were doing other collision we could separate it
        self.pickerNode.setFromCollideMask(p3dc.BitMask32.bit(1))
        self.pickerRay = p3dc.CollisionRay()  # Make our ray
        # Add it to the collision node
        self.pickerNode.addSolid(self.pickerRay)
        # Register the ray as something that can cause collisions
        self.picker.addCollider(self.pickerNP, self.pq)
Exemplo n.º 7
0
    def start(self):
        # This code puts the standard title and instruction text on screen
        self.title = OnscreenText(text="Panda3D: Tutorial - Mouse Picking",
                                  style=1,
                                  fg=(1, 1, 1, 1),
                                  shadow=(0, 0, 0, 1),
                                  pos=(0.8, -0.95),
                                  scale=.07)
        self.escapeEvent = OnscreenText(text="ESC: Quit",
                                        parent=self.a2dTopLeft,
                                        style=1,
                                        fg=(1, 1, 1, 1),
                                        pos=(0.06, -0.1),
                                        align=p3dc.TextNode.ALeft,
                                        scale=.05)
        self.mouse1Event = OnscreenText(
            text="Left-click and drag: Pick up and drag piece",
            parent=self.a2dTopLeft,
            align=p3dc.TextNode.ALeft,
            style=1,
            fg=(1, 1, 1, 1),
            pos=(0.06, -0.16),
            scale=.05)

        self.accept('escape', sys.exit)  # Escape quits
        self.disableMouse()  # Disble mouse camera control
        self.camera.setPosHpr(0, -12, 8, 0, -35, 0)  # Set the camera
        self.setupLights()  # Setup default lighting

        # Since we are using collision detection to do picking, we set it up like
        # any other collision detection system with a traverser and a handler
        self.picker = p3dc.CollisionTraverser()  # Make a traverser
        self.pq = p3dc.CollisionHandlerQueue()  # Make a handler
        # Make a collision node for our picker ray
        self.pickerNode = p3dc.CollisionNode('mouseRay')
        # Attach that node to the camera since the ray will need to be positioned
        # relative to it
        self.pickerNP = self.camera.attachNewNode(self.pickerNode)
        # Everything to be picked will use bit 1. This way if we were doing other
        # collision we could separate it
        self.pickerNode.setFromCollideMask(p3dc.BitMask32.bit(1))
        self.pickerRay = p3dc.CollisionRay()  # Make our ray
        # Add it to the collision node
        self.pickerNode.addSolid(self.pickerRay)
        # Register the ray as something that can cause collisions
        self.picker.addCollider(self.pickerNP, self.pq)
        # self.picker.showCollisions(render)

        # Now we create the chess board and its pieces

        # We will attach all of the squares to their own root. This way we can do the
        # collision pass just on the squares and save the time of checking the rest
        # of the scene
        self.squareRoot = self.render.attachNewNode("squareRoot")

        # For each square
        self.squares = [None for i in range(64)]
        self.pieces = [None for i in range(64)]
        for i in range(64):
            # Load, parent, color, and position the model (a single square
            # polygon)
            self.squares[i] = self.loader.loadModel("chessboard_models/square")
            self.squares[i].reparentTo(self.squareRoot)
            self.squares[i].setPos(SquarePos(i))
            self.squares[i].setColor(SquareColor(i))
            # Set the model itself to be collideable with the ray. If this model was
            # any more complex than a single polygon, you should set up a collision
            # sphere around it instead. But for single polygons this works
            # fine.
            self.squares[i].find("**/polygon").node().setIntoCollideMask(
                p3dc.BitMask32.bit(1))
            # Set a tag on the square's node so we can look up what square this is
            # later during the collision pass
            self.squares[i].find("**/polygon").node().setTag('square', str(i))

            # We will use this variable as a pointer to whatever piece is currently
            # in this square

        # The order of pieces on a chessboard from white's perspective. This list
        # contains the constructor functions for the piece classes defined
        # below
        pieceOrder = (Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook)

        for i in range(8, 16):
            # Load the white pawns
            self.pieces[i] = Pawn(i, WHITE)
        for i in range(48, 56):
            # load the black pawns
            self.pieces[i] = Pawn(i, PIECEBLACK)
        for i in range(8):
            # Load the special pieces for the front row and color them white
            self.pieces[i] = pieceOrder[i](i, WHITE)
            # Load the special pieces for the back row and color them black
            self.pieces[i + 56] = pieceOrder[i](i + 56, PIECEBLACK)

        # This will represent the index of the currently highlited square
        self.hiSq = False
        # This wil represent the index of the square where currently dragged piece
        # was grabbed from
        self.dragging = False

        # Start the task that handles the picking
        self.mouseTask = self.taskMgr.add(self._mouseTask, 'mouseTask')
        self.accept("mouse1", self.grabPiece)  # left-click grabs a piece
        self.accept("mouse1-up", self.releasePiece)  # releasing places it
Exemplo n.º 8
0
    def start(self):

        # Set the background color to black
        self.win.setClearColor((0, 0, 0, 1))

        # This is used to store which keys are currently pressed.
        self.keyMap = {
            "left": 0, "right": 0, "forward": 0, "cam-left": 0, "cam-right": 0}

        # Post the instructions
        self.title = addTitle(
            "Panda3D Tutorial: Roaming Ralph (Walking on Uneven Terrain)")
        self.inst1 = addInstructions(0.06, "[ESC]: Quit")
        self.inst2 = addInstructions(0.12, "[Left Arrow]: Rotate Ralph Left")
        self.inst3 = addInstructions(0.18, "[Right Arrow]: Rotate Ralph Right")
        self.inst4 = addInstructions(0.24, "[Up Arrow]: Run Ralph Forward")
        self.inst6 = addInstructions(0.30, "[A]: Rotate p3dc.Camera Left")
        self.inst7 = addInstructions(0.36, "[S]: Rotate p3dc.Camera Right")

        # Set up the environment
        #
        # This environment model contains collision meshes.  If you look
        # in the egg file, you will see the following:
        #
        #    <Collide> { Polyset keep descend }
        #
        # This tag causes the following mesh to be converted to a collision
        # mesh -- a mesh which is optimized for collision, not rendering.
        # It also keeps the original mesh, so there are now two copies ---
        # one optimized for rendering, one for collisions.

        self.environ = self.loader.loadModel("roaming_ralph_models/world")
        self.environ.reparentTo(self.render)

        # Create the main character, Ralph

        ralphStartPos = self.environ.find("**/start_point").getPos()
        self.ralph = Actor("roaming_ralph_models/ralph",
                           {"run": "roaming_ralph_models/ralph-run",
                            "walk": "roaming_ralph_models/ralph-walk"})
        self.ralph.reparentTo(self.render)
        self.ralph.setScale(.2)
        self.ralph.setPos(ralphStartPos + (0, 0, 0.5))

        # Create a floater object, which floats 2 units above ralph.  We
        # use this as a target for the camera to look at.

        self.floater = p3dc.NodePath(p3dc.PandaNode("floater"))
        self.floater.reparentTo(self.ralph)
        self.floater.setZ(2.0)

        # Accept the control keys for movement and rotation

        self.accept("escape", sys.exit)
        self.accept("arrow_left", self.setKey, ["left", True])
        self.accept("arrow_right", self.setKey, ["right", True])
        self.accept("arrow_up", self.setKey, ["forward", True])
        self.accept("a", self.setKey, ["cam-left", True])
        self.accept("s", self.setKey, ["cam-right", True])
        self.accept("arrow_left-up", self.setKey, ["left", False])
        self.accept("arrow_right-up", self.setKey, ["right", False])
        self.accept("arrow_up-up", self.setKey, ["forward", False])
        self.accept("a-up", self.setKey, ["cam-left", False])
        self.accept("s-up", self.setKey, ["cam-right", False])

        self.taskMgr.add(self.move, "moveTask")

        # Game state variables
        self.isMoving = False

        # Set up the camera
        self.disableMouse()
        self.camera.setPos(self.ralph.getX(), self.ralph.getY() + 10, 2)

        # We will detect the height of the terrain by creating a collision
        # ray and casting it downward toward the terrain.  One ray will
        # start above ralph's head, and the other will start above the camera.
        # A ray may hit the terrain, or it may hit a rock or a tree.  If it
        # hits the terrain, we can detect the height.  If it hits anything
        # else, we rule that the move is illegal.
        self.cTrav = p3dc.CollisionTraverser()

        self.ralphGroundRay = p3dc.CollisionRay()
        self.ralphGroundRay.setOrigin(0, 0, 9)
        self.ralphGroundRay.setDirection(0, 0, -1)
        self.ralphGroundCol = p3dc.CollisionNode('ralphRay')
        self.ralphGroundCol.addSolid(self.ralphGroundRay)
        self.ralphGroundCol.setFromCollideMask(p3dc.CollideMask.bit(0))
        self.ralphGroundCol.setIntoCollideMask(p3dc.CollideMask.allOff())
        self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol)
        self.ralphGroundHandler = p3dc.CollisionHandlerQueue()
        self.cTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler)

        self.camGroundRay = p3dc.CollisionRay()
        self.camGroundRay.setOrigin(0, 0, 9)
        self.camGroundRay.setDirection(0, 0, -1)
        self.camGroundCol = p3dc.CollisionNode('camRay')
        self.camGroundCol.addSolid(self.camGroundRay)
        self.camGroundCol.setFromCollideMask(p3dc.CollideMask.bit(0))
        self.camGroundCol.setIntoCollideMask(p3dc.CollideMask.allOff())
        self.camGroundColNp = self.camera.attachNewNode(self.camGroundCol)
        self.camGroundHandler = p3dc.CollisionHandlerQueue()
        self.cTrav.addCollider(self.camGroundColNp, self.camGroundHandler)

        # Uncomment this line to see the collision rays
        #self.ralphGroundColNp.show()
        #self.camGroundColNp.show()

        # Uncomment this line to show a visual representation of the
        # collisions occuring
        #self.cTrav.showCollisions(render)

        # Create some lighting
        ambientLight = p3dc.AmbientLight("ambientLight")
        ambientLight.setColor((.3, .3, .3, 1))
        directionalLight = p3dc.DirectionalLight("directionalLight")
        directionalLight.setDirection((-5, -5, -5))
        directionalLight.setColor((1, 1, 1, 1))
        directionalLight.setSpecularColor((1, 1, 1, 1))
        self.render.setLight(self.render.attachNewNode(ambientLight))
        self.render.setLight(self.render.attachNewNode(directionalLight))
Exemplo n.º 9
0
    def start(self):

        # Set time of day
        if self.render_pipeline: self.render_pipeline.daytime_mgr.time = "7:40"

        # Use a special effect for rendering the scene, this is because the
        # roaming ralph model has no normals or valid materials
        if self.render_pipeline:
            self.render_pipeline.set_effect(
                ape.render(),
                "roaming_ralph_pipeline_scene-effect.yaml", {},
                sort=250)

        self.keyMap = {
            "left": 0,
            "right": 0,
            "forward": 0,
            "backward": 0,
            "cam-left": 0,
            "cam-right": 0
        }
        self.speed = 1.0
        ape.base().win.setClearColor(p3dc.Vec4(0, 0, 0, 1))

        # Post the instructions

        self.inst4 = addInstructions(0.90, "[W]  Run Ralph Forward")
        self.inst4 = addInstructions(0.85, "[S]  Run Ralph Backward")
        self.inst2 = addInstructions(0.80, "[A]  Rotate Ralph Left")
        self.inst3 = addInstructions(0.75, "[D]  Rotate Ralph Right")
        self.inst6 = addInstructions(0.70, "[Left Arrow]  Rotate Camera Left")
        self.inst7 = addInstructions(0.65,
                                     "[Right Arrow]  Rotate Camera Right")

        # Set up the environment
        #
        # This environment model contains collision meshes.  If you look
        # in the egg file, you will see the following:
        #
        #    <Collide> { Polyset keep descend }
        #
        # This tag causes the following mesh to be converted to a collision
        # mesh -- a mesh which is optimized for collision, not rendering.
        # It also keeps the original mesh, so there are now two copies ---
        # one optimized for rendering, one for collisions.

        self.environ = ape.loader().loadModel(
            "roaming_ralph_pipeline_resources/world")
        self.environ.reparentTo(ape.render())
        self.environ.setPos(0, 0, 0)

        # Remove wall nodes
        self.environ.find("**/wall").remove_node()

        # Create the main character, Ralph
        self.ralph = Actor(
            "roaming_ralph_pipeline_resources/ralph", {
                "run": "roaming_ralph_pipeline_resources/ralph-run",
                "walk": "roaming_ralph_pipeline_resources/ralph-walk",
                "stand": "roaming_ralph_pipeline_resources/ralph"
            })
        self.ralph.reparentTo(ape.render())
        self.ralph.setScale(.2)
        self.ralph.setPos(p3dc.Vec3(-110.9, 29.4, 1.8))

        # Create a floater object.  We use the "floater" as a temporary
        # variable in a variety of calculations.

        self.floater = p3dc.NodePath(p3dc.PandaNode("floater"))
        self.floater.reparentTo(ape.render())

        # Accept the control keys for movement and rotation

        self.accept("a", self.setKey, ["left", 1])
        self.accept("d", self.setKey, ["right", 1])
        self.accept("w", self.setKey, ["forward", 1])
        self.accept("s", self.setKey, ["backward", 1])
        self.accept("arrow_left", self.setKey, ["cam-left", 1])
        self.accept("arrow_right", self.setKey, ["cam-right", 1])
        self.accept("a-up", self.setKey, ["left", 0])
        self.accept("d-up", self.setKey, ["right", 0])
        self.accept("w-up", self.setKey, ["forward", 0])
        self.accept("s-up", self.setKey, ["backward", 0])
        self.accept("arrow_left-up", self.setKey, ["cam-left", 0])
        self.accept("arrow_right-up", self.setKey, ["cam-right", 0])
        self.accept("=", self.adjustSpeed, [0.25])
        self.accept("+", self.adjustSpeed, [0.25])
        self.accept("-", self.adjustSpeed, [-0.25])

        ape.base().taskMgr.add(self.move, "moveTask")

        # Game state variables
        self.isMoving = False

        # Set up the camera

        ape.base().disableMouse()
        ape.base().camera.setPos(self.ralph.getX() + 10,
                                 self.ralph.getY() + 10, 2)
        ape.base().camLens.setFov(80)

        # We will detect the height of the terrain by creating a collision
        # ray and casting it downward toward the terrain.  One ray will
        # start above ralph's head, and the other will start above the camera.
        # A ray may hit the terrain, or it may hit a rock or a tree.  If it
        # hits the terrain, we can detect the height.  If it hits anything
        # else, we rule that the move is illegal.
        self.cTrav = p3dc.CollisionTraverser()

        self.ralphGroundRay = p3dc.CollisionRay()
        self.ralphGroundRay.setOrigin(0, 0, 1000)
        self.ralphGroundRay.setDirection(0, 0, -1)
        self.ralphGroundCol = p3dc.CollisionNode('ralphRay')
        self.ralphGroundCol.addSolid(self.ralphGroundRay)
        self.ralphGroundCol.setFromCollideMask(p3dc.BitMask32.bit(0))
        self.ralphGroundCol.setIntoCollideMask(p3dc.BitMask32.allOff())
        self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol)
        self.ralphGroundHandler = p3dc.CollisionHandlerQueue()
        self.cTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler)

        self.camGroundRay = p3dc.CollisionRay()
        self.camGroundRay.setOrigin(0, 0, 1000)
        self.camGroundRay.setDirection(0, 0, -1)
        self.camGroundCol = p3dc.CollisionNode('camRay')
        self.camGroundCol.addSolid(self.camGroundRay)
        self.camGroundCol.setFromCollideMask(p3dc.BitMask32.bit(0))
        self.camGroundCol.setIntoCollideMask(p3dc.BitMask32.allOff())
        self.camGroundColNp = ape.base().camera.attachNewNode(
            self.camGroundCol)
        self.camGroundHandler = p3dc.CollisionHandlerQueue()
        self.cTrav.addCollider(self.camGroundColNp, self.camGroundHandler)

        # Uncomment this line to see the collision rays
        #self.ralphGroundColNp.show()
        #self.camGroundColNp.show()

        # Uncomment this line to show a visual representation of the
        # collisions occuring
        #self.cTrav.showCollisions(ape.render())

        # Create some lighting
        ambientLight = p3dc.AmbientLight("ambientLight")
        ambientLight.setColor(p3dc.Vec4(.3, .3, .3, 1))
        directionalLight = p3dc.DirectionalLight("directionalLight")
        directionalLight.setDirection(p3dc.Vec3(-5, -5, -5))
        directionalLight.setColor(p3dc.Vec4(1, 1, 1, 1))
        directionalLight.setSpecularColor(p3dc.Vec4(1, 1, 1, 1))
        ape.render().setLight(ape.render().attachNewNode(ambientLight))
        ape.render().setLight(ape.render().attachNewNode(directionalLight))