panda3d 键盘移动场景

Posted bcbobo21cn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了panda3d 键盘移动场景相关的知识,希望对你有一定的参考价值。

看一下panda3d示例Tut-Roaming-Ralph.py;

角色在场景中漫游;光标键控制人物;a、s键旋转场景;

pview看一下示例的模型,一个是人的模型,还有一个是场景;

它的代码比较多;除了场景漫游,还有碰撞检测;碰撞检测,就是人物碰到坡,打光标键自己会爬,碰到树,打前进光标键,人物应动不了;下回再整碰撞检测;

先看一下a和s键旋转场景;根据它的示例大体做一下,

import direct.directbase.DirectStart
from panda3d.core import CollisionTraverser,CollisionNode
from panda3d.core import CollisionHandlerQueue,CollisionRay
from panda3d.core import Filename,AmbientLight,DirectionalLight
from panda3d.core import PandaNode,NodePath,Camera,TextNode
from panda3d.core import Vec3,Vec4,BitMask32
from direct.gui.OnscreenText import OnscreenText
from direct.actor.Actor import Actor
from direct.showbase.DirectObject import DirectObject
import random, sys, os, math

class World(DirectObject):

    def __init__(self):

        self.keyMap = "left":0, "right":0, "forward":0, "cam-left":0, "cam-right":0
        base.win.setClearColor(Vec4(0,0,0,1))

        self.environ = loader.loadModel("models/world")      
        self.environ.reparentTo(render)
        self.environ.setPos(0,0,0)
        
        self.accept("a", self.setKey, ["cam-left",1])
        self.accept("s", self.setKey, ["cam-right",1])
       
        taskMgr.add(self.move,"moveTask")
        base.disableMouse()
       
        # Create some lighting
        ambientLight = AmbientLight("ambientLight")
        ambientLight.setColor(Vec4(.3, .3, .3, 1))
        directionalLight = DirectionalLight("directionalLight")
        directionalLight.setDirection(Vec3(-5, -5, -5))
        directionalLight.setColor(Vec4(1, 1, 1, 1))
        directionalLight.setSpecularColor(Vec4(1, 1, 1, 1))
        render.setLight(render.attachNewNode(ambientLight))
        render.setLight(render.attachNewNode(directionalLight))
    
    def setKey(self, key, value):
        self.keyMap[key] = value

    def move(self, task):
        if (self.keyMap["cam-left"]!=0):
            base.camera.setX(base.camera, -20 * globalClock.getDt())
        if (self.keyMap["cam-right"]!=0):
            base.camera.setX(base.camera, +20 * globalClock.getDt())
        return task.cont

w = World()
run()

 

    场景加载了,打a或s键,只有一下效果,场景一直在移动;下回再整; 

 self.keyMap = "left":0, "right":0, "forward":0, "cam-left":0, "cam-right":0

python的语法还不太熟悉,这个map数组看上去没有先声明,直接赋值就可以了;

以上是关于panda3d 键盘移动场景的主要内容,如果未能解决你的问题,请参考以下文章

初步了解Panda3D场景管理

panda3d场景的主要状态

初步了解panda3d灯光

unity场景导航和键盘快捷键

如何使用键盘移动对象?

PHP T9 移动键盘 [关闭]