I’ve been away from the forums, trying things out and I’ve made some good progress. I’m aiming to make a floor plan layout, and have a user (as the first person camera) walk from room to room. This is what I have for my code so far:
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
import sys
class World(DirectObject):
def __init__(self):
# disable default mouse controls
base.disableMouse()
# loads maze model
self.environmentModel = loader.loadModel("models/maze")
self.environmentModel.reparentTo(render)
self.environmentModel.setPos(-2, -4, 0)
base.setBackgroundColor(0, 0.3, 0.8)
# enable two sided rendering
self.environmentModel.setTwoSided(True)
# loads camera
self.cameraModel = loader.loadModel("models/camera")
self.cameraModel.reparentTo(render)
self.cameraModel.setPos(-1, -1, 1.5)
# Create Ambient Light
ambientLight = AmbientLight('ambientLight')
ambientLight.setColor(VBase4(.5, .5, .5, 1))
ambientLightNP = render.attachNewNode(ambientLight)
render.setLight(ambientLightNP)
# Directional light 1
directionalLight = DirectionalLight('directionalLight')
directionalLight.setColor(Vec4(0.1, 0.1, 0.1, 1))
directionalLightNP = render.attachNewNode(directionalLight)
directionalLightNP.setHpr(180, 0, 0)
render.setLight(directionalLightNP)
# Directional light 2
directionalLight = DirectionalLight('directionalLight')
directionalLight.setColor(Vec4(0.1, 0.1, 0.1, 1))
directionalLightNP = render.attachNewNode(directionalLight)
directionalLightNP.setHpr(0, 0, 0)
render.setLight(directionalLightNP)
base.camera.reparentTo(self.cameraModel)
self.keyMap = {"up" : False, "down" : False, "left" : False, "right" : False,}
self.accept("escape", sys.exit)
self.accept("arrow_up", self.setKey, ["up", True])
self.accept("arrow_down", self.setKey, ["down", True])
self.accept("arrow_left", self.setKey, ["left", True])
self.accept("arrow_right", self.setKey, ["right", True])
self.accept("arrow_up-up", self.setKey, ["up", False])
self.accept("arrow_down-up", self.setKey, ["down", False])
self.accept("arrow_left-up", self.setKey, ["left", False])
self.accept("arrow_right-up", self.setKey, ["right", False])
taskMgr.add(self.cameraControl, "Camera Control")
def setKey(self, key, value):
self.keyMap[key] = value
def cameraControl(self, task):
dt = globalClock.getDt()
if(dt > .20):
return task.cont
#Assigns key input to movement
#up = forward
#down = back
#left = turn left
#right = turn right
if(self.keyMap["up"] == True):
self.cameraModel.setY(self.cameraModel, 7 * dt)
return task.cont
elif(self.keyMap["down"] == True):
self.cameraModel.setY(self.cameraModel, -7 * dt)
return task.cont
elif(self.keyMap["left"] == True):
self.cameraModel.setH(self.cameraModel, 50 * dt)
return task.cont
elif(self.keyMap["right"] == True):
self.cameraModel.setH(self.cameraModel, -50 * dt)
return task.cont
else:
return task.cont
w = World()
run()
However, I have some things that I want to improve:
- I want to make the corners of the rooms more visible, as it can be hard to distinguish walls at times.
- I want to add collision detection, to not allow the camera to pass through.
This is the .egg of the “maze”