I’m stuying the code here Lesson 6 | Panda3D Beginner’s Tutorial and the example code works nicely. The player with collision sphere is created and the sphere’s position updates as the player moves around.
When I used the code and adjusted to my use case sphere was created but it’s not attached
to the player which in my case is the camera object. I’m making FPS view.
from direct.showbase.ShowBase import ShowBase
from panda3d.core import (loadPrcFile, Point2, Point3, WindowProperties,
CollisionHandlerQueue, CollisionHandlerPusher,
CollisionNode, CollisionPolygon, CollisionTraverser,
CollisionSphere)
import sys
loadPrcFile('conf.prc')
class FloorPlan(ShowBase):
def __init__(self):
super().__init__()
self.cam.setPos(0, 0, -5)
#self.disableMouse()
props = WindowProperties()
props.setCursorHidden(True)
props.setMouseMode(WindowProperties.M_relative)
self.win.requestProperties(props)
self.path = self.render.attach_new_node("player")
base.cam.reparentTo(self.path)
base.cam.setZ(1.85)
base.camLens.set_fov(100, 100)
self.setBackgroundColor(0, 0, 0, 1)
self.house = self.loader.loadModel('./floorplan.glb')
self.house.setPos(0, 0, 0)
self.house.setH(self.house, 90)
self.house.reparentTo(self.render)
self.prev_mouse = Point2(0)
self.accept('q', self.quit_program)
self.cTrav = CollisionTraverser()
self.pusher = CollisionHandlerPusher()
cnodePath = self.camera.attachNewNode(CollisionNode('collisionNode'))
cnodePath.node().addSolid(CollisionSphere(self.camera.getPos(), 4.0))
queue = CollisionHandlerQueue()
cnodePath.show()
pusher = CollisionHandlerPusher()
pusher.addCollider(cnodePath, self.camera)
wallNode = CollisionNode('wall')
wallNode.addSolid(CollisionPolygon(Point3(0, -10, 0), Point3(5, -5, 0),
Point3(0, -5, 5), Point3(5, -5, 5)))
self.cTrav.addCollider(cnodePath, queue)
self.taskMgr.add(self.update_keys, 'update-keys')
def update_keys(self, task):
if base.mouseWatcherNode.has_mouse():
mouse = base.mouseWatcherNode.get_mouse()
button = getattr(base.mouseWatcherNode, 'is_raw_button_down', base.mouseWatcherNode.is_button_down)
delta = mouse - self.prev_mouse
self.prev_mouse = Point2(mouse)
self.path.setH(self.path, -delta.x * 15)
base.cam.set_p(base.cam, delta.y * 15)
base.cam.set_p(max(-85, min(base.cam.get_p(), 85)))
speed = (8 + int(button("lshift") * 4)) * base.clock.dt
self.path.setPos(self.path,
((int(button("d") - int(button("a"))) * speed)),
((int(button("w") - int(button("s"))) * speed)), 0)
return task.cont
def quit_program(self):
sys.exit(0)
game = FloorPlan()
game.run()
As you can see it’s pretty much the same code. Amended to fit my scene.
I admit I’m little frustrated when I see example code to learn from but it’s not working in my use case.
Your feedback is much appreciated.