Пример #1
0
    def __init__(self):
        from pandac.PandaModules import CollisionHandlerFloor, CollisionHandlerPusher, CollisionHandlerEvent, CollisionTraverser
        from pandac.PandaModules import DirectionalLight, AmbientLight, VBase4
        ShowBase.__init__(self)

        self.sky = self.loader.loadModel('models/sky-sphere')
        self.sky.reparentTo(self.render)
        self.stage = self.loader.loadModel('models/test-collide')
        self.stage.reparentTo(self.render)
        self.floor = self.stage.findAllMatches('**/=CollideType=floor')
        self.floor.setCollideMask(FLOOR_MASK)
        self.obstacles = self.stage.findAllMatches('**/=CollideType=obstacle')
        if self.obstacles:
            self.obstacles.setCollideMask(OBSTACLE_MASK)
        self.zones = self.stage.findAllMatches('**/=CollideType=zone')
        if self.zones:
            self.zones.setCollideMask(ZONE_MASK)
        self.create_stanchions()

        # Character rig, which allows camera to follow character
        self.char_rig = self.stage.attachNewNode('char_rig')

        self.active_char = Character('mainchar', self.char_rig)

        self.cam.reparentTo(self.char_rig)
        self.cam.setPos(0.5, -3, 1.5)
        self.cam.lookAt(0.5, 0, 1.5)

        self.light = DirectionalLight('dlight')
        self.light.setColor(VBase4(0.3, 0.28, 0.26, 1.0))
        self.lightNP = self.stage.attachNewNode(self.light)
        self.lightNP.setHpr(-75, -45, 0)
        self.stage.setLight(self.lightNP)

        self.amblight = AmbientLight('amblight')
        self.amblight.setColor(VBase4(0.7, 0.68, 0.66, 1.0))
        self.amblightNP = self.stage.attachNewNode(self.amblight)
        self.stage.setLight(self.amblightNP)

        self.accept('w', self.active_char.begin_forward)
        self.accept('a', self.active_char.begin_left)
        self.accept('s', self.active_char.begin_backward)
        self.accept('d', self.active_char.begin_right)
        self.accept('w-up', self.active_char.end_forward)
        self.accept('a-up', self.active_char.end_left)
        self.accept('s-up', self.active_char.end_backward)
        self.accept('d-up', self.active_char.end_right)
        self.taskMgr.add(self.active_char.MoveTask, 'MoveTask')

        self.look = False
        self.prev_pos = None
        self.accept('mouse2', self.begin_look)
        self.accept('mouse2-up', self.end_look)
        self.accept('mouse3', self.active_char.begin_spin)
        self.accept('mouse3-up', self.active_char.end_spin)
        self.taskMgr.add(self.MouseTask, 'MouseTask')

        self.floor_handler = CollisionHandlerFloor()
        self.floor_handler.addCollider(self.active_char.actor_from_floor,
                                       self.char_rig)
        self.wall_handler = CollisionHandlerPusher()
        self.wall_handler.addCollider(self.active_char.actor_from_obstacle,
                                      self.char_rig)
        self.zone_handler = CollisionHandlerEvent()
        self.zone_handler.addInPattern('%fn-into')
        self.zone_handler.addOutPattern('%fn-out')

        def foo(entry):
            print 'You are in the zone'

        def bar(entry):
            print 'You are not in the zone'

        self.accept('blockchar_zone-into', foo)
        self.accept('blockchar_zone-out', bar)
        self.cTrav = CollisionTraverser('main traverser')
        self.cTrav.setRespectPrevTransform(True)
        self.cTrav.addCollider(self.active_char.actor_from_floor,
                               self.floor_handler)
        self.cTrav.addCollider(self.active_char.actor_from_obstacle,
                               self.wall_handler)
        self.cTrav.addCollider(self.active_char.actor_from_zone,
                               self.zone_handler)
Пример #2
0
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import CollisionHandlerEvent, CollisionHandlerQueue, CollisionHandlerFloor, CollisionHandlerPusher, CollisionNode, CollisionTube, CollisionSphere, CollisionRay, CollisionTraverser, BitMask32
from doomsday.base import Globals, SoundBank
from doomsday.world.Splat import Splat
from panda3d.core import Vec4, Vec3
import math

