Panda3D学习 (7):查找

离实习开始也只剩三天了,抓紧再多看一些例程了

选的这个是查找与获取,一听这名字就感觉应该是用途非常广泛的内容。


1.键盘交互事件

之前接触到,但没有深入了解,这里结合手册进行了研究,主要有这几类:

1. Keys that type a character are named that character. It is always lowercase, even when shift or caps lock is pressed. (Shift and other modifiers are explained below.)
e.g.

 "a", "b", "3", "[", etc.

无论大小写与否,都是按照小写字母来的(其他同理) 

但事实上我真正跑起来的时候有点问题 (等下再说)

2. The key down event is named for the key. 按下这个动作命名了这个事件。

3. As of 1.3.0 The keyboard autorepeat is named for the key + "-repeat" e.g.

 "a-repeat", "2-repeat", "[-repeat"

repeat代表长按

4. The key up event is named for the key + "-up" e.g.

 "a-up", "2-up", "[-up"

up代表放开。

5. All key events (including "-up") have a corresponding time event labeled
  

"time-" + <key name>

暂时不知道是干嘛的。 (我将程序中的“a"改为"time-a"之后,发现按一下a整个程序就退出了)

原例程中的键盘事件为“1”“2”“3”“4”,结果我发现按了之后毫无反应,但如果改为“a""b"这些字母后就可以正常运行。我将”1“改为”raw-1“后,按1则又可以正常运行了。

因此我觉得,这个可能是我键盘和操作系统的键位不对应造成程序读入错误。


2. controlJoint

这是之前从未碰到过的一个新概念,这里的joint应该指的是关节的意思(名词),而不是共同的意思(形容词)。

https://www.panda3d.org/manual/index.php/Actor_Manipulations

这是目录中没有出现的,但是在我搜索controlJoint的时候找到了

NodePath = ModelNodePath.controlJoint(None,'模型节点','关节名称')

这个模型节点的名称一般为modelroot,应该具体和egg文件有关,例程里也是这样的,就不深究了。

关节名称 例程里是Neck (颈部) 和 RightHand(右手), 因为后续也没有调用过,而我随机修改后程序就会异常,因此我估计是egg文件种定义好的内容。


3. 鼠标相关操作

之前接触的一直都是键盘交互,这个例程涉及到了鼠标的相关交互

if base.mouseWatcherNode.hasMouse():
  x=base.mouseWatcherNode.getMouseX()

  y=base.mouseWatcherNode.getMouseY()

主要是通过一个base类下叫做mouseWatcherNode的已有对象来获取鼠标相关信息

self.eveNeck.setP(clamp(mpos.getX()) * 90)

            self.eveNeck.setH(clamp(mpos.getY()) * 50)

再用刚暴露出的eveNeck节点(可以认为是控制人物颈部)来set P和H值 (与鼠标的x,y值相关)来获得人一直望向鼠标的效果。

为了使得该效果一直保持,使用了任务的形式(每帧都会执行),然后用return Task.cont 。


4.切换手中握的东西

        for row in positions:
            np = loader.loadModel(row[0])  # Load the model
            np.setPos(row[1][0], row[1][1], row[1][2])  # Position it
            np.setHpr(row[2][0], row[2][1], row[2][2])  # Rotate it
            np.setScale(row[3])  # Scale it
            # Reparent the model to the exposed joint. That way when the joint moves,
            # the model we just loaded will move with it.
            np.reparentTo(self.rightHand)
            self.models.append(np)  # Add it to our models list


        self.switchObject(0)  # Make object 0 the first shown

        self.setupLights()

这里使用的方法是先通过self.rightHand = self.eve.exposeJoint(None, 'modelRoot', 'RightHand')

获得右手的节点,然后将4种模型全部通过reparent加到该节点下,

def switchObject(self, i):
    for np in self.models:
        np.hide()

    self.models[i].show()

隐藏所有其余模型,只show一个的方式,来获得切换的效果。


综上所述,这个例程主要是通过controlJoint的方式,来控制整个model中单独的一块,不过至今都还没有研究过egg文件的内容,不知道具体是怎么样的呢。

猜你喜欢

转载自blog.csdn.net/zanbaixi2128/article/details/80856290