I have a car and a track.
I would like my car to always be positioned above the trak.
How do I do this using a ray ?
Please post code if possible.
I have a car and a track.
I would like my car to always be positioned above the trak.
How do I do this using a ray ?
Please post code if possible.
Same code you’d use for a car going uphill
Here is some quick code, its a bit more complicated then your previous questions so the best way is to read it through and understand why I did what, then write your own version that suits your need.
This atleast handles the collision part, I guess you’d have to adjust the cars hpr in there aswell.
Edit: I’m not a mod here, but I think these questions fit in the “scripting issues” forum, not “general discussion”.
class World(DirectObject):
def __init__(self):
self.loadWorld()
self.gameTask = taskMgr.add(self.gameLoop, "gameLoop")
self.gameTask.last = 0
def gameLoop(self, task):
dt = task.time - task.last
task.last = task.time
#If dt is large, then there has been a hiccup that could cause the player
#to leave the field if this functions runs, so ignore the frame
if dt > .2: return Task.cont
# get the collision entries, and act accordingly
for i in range(self.cHandler.getNumEntries()):
entry = self.cHandler.getEntry(i)
name = entry.getIntoNode().getName()
if name == "Terrain": self.groundCollideHandler(entry)
def groundCollideHandler(self, colEntry):
#Set the player to the appropriate Z value for it to be exactly on the ground
newZ = colEntry.getSurfacePoint(render).getZ()
if newZ - self.player.getZ() > 2:
self.player.bump()
else:
self.player.setZ(newZ+.4)
def loadWorld(self):
# load visible geometry
world = loader.loadModel("models/leafBay")
world.setScale(0.12)
world.reparentTo(render)
# load unvisible collision geometry
solids = loader.loadModel("models/leafBay_coll")
solids.setScale(0.12)
solids.reparentTo(render)
# find collision group "Terrain" and set collideMask bit 1
terrain = solids.find("**/Terrain")
terrain.node().setIntoCollideMask(BitMask32.bit(1))
#################################
## setup collisions
##
# create the collision ray
ray = CollisionRay()
ray.setOrigin(0,0,50)
ray.setDirection(0,0,-1)
# set bitmask and stuff
rayNode = CollisionNode('groundRay')
rayNode.addSolid(ray)
rayNode.setFromCollideMask(BitMask32.bit(1))
rayNode.setIntoCollideMask(BitMask32.allOff())
# attach the rayNP to the player
actor = self.player.getActor()
rayNP = actor.attachNewNode(rayNode)
# uncomment next line to view the ray
#rayNP.show()
# add collision nodes to a collision handler
self.cTrav = CollisionTraverser()
self.cHandler = CollisionHandlerQueue()
self.cTrav.addCollider(rayNP.node(),self.cHandler)
# uncomment next line to show collisions
#self.cTrav.showCollisions(render)
base.cTrav = self.cTrav