collHdl = CollisionHandlerEvent()
collHdlF = CollisionHandlerFloor()
collHdlF.setMaxVelocity(8)
collHdlPush = CollisionHandlerPusher()

class WorldCollisions(DirectObject):
    
    def __init__(self):
        base.cTrav = CollisionTraverser()
        self.called = False
        
    def handleInCollisions(self, entry):
        cogCollNames = ['coll_body', 'coll_crit']
        intoNode = entry.getIntoNodePath()
        fromNode = entry.getFromNodePath()
        if(self.called):
            return
        self.called = True
        splatColor = Vec4(0.7, 0.7, 0.8, 1.0)
        splatScale = 0.7
        if(intoNode.getName() in cogCollNames):
            cog = intoNode.getParent().getPythonTag('Cog')
            gag = fromNode.getParent().getPythonTag('Stats')
            splatColor = gag.getSplatColor()
Пример #3
0
class FreeBLiTZ(ShowBase):
    def __init__(self):
        from pandac.PandaModules import CollisionHandlerFloor, CollisionHandlerPusher, CollisionHandlerEvent, CollisionTraverser
        from pandac.PandaModules import DirectionalLight, AmbientLight, VBase4
        ShowBase.__init__(self)

        self.sky = self.loader.loadModel('models/sky-sphere')
        self.sky.reparentTo(self.render)
        self.stage = self.loader.loadModel('models/test-collide')
        self.stage.reparentTo(self.render)
        self.floor = self.stage.findAllMatches('**/=CollideType=floor')
        self.floor.setCollideMask(FLOOR_MASK)
        self.obstacles = self.stage.findAllMatches('**/=CollideType=obstacle')
        if self.obstacles:
            self.obstacles.setCollideMask(OBSTACLE_MASK)
        self.zones = self.stage.findAllMatches('**/=CollideType=zone')
        if self.zones:
            self.zones.setCollideMask(ZONE_MASK)
        self.create_stanchions()

        # Character rig, which allows camera to follow character
        self.char_rig = self.stage.attachNewNode('char_rig')

        self.active_char = Character('mainchar', self.char_rig)

        self.cam.reparentTo(self.char_rig)
        self.cam.setPos(0.5, -3, 1.5)
        self.cam.lookAt(0.5, 0, 1.5)

        self.light = DirectionalLight('dlight')
        self.light.setColor(VBase4(0.3, 0.28, 0.26, 1.0))
        self.lightNP = self.stage.attachNewNode(self.light)
        self.lightNP.setHpr(-75, -45, 0)
        self.stage.setLight(self.lightNP)

        self.amblight = AmbientLight('amblight')
        self.amblight.setColor(VBase4(0.7, 0.68, 0.66, 1.0))
        self.amblightNP = self.stage.attachNewNode(self.amblight)
        self.stage.setLight(self.amblightNP)

        self.accept('w', self.active_char.begin_forward)
        self.accept('a', self.active_char.begin_left)
        self.accept('s', self.active_char.begin_backward)
        self.accept('d', self.active_char.begin_right)
        self.accept('w-up', self.active_char.end_forward)
        self.accept('a-up', self.active_char.end_left)
        self.accept('s-up', self.active_char.end_backward)
        self.accept('d-up', self.active_char.end_right)
        self.taskMgr.add(self.active_char.MoveTask, 'MoveTask')

        self.look = False
        self.prev_pos = None
        self.accept('mouse2', self.begin_look)
        self.accept('mouse2-up', self.end_look)
        self.accept('mouse3', self.active_char.begin_spin)
        self.accept('mouse3-up', self.active_char.end_spin)
        self.taskMgr.add(self.MouseTask, 'MouseTask')

        self.floor_handler = CollisionHandlerFloor()
        self.floor_handler.addCollider(self.active_char.actor_from_floor,
                                       self.char_rig)
        self.wall_handler = CollisionHandlerPusher()
        self.wall_handler.addCollider(self.active_char.actor_from_obstacle,
                                      self.char_rig)
        self.zone_handler = CollisionHandlerEvent()
        self.zone_handler.addInPattern('%fn-into')
        self.zone_handler.addOutPattern('%fn-out')

        def foo(entry):
            print 'You are in the zone'

        def bar(entry):
            print 'You are not in the zone'

        self.accept('blockchar_zone-into', foo)
        self.accept('blockchar_zone-out', bar)
        self.cTrav = CollisionTraverser('main traverser')
        self.cTrav.setRespectPrevTransform(True)
        self.cTrav.addCollider(self.active_char.actor_from_floor,
                               self.floor_handler)
        self.cTrav.addCollider(self.active_char.actor_from_obstacle,
                               self.wall_handler)
        self.cTrav.addCollider(self.active_char.actor_from_zone,
                               self.zone_handler)
        #self.cTrav.showCollisions(self.stage)

    def create_stanchions(self):
        from pandac.PandaModules import GeomVertexReader, CollisionNode, CollisionTube
        self.stanchions = self.stage.findAllMatches('**/=Stanchion')
        for stanchion in self.stanchions:
            geomnode = stanchion.node()
            radius = float(stanchion.getTag('Stanchion'))
            geom = geomnode.getGeom(0)
            vdata = geom.getVertexData()
            for gp in range(geom.getNumPrimitives()):
                vreader = GeomVertexReader(vdata, 'vertex')
                prim = geom.getPrimitive(gp)
                prim = prim.decompose()
                for p in range(prim.getNumPrimitives()):
                    start = prim.getPrimitiveStart(p)
                    end = prim.getPrimitiveEnd(p)
                    vertices = []
                    for v in range(start, end):
                        vi = prim.getVertex(v)
                        vreader.setRow(vi)
                        vertex = vreader.getData3f()
                        vertices.append(vertex)
                    vertices.append(vertices[0])
                    for i in range(1, len(vertices)):
                        a, b = vertices[i - 1], vertices[i]
                        stanchion_np = stanchion.attachNewNode(
                            CollisionNode('stanchion'))
                        print 'creating cyl with radius %f from %s to %s' % (
                            radius, a, b)
                        stanchion_np.node().addSolid(
                            CollisionTube(a[0], a[1], a[2], b[0], b[1], b[2],
                                          radius))
                        stanchion_np.node().setFromCollideMask(OBSTACLE_MASK)
            geomnode.removeAllGeoms()

    def begin_look(self):
        self.look = True

    def end_look(self):
        self.look = False
        self.prev_pos = None

    def MouseTask(self, task):
        if self.mouseWatcherNode.hasMouse():
            (x, y) = self.mouseWatcherNode.getMouse()
            if self.prev_pos:
                if self.look or self.active_char.spinning:
                    h_diff = (x - self.prev_pos[0]) * 180
                    p_diff = (y - self.prev_pos[1]) * 90
                    new_h = clamp_deg_sign(self.char_rig.getH() - h_diff)
                    self.char_rig.setH(new_h)
                    self.cam.setP(self.cam.getP() + p_diff)
                    self.active_char.spin(new_h)
            self.prev_pos = (x, y)
        return task.cont
