Hi, I’m trying to make a small jumping system for my testing program. How can I give my model an upwards velocity so that it jumps? I have already successfully implemented gravity.
from direct.showbase.ShowBase import ShowBase
from direct.actor.Actor import Actor
from panda3d.core import loadPrcFileData
from panda3d.core import AmbientLight
from panda3d.core import VBase4
from panda3d.core import CollisionSphere
from panda3d.core import CollisionHandlerPusher
from panda3d.core import CollisionTraverser
from panda3d.core import CollisionNode
from panda3d.core import Vec3
loadPrcFileData("", "window-title Test Program")
loadPrcFileData("", "win-size 800 500")
class Test(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.z_speed = 0
self.gravity = -9.81
self.x = 0
self.y = 0
base.disableMouse()
base.setBackgroundColor(0, 0, 0)
light = AmbientLight("light")
light.setColor(VBase4(0.4, 0.4, 0.4, 1))
lightNP = self.render.attachNewNode(light)
self.render.setLight(lightNP)
env = self.loader.loadModel("models/egg/environment")
env.reparentTo(self.render)
env.setScale(0.25)
env.setPos(-8, 42, 0)
something = self.loader.loadModel("untitled")
something.reparentTo(self.render)
something.setPos(0, 10, 0)
self.panda = Actor("models/panda-model", {"walk": "models/panda-walk4"})
self.panda.reparentTo(self.render)
self.panda.setScale(0.005)
self.panda.setPos(0, 0, 50)
self.camera.reparentTo(self.panda)
self.camera.setY(3000)
self.camera.setZ(700)
self.camera.setH(180)
self.walk = {"forward": False, "backward": False, "left": False, "right": False, "jump": False}
self.accept("w", self.setKey, ["forward", True])
self.accept("s", self.setKey, ["backward", True])
self.accept("a", self.setKey, ["left", True])
self.accept("d", self.setKey, ["right", True])
self.accept("space", self.setKey, ["jump", True])
self.accept("w-up", self.setKey, ["forward", False])
self.accept("s-up", self.setKey, ["backward", False])
self.accept("a-up", self.setKey, ["left", False])
self.accept("d-up", self.setKey, ["right", False])
self.accept("space-up", self.setKey, ["jump", False])
colNode = CollisionNode("colNode")
colSphere = CollisionSphere(self.panda.getX(), self.panda.getY(), self.panda.getZ() + 300, 250)
colNode.addSolid(colSphere)
colNP = self.render.attachNewNode(colNode)
colNP.reparentTo(self.panda)
self.cTrav = CollisionTraverser()
self.cHan = CollisionHandlerPusher()
self.cHan.addCollider(colNP, self.panda)
self.cTrav.addCollider(colNP, self.cHan)
taskMgr.add(self.jumpPanda, "Jump Panda")
taskMgr.add(self.controlGravity, "Gravity Control")
taskMgr.add(self.movePanda, "Panda Control")
taskMgr.add(self.checkCollisions, "Collision Check")
def jumpPanda(self, task):
return task.cont
def checkCollisions(self, task):
self.cTrav.traverse(self.render)
return task.cont
def setKey(self, key, value):
self.walk[key] = value
def controlGravity(self, task):
dt = globalClock.getDt()
self.z_speed += self.gravity * dt
self.panda.setZ(max(self.panda.getZ() + self.z_speed * dt, 0))
return task.cont
def movePanda(self, task):
dt = globalClock.getDt()
if self.walk["forward"]:
self.panda.setY(self.panda, -1000 * dt)
if self.panda.getCurrentAnim() != "walk":
self.panda.setPlayRate(1, "walk")
self.panda.loop("walk")
if self.walk["backward"]:
self.panda.setY(self.panda, 1000 * dt)
if self.panda.getCurrentAnim() != "walk":
self.panda.setPlayRate(-1, "walk")
self.panda.loop("walk")
if not self.walk["forward"]:
if not self.walk["backward"]:
if self.panda.getCurrentAnim() == "walk":
self.panda.setPlayRate(0, "walk")
self.panda.pose("walk", 0)
self.panda.stop("walk")
if self.walk["left"]:
self.panda.setH(self.panda, 100 * dt)
if self.walk["right"]:
self.panda.setH(self.panda, -100 * dt)
return task.cont
t = Test()
t.run()
The code might be a bit messy, but it’s just a testing program. Also, I don’t know much into collision detection yet, so if I am doing something wrong with the collisions, please let me know so I can learn. If I walk into my custom created model, the panda tends to glitch around and it drives the camera crazy. Also, I punzipped the environment model and put it in a custom egg folder in the models folder, as you can see in the path when I load the environment model. I edited the egg file to add the following to it:
<Collide> { Polyset keep descend }
The only thing in the entire model that detects collision is one of the trees, and it detects the collision without any bugs (the panda doesn’t walk through it). I did the same with my custom created model, but the panda and camera teleports back and forth inside it (a little difficult to explain, but I can put it as: crazy). Thanks!