it might be kind of a mess, but here it is (im new panda3d)
from direct.showbase.ShowBase import ShowBase
from panda3d.core import loadPrcFile, CollisionBox, CollisionNode, CollisionHandlerQueue, CollisionTraverser, CollisionHandlerPusher, NodePath, ClockObject, BitMask32,Vec3,WindowProperties,DirectionalLight,Material,AmbientLight,PerspectiveLens
from panda3d.physics import ActorNode, PhysicsManager, ForceNode, LinearVectorForce
from panda3d.bullet import BulletWorld, BulletPlaneShape, BulletRigidBodyNode, BulletBoxShape, BulletDebugNode, BulletCharacterControllerNode, BulletCapsuleShape, ZUp
from direct.showbase.InputStateGlobal import inputState
from direct.gui.OnscreenImage import OnscreenImage, TransparencyAttrib
loadPrcFile('settings.prc')
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.plane = self.loader.loadModel("plane.glb")
self.enableParticles()
self.object_z_speed = 0
taskMgr.add(self.update, 'update')
self.world = BulletWorld()
self.world.setGravity((0,0,-9.81))
plane_shape = BulletBoxShape((5, 5, 0.5))
brbnode = BulletRigidBodyNode('Ground')
self.worldNP = render.attachNewNode(brbnode)
brbnode.addShape(plane_shape)
plane_np = render.attachNewNode(brbnode)
plane_np.setPos(0, 0, 0)
self.plane.reparentTo(plane_np)
self.plane.setPos(0,0,0)
self.world.attachRigidBody(brbnode)
debugNode = BulletDebugNode('Debug')
debugNode.showWireframe(True)
debugNode.showConstraints(True)
debugNode.showBoundingBoxes(True)
debugNode.showNormals(True)
self.debugNP = render.attachNewNode(debugNode)
self.world.setDebugNode(self.debugNP.node())
height = 1.85
radius = 0.4
shape = BulletCapsuleShape(radius, height - 2*radius, ZUp)
self.playerNode = BulletCharacterControllerNode(shape, 0.4, 'Player')
self.playerNP = self.worldNP.attachNewNode(self.playerNode)
self.playerNP.setPos(-2, 0, 14)
self.playerNP.setCollideMask(BitMask32.allOn())
#self.camera.reparentTo(self.playerNP)
self.camera.setPos(0,0,0.75)
self.world.attachCharacter(self.playerNP.node())
cube_shape = BulletBoxShape((0.5, 0.5, 0.5))
cube_brbnode = BulletRigidBodyNode('Box')
cube_brbnode.setMass(1.0)
cube_brbnode.addShape(cube_shape)
self.cube_np = render.attachNewNode(cube_brbnode)
self.cube_np.setPos(0, 0, 5)
self.world.attachRigidBody(cube_brbnode)
self.cube = self.loader.loadModel("cube.glb")
self.cube.reparentTo(self.cube_np)
self.accept('f3', self.switchDebug)
self.accept('escape', self.releaseMouse)
self.accept('mouse1', self.captureMouse)
inputState.watchWithModifiers('forward', 'w')
inputState.watchWithModifiers('reverse', 's')
inputState.watchWithModifiers('left', 'a')
inputState.watchWithModifiers('right', 'd')
inputState.watchWithModifiers('jump', 'space')
self.setupCamera()
self.captureMouse()
self.playerH = 0
self.playerP = 0
self.mouseMultiplier = 0.1
self.setupSkybox()
self.setupLightning()
def captureMouse(self):
self.mouse_ctrl = True
md = self.win.getPointer(0)
self.lastMouseX = md.getX()
self.lastMouseY = md.getY()
properties = WindowProperties()
properties.setCursorHidden(True)
self.win.requestProperties(properties)
def processInput(self):
speed = Vec3(0, 0, 0)
omega = 0.0
if inputState.isSet('forward'): speed.setY( 3.0)
if inputState.isSet('reverse'): speed.setY(-3.0)
if inputState.isSet('left'): speed.setX(-3.0)
if inputState.isSet('right'): speed.setX( 3.0)
if inputState.isSet('jump'): self.doJump()
props = self.win.getProperties()
windowCenterX = int(props.getXSize() / 2)
windowCenterY = int(props.getYSize() / 2)
md = self.win.getPointer(0)
mouseX = md.getX()
mouseY = md.getY()
if self.mouse_ctrl:
self.win.movePointer(0,windowCenterX,windowCenterY)
self.playerH = -(mouseX-windowCenterX)*self.mouseMultiplier*75
self.playerP -= (mouseY-windowCenterY)*self.mouseMultiplier
if self.playerP < -90:
self.playerP = -90
elif self.playerP > 90:
self.playerP = 90
self.camera.setP(self.playerP)
omega = self.playerH
square_root = ((speed.getX()**2)+(speed.getY()**2))**0.5
if speed.getY() != 0 or speed.getX() != 0:
speed = Vec3(speed.getX()/square_root*3,speed.getY()/square_root*3,0)
self.playerNode.setAngularMovement(omega)
self.playerNode.setLinearMovement(speed, True)
def setupLightning(self):
sun = DirectionalLight('sun')
sun.setColor((1,1,1,1))
sun.setShadowCaster(True,512,512)
sun.getLens().set_film_size(100)
sun.getLens().set_near_far(1,100)
sun.showFrustum()
alight = AmbientLight('shadow')
alight.setColor((0.1,0.1,0.1,1))
alnp = render.attachNewNode(alight)
render.setLight(alnp)
dlnp = render.attachNewNode(sun)
dlnp.setHpr(45,-45,0)
dlnp.setPos(30,-30,30)
render.setLight(dlnp)
render.setShaderAuto()
def setupSkybox(self):
skybox = loader.loadModel('skybox.glb')
skybox.setScale(500)
skybox.setBin('background',0)
skybox.setDepthWrite(0)
skybox.setLightOff()
skybox.reparentTo(self.playerNP)
def doJump(self):
self.playerNode.setJumpSpeed(5)
self.playerNode.doJump()
def setupCamera(self):
#self.disableMouse()
self.camLens.setFov(90)
self.camLens.setNear(0.1)
crosshair = OnscreenImage(image = 'crosshair.png', pos = (0, 0, 0), scale = 0.03)
crosshair.setTransparency(TransparencyAttrib.MAlpha)
def move(self,dir):
self.dir = dir
def update(self, task):
dt = globalClock.getDt()
self.world.doPhysics(dt)
self.processInput()
return task.cont
def switchDebug(self):
if self.debugNP.isHidden():
self.debugNP.show()
self.setFrameRateMeter(True)
else:
self.debugNP.hide()
self.setFrameRateMeter(False)
def releaseMouse(self):
self.mouse_ctrl = False
properties = WindowProperties()
properties.setCursorHidden(False)
self.win.requestProperties(properties)
app = MyApp()
app.run()