Пример #4
0
    def __init__(self):
        from pandac.PandaModules import CollisionHandlerFloor, CollisionHandlerPusher, CollisionHandlerEvent, CollisionTraverser
        from pandac.PandaModules import DirectionalLight, AmbientLight, VBase4
        ShowBase.__init__(self)

        self.sky = self.loader.loadModel('models/sky-sphere')
        self.sky.reparentTo(self.render)
        self.stage = self.loader.loadModel('models/test-collide')
        self.stage.reparentTo(self.render)
        self.floor = self.stage.findAllMatches('**/=CollideType=floor')
        self.floor.setCollideMask(FLOOR_MASK)
        self.obstacles = self.stage.findAllMatches('**/=CollideType=obstacle')
        if self.obstacles:
            self.obstacles.setCollideMask(OBSTACLE_MASK)
        self.zones = self.stage.findAllMatches('**/=CollideType=zone')
        if self.zones:
            self.zones.setCollideMask(ZONE_MASK)
        self.create_stanchions()

        # Character rig, which allows camera to follow character
        self.char_rig = self.stage.attachNewNode('char_rig')

        self.active_char = Character('mainchar', self.char_rig)

        self.cam.reparentTo(self.char_rig)
        self.cam.setPos(0.5, -3, 1.5)
        self.cam.lookAt(0.5, 0, 1.5)

        self.light = DirectionalLight('dlight')
        self.light.setColor(VBase4(0.3, 0.28, 0.26, 1.0))
        self.lightNP = self.stage.attachNewNode(self.light)
        self.lightNP.setHpr(-75, -45, 0)
        self.stage.setLight(self.lightNP)

        self.amblight = AmbientLight('amblight')
        self.amblight.setColor(VBase4(0.7, 0.68, 0.66, 1.0))
        self.amblightNP = self.stage.attachNewNode(self.amblight)
        self.stage.setLight(self.amblightNP)

        self.accept('w', self.active_char.begin_forward)
        self.accept('a', self.active_char.begin_left)
        self.accept('s', self.active_char.begin_backward)
        self.accept('d', self.active_char.begin_right)
        self.accept('w-up', self.active_char.end_forward)
        self.accept('a-up', self.active_char.end_left)
        self.accept('s-up', self.active_char.end_backward)
        self.accept('d-up', self.active_char.end_right)
        self.taskMgr.add(self.active_char.MoveTask, 'MoveTask')

        self.look = False
        self.prev_pos = None
        self.accept('mouse2', self.begin_look)
        self.accept('mouse2-up', self.end_look)
        self.accept('mouse3', self.active_char.begin_spin)
        self.accept('mouse3-up', self.active_char.end_spin)
        self.taskMgr.add(self.MouseTask, 'MouseTask')

        self.floor_handler = CollisionHandlerFloor()
        self.floor_handler.addCollider(self.active_char.actor_from_floor, self.char_rig)
        self.wall_handler = CollisionHandlerPusher()
        self.wall_handler.addCollider(self.active_char.actor_from_obstacle, self.char_rig)
        self.zone_handler = CollisionHandlerEvent()
        self.zone_handler.addInPattern('%fn-into')
        self.zone_handler.addOutPattern('%fn-out')
        def foo(entry):
            print 'You are in the zone'
        def bar(entry):
            print 'You are not in the zone'
        self.accept('blockchar_zone-into', foo)
        self.accept('blockchar_zone-out', bar)
        self.cTrav = CollisionTraverser('main traverser')
        self.cTrav.setRespectPrevTransform(True)
        self.cTrav.addCollider(self.active_char.actor_from_floor, self.floor_handler)
        self.cTrav.addCollider(self.active_char.actor_from_obstacle, self.wall_handler)
        self.cTrav.addCollider(self.active_char.actor_from_zone, self.zone_handler)
