Can somebody point me towards an example that has GeoMIP Terrain working with collisions? I’m getting really lost in trying to find a way to make this work.
Right now I’ve got a model that can walk around, and I added a CollisionRay to it, but I can’t figure out how to make it interact with the GeoMIP terrain I have.
The only thread with anything useful I’ve been able to find it panda3d.org/forums/viewtopi … sc&start=0, which helped me with a few issues, but I can’t seem to get it to notice collisions with the ground.
Here’s the code:
import direct.directbase.DirectStart
import sys, os, math, random
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from pandac.PandaModules import Vec3, Vec4, BitMask32
from panda3d.core import CollisionTraverser, CollisionNode, CollisionHandlerQueue, CollisionRay
from direct.actor.Actor import Actor
from direct.gui.DirectGui import DirectFrame
from direct.gui.OnscreenText import OnscreenText
class Game(DirectObject):
def __init__(self):
self.cTrav = CollisionTraverser()
self.cTrav.showCollisions(render)
self.loadTerrain()
self.debugConsole()
self.loadModel()
self.direction = "Stopped"
self.facingDirection = self.model.getH( )
self.turningDirection = 0;
taskMgr.add( self.movingTask, 'movingTask' )
taskMgr.add( self.turningTask, 'turningTask' )
self.accept('w', self.setMovingDirection, ["Forward"])
self.accept('w-up', self.setMovingDirection, ["Stopped"])
self.accept('s', self.setMovingDirection, ["Backward"])
self.accept('s-up', self.setMovingDirection, ["Stopped"])
self.accept('a', self.setTurningDirection, [1])
self.accept('a-up', self.setTurningDirection, [0])
self.accept('d', self.setTurningDirection, [-1])
self.accept('d-up', self.setTurningDirection, [0])
self.accept('space', self.jump)
def jump(self):
print "Jump"
self.model.setZ(self.model, 100)
def setMovingDirection(self, dir):
self.direction = dir
if dir == "Forward":
self.modelWalk.resume( )
if dir == "Backward":
self.modelWalk.resume( )
if dir == "Stopped":
self.modelWalk.pause( )
def setTurningDirection(self, dir):
self.turningDirection = dir
def turningTask(self, task):
self.model.setH( self.facingDirection + self.turningDirection )
self.facingDirection = self.model.getH( )
return task.cont
def movingTask(self, task):
speed = 0
if self.direction == "Forward":
speed = -5
if self.direction == "Backward":
speed = 5
startpos = self.model.getPos()
self.model.setY(self.model, speed)
#check for collisions
self.cTrav.traverse(render)
entries = []
for i in range(self.modelGroundHandler.getNumEntries()):
entry = self.modelGroundHandler.getEntry(i)
print entry
entries.append(entry)
self.modelGroundHandler.sortEntries()
if self.modelGroundHandler.getNumEntries() > 0:
entry = self.modelGroundHandler.getEntry(0)
print entry
return task.cont
def loadModel(self):
self.model = Actor( "tiny/tiny",
{"walk": "tiny/tiny"})
self.modelGroundRay = CollisionRay()
self.modelGroundRay.setOrigin(-65,55,115)
self.modelGroundRay.setDirection(0,0,-4)
self.modelGroundCol = CollisionNode('modelRay')
self.modelGroundCol.addSolid(self.modelGroundRay)
self.modelGroundCol.setFromCollideMask(BitMask32.bit(0))
self.modelGroundCol.setIntoCollideMask(BitMask32.allOff())
self.modelGroundColNp = self.model.attachNewNode(self.modelGroundCol)
self.modelGroundColNp.show()
self.modelGroundHandler = CollisionHandlerQueue()
self.cTrav.addCollider(self.modelGroundColNp, self.modelGroundHandler)
self.model.reparentTo( render )
self.model.setScale( .02 )
self.model.setH( 90 )
self.model.setPos(10.0, 55.0, 75.0)
base.disableMouse()
taskMgr.add( self.cameraFollowTask, 'cameraFollowTask')
self.modelWalk = self.model.actorInterval( "walk" )
self.modelWalk.loop( )
self.modelWalk.pause( )
def loadTerrain(self):
self.terrain = GeoMipTerrain("map1")
self.terrain.setHeightfield("maps/map2.png")
self.terrain.setColorMap("textures/grass.jpg")
self.terrain.setBlockSize(32)
self.terrain.setNear(40)
self.terrain.setFar(100)
self.terrain.setFocalPoint(base.camera)
self.root = self.terrain.getRoot()
self.root.reparentTo(render)
self.root.setSz(100)
self.terrain.generate()
taskMgr.add(self.updateTerrainTask, 'RegenTerrain')
def debugConsole(self):
self.textDebug = TextNode('DebugConsole')
self.textDebug.setFrameColor(0, 0, 0, 1)
self.textDebug.setCardColor(0, 0, 0, 1)
self.textDebug.setCardAsMargin(0.4, 0.4, 0.4, 0.4)
self.textDebug.setCardDecal(True)
self.textNodePath = aspect2d.attachNewNode(self.textDebug)
self.textNodePath.setScale(0.04)
self.textNodePath.reparentTo(base.a2dTopLeft)
self.textNodePath.setPos(0.02, 0, -0.04)
self.textDebug.setText("Initializing debug console...")
taskMgr.doMethodLater(1, self.updateDebugConsoleTask, 'RegenConsole')
def updateDebugConsoleTask(self, task):
fps = "FPS: " + str(round(globalClock.getAverageFrameRate(), 0)) + "\nCamera XYZ: " + str(camera.getPos( )) + "\nFAC: " + str(self.facingDirection)
self.textDebug.setText(fps)
task.delayTime = 1
return task.again
def updateTerrainTask(self, task):
self.terrain.update()
return task.cont
def cameraFollowTask( self, task ):
offset = Vec3( 0, 75, 40 )
desiredPos = self.model.getPos( ) + self.model.getQuat( ).xform( offset )
camera.setPos(desiredPos)
camera.lookAt(self.model)
return task.cont
Game = Game()
render.analyze()
run()
This is what I get when I run it and move around:
As you can see, it’s hitting the GeoMip terrain and so is the ray (the white line right beside him). But it’s not recording it as a collision at all.