Hi, I am very new to Panda and I have been trying to make player movement. I have made a very very simple block of code that moves my character on the world axis, but though I have my camera parented to my character model, when it rotates on the heading the camera does not fully circle and change heading along with my character, of course, I needed a script for that.
So me at 10 pm slapped the tutorial camera rotate with this math which I am not familiar with and changed some variables to connect it with my playerâs heading and It is not working.
The camera does rotate around the player perfectly fine in position, but though I am setting the heading the same as the player, the player is still turning on its own speed.
How can I fix the mess I have made?
from direct.showbase.ShowBase import ShowBase
from panda3d.core import loadPrcFileData
from panda3d.core import NodePath
from direct.actor.Actor import Actor
from direct.task import Task
from panda3d.core import LPoint3f # Note: LPoint3f is like a vector 3 value.
from math import pi, sin, cos
# My Config Variables
winTitle = "Max's Panda 3D Program"
iconPath = "pandaIcon.ico"
fov = 50
frameMeter = True
winWidth = 720
winLength = 1280
fullscreen = False
syncVideo = True
sceneGraphAnalyzer = True
red = 135
green = 168 # RGB Values
blue = 222
red = red / 255
green = green / 255 # Convert to background-color doubles
blue = blue / 255
gameConfiguration = f"""
win-size {winLength} {winWidth}
window-title {winTitle}
show-frame-rate-meter {frameMeter}
background-color {red} {green} {blue} 0
default-fov {fov}
fullscreen {fullscreen}
icon-filename {iconPath}
show-scene-graph-analyzer-meter {sceneGraphAnalyzer}
sync-video {syncVideo}
"""
loadPrcFileData("", gameConfiguration)
# Key bind dictionary
keyMap = {
"forward": False,
"backward": False,
"left": False,
"right": False
}
# Update key state.
def updateKeyMap(key, state):
keyMap[key] = state
class Main(ShowBase): # Class created, inherits ShowBase.
def __init__(self):
ShowBase.__init__(self) # Initialize ShowBase.
self.disable_mouse() # Disables default Panda camera controls.
environmentNode = NodePath("Environment") # Creates Panda Node in scene graph.
scene = self.loader.loadModel("phase_8/models/neighborhoods/the_burrrgh.bam")
self.skybox = self.loader.loadModel("phase_3.5/models/props/TT_sky.bam")
scene.reparentTo(environmentNode) # Parents scene inside 'Environment' node.
self.skybox.reparentTo(environmentNode)
environmentNode.reparentTo(self.render) # Reparents node and children to 'render' (Scene Graph root).
self.plrCharacter = Actor("phase_6/models/char/dale_1000.bam",
{"idleAnim":"phase_6/models/char/dale_idle.bam",
"walkAnim":"phase_6/models/char/dale_walk.bam"})
self.plrCharacter.setScale(3.5)
self.plrCharacter.reparentTo(self.render)
self.plrCharacter.loop("idleAnim")
self.plrCharacter.setPos(20, 0, 4)
self.camera.reparentTo(self.plrCharacter)
self.camera.setPos(self.plrCharacter.get_pos())
self.accept("a", updateKeyMap, ["left", True])
self.accept("a-up", updateKeyMap, ["left", False])
self.accept("d", updateKeyMap, ["right", True])
self.accept("d-up", updateKeyMap, ["right", False])
self.accept("w", updateKeyMap, ["forward", True])
self.accept("w-up", updateKeyMap, ["forward", False])
self.accept("s", updateKeyMap, ["backward", True])
self.accept("s-up", updateKeyMap, ["backward", False])
self.task_mgr.add(self.skyboxRotate, "SkyboxRotate")
self.task_mgr.add(self.playerMovement, "PlayerMovement")
def skyboxRotate(self, task):
deltaTime = globalClock.getDt()
self.skybox.setHpr((self.skybox.get_hpr() + LPoint3f((2 * deltaTime), 0, 0)))
return Task.cont
def playerMovement(self, task):
deltaTime = globalClock.getDt()
if keyMap["left"]:
heading = self.plrCharacter.getH()
heading += (50 * deltaTime)
self.plrCharacter.setH(heading)
self.camera.setHpr(self.plrCharacter.get_h(), -7, 0)
angleDegrees = self.plrCharacter.get_h()
angleRadians = angleDegrees * (pi / 180.0)
self.camera.setPos(5 * sin(angleRadians), -5 * cos(angleRadians), 1.7)
if keyMap["right"]:
heading = self.plrCharacter.getH()
heading -= (50 * deltaTime)
self.plrCharacter.setH(heading)
self.camera.setHpr(self.plrCharacter.get_h(), -7, 0)
angleDegrees = self.plrCharacter.get_h()
angleRadians = angleDegrees * (pi / 180.0)
self.camera.setPos(5 * sin(angleRadians), -5 * cos(angleRadians), 1.7)
if keyMap["forward"]:
position = self.plrCharacter.getPos()
position.y += (15 * deltaTime)
self.plrCharacter.setPos(position)
if keyMap["backward"]:
position = self.plrCharacter.getPos()
position.y -= (15 * deltaTime)
self.plrCharacter.setPos(position)
return Task.cont
game = Main()
game.run()