Пример #5
0
class FreeBLiTZ(ShowBase):

    def __init__(self):
        from pandac.PandaModules import CollisionHandlerFloor, CollisionHandlerPusher, CollisionHandlerEvent, CollisionTraverser
        from pandac.PandaModules import DirectionalLight, AmbientLight, VBase4
        ShowBase.__init__(self)

        self.sky = self.loader.loadModel('models/sky-sphere')
        self.sky.reparentTo(self.render)
        self.stage = self.loader.loadModel('models/test-collide')
        self.stage.reparentTo(self.render)
        self.floor = self.stage.findAllMatches('**/=CollideType=floor')
        self.floor.setCollideMask(FLOOR_MASK)
        self.obstacles = self.stage.findAllMatches('**/=CollideType=obstacle')
        if self.obstacles:
            self.obstacles.setCollideMask(OBSTACLE_MASK)
        self.zones = self.stage.findAllMatches('**/=CollideType=zone')
        if self.zones:
            self.zones.setCollideMask(ZONE_MASK)
        self.create_stanchions()

        # Character rig, which allows camera to follow character
        self.char_rig = self.stage.attachNewNode('char_rig')

        self.active_char = Character('mainchar', self.char_rig)

        self.cam.reparentTo(self.char_rig)
        self.cam.setPos(0.5, -3, 1.5)
        self.cam.lookAt(0.5, 0, 1.5)

        self.light = DirectionalLight('dlight')
        self.light.setColor(VBase4(0.3, 0.28, 0.26, 1.0))
        self.lightNP = self.stage.attachNewNode(self.light)
        self.lightNP.setHpr(-75, -45, 0)
        self.stage.setLight(self.lightNP)

        self.amblight = AmbientLight('amblight')
        self.amblight.setColor(VBase4(0.7, 0.68, 0.66, 1.0))
        self.amblightNP = self.stage.attachNewNode(self.amblight)
        self.stage.setLight(self.amblightNP)

        self.accept('w', self.active_char.begin_forward)
        self.accept('a', self.active_char.begin_left)
        self.accept('s', self.active_char.begin_backward)
        self.accept('d', self.active_char.begin_right)
        self.accept('w-up', self.active_char.end_forward)
        self.accept('a-up', self.active_char.end_left)
        self.accept('s-up', self.active_char.end_backward)
        self.accept('d-up', self.active_char.end_right)
        self.taskMgr.add(self.active_char.MoveTask, 'MoveTask')

        self.look = False
        self.prev_pos = None
        self.accept('mouse2', self.begin_look)
        self.accept('mouse2-up', self.end_look)
        self.accept('mouse3', self.active_char.begin_spin)
        self.accept('mouse3-up', self.active_char.end_spin)
        self.taskMgr.add(self.MouseTask, 'MouseTask')

        self.floor_handler = CollisionHandlerFloor()
        self.floor_handler.addCollider(self.active_char.actor_from_floor, self.char_rig)
        self.wall_handler = CollisionHandlerPusher()
        self.wall_handler.addCollider(self.active_char.actor_from_obstacle, self.char_rig)
        self.zone_handler = CollisionHandlerEvent()
        self.zone_handler.addInPattern('%fn-into')
        self.zone_handler.addOutPattern('%fn-out')
        def foo(entry):
            print 'You are in the zone'
        def bar(entry):
            print 'You are not in the zone'
        self.accept('blockchar_zone-into', foo)
        self.accept('blockchar_zone-out', bar)
        self.cTrav = CollisionTraverser('main traverser')
        self.cTrav.setRespectPrevTransform(True)
        self.cTrav.addCollider(self.active_char.actor_from_floor, self.floor_handler)
        self.cTrav.addCollider(self.active_char.actor_from_obstacle, self.wall_handler)
        self.cTrav.addCollider(self.active_char.actor_from_zone, self.zone_handler)
        #self.cTrav.showCollisions(self.stage)

    def create_stanchions(self):
        from pandac.PandaModules import GeomVertexReader, CollisionNode, CollisionTube
        self.stanchions = self.stage.findAllMatches('**/=Stanchion')
        for stanchion in self.stanchions:
            geomnode = stanchion.node()
            radius = float(stanchion.getTag('Stanchion'))
            geom = geomnode.getGeom(0)
            vdata = geom.getVertexData()
            for gp in range(geom.getNumPrimitives()):
                vreader = GeomVertexReader(vdata, 'vertex')
                prim = geom.getPrimitive(gp)
                prim = prim.decompose()
                for p in range(prim.getNumPrimitives()):
                    start = prim.getPrimitiveStart(p)
                    end = prim.getPrimitiveEnd(p)
                    vertices = []
                    for v in range(start, end):
                        vi = prim.getVertex(v)
                        vreader.setRow(vi)
                        vertex = vreader.getData3f()
                        vertices.append(vertex)
                    vertices.append(vertices[0])
                    for i in range(1, len(vertices)):
                        a, b =  vertices[i-1], vertices[i]
                        stanchion_np = stanchion.attachNewNode(CollisionNode('stanchion'))
                        print 'creating cyl with radius %f from %s to %s' % (radius, a, b)
                        stanchion_np.node().addSolid(CollisionTube(a[0], a[1], a[2], b[0], b[1], b[2], radius))
                        stanchion_np.node().setFromCollideMask(OBSTACLE_MASK)
            geomnode.removeAllGeoms()

    def begin_look(self):
        self.look = True

    def end_look(self):
        self.look = False
        self.prev_pos = None

    def MouseTask(self, task):
        if self.mouseWatcherNode.hasMouse():
            (x, y) = self.mouseWatcherNode.getMouse()
            if self.prev_pos:
                if self.look or self.active_char.spinning:
                    h_diff = (x - self.prev_pos[0]) * 180
                    p_diff = (y - self.prev_pos[1]) * 90
                    new_h = clamp_deg_sign(self.char_rig.getH() - h_diff)
                    self.char_rig.setH(new_h)
                    self.cam.setP(self.cam.getP() + p_diff)
                    self.active_char.spin(new_h)
            self.prev_pos = (x, y)
        return task.cont
Пример #6
0
    "a minimal sample to show how to keep an avatar grounded and blocked by invisible walls"
)
snipstuff.info.append("WASD=move the avatar around\nSPACE=avatar hiccup")
snipstuff.info_show()

#=========================================================================
# Main
"""
Starting from step1, we just put an additional collision handler to take care to keep the avatar grounded. This will PUSH the avatar back as soon as hit geometry we settled to be a wall: in blender we modelled polygons to wrap around the little house and all around the terrain area so that this time the avatar, differently from step1, won't pass through the house and won't be able to leave the terrain perimeter anymore. I suggest you to open the blender source to find out what I'm talking about here.
"""
#=========================================================================

#** Collision system ignition
base.cTrav = CollisionTraverser()
# did you saw this stuff in step1?
floorHandler = CollisionHandlerFloor()
floorHandler.setMaxVelocity(14)
# here it is the new fella - this will take care to push the avatar off the walls
wallHandler = CollisionHandlerPusher()

#** As you know this mask is used to mark the geometries for the floor collisions...
FLOOR_MASK = BitMask32.bit(1)
#... and this time we need another one to mark the walls as well.
WALL_MASK = BitMask32.bit(2)

#** This is our steering avatar - this time we use a little different setup, more close to real applications: we wrap either the avatar and its collision ray into another nodepath. This way we add lotta flexibility allowing us to make fancy things like you'll see below, to make the avatar rolling while steering, a thing not possible before and also to get rid of the global floorHandler.setOffset(1.0) shift, to set our avatar precisly placed above the surface.
avatarNP = NodePath('smileyNP')
avatarNP.reparentTo(base.render)
avatar = loader.loadModel('smiley')
avatar.reparentTo(avatarNP)
# since our avatar origin is centered in a model sized 2,2,2, we need to shift it 1 unit above the ground and this time we make this happen shifting it off its own root node (avatarNP)
Пример #7
0
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import CollisionHandlerEvent, CollisionHandlerQueue, CollisionHandlerFloor, CollisionHandlerPusher, CollisionNode, CollisionTube, CollisionSphere, CollisionRay, CollisionTraverser, BitMask32
from doomsday.base import Globals, SoundBank
from doomsday.world.Splat import Splat
from panda3d.core import Vec4, Vec3
import math

collHdl = CollisionHandlerEvent()
collHdlF = CollisionHandlerFloor()
collHdlF.setMaxVelocity(8)
collHdlPush = CollisionHandlerPusher()


class WorldCollisions(DirectObject):
    def __init__(self):
        base.cTrav = CollisionTraverser()
        self.called = False

    def handleInCollisions(self, entry):
        cogCollNames = ['coll_body', 'coll_crit']
        intoNode = entry.getIntoNodePath()
        fromNode = entry.getFromNodePath()
        if (self.called):
            return
        self.called = True
        splatColor = Vec4(0.7, 0.7, 0.8, 1.0)
        splatScale = 0.7
        if (intoNode.getName() in cogCollNames):
            cog = intoNode.getParent().getPythonTag('Cog')
            gag = fromNode.getParent().getPythonTag('Stats')
            splatColor = gag.getSplatColor()