Move a 3d model in a terrain .bam file

OK, can anyone compile some code that moves the object in the terrain I need help with this, I don’t know panda3d and I’m going to make a Car game. and after showing the code can you show some code on how to do this but with animation in the file type of .obj .blend .fbx.Please #PYTHON

One of the sample-programs shows this! If you look at the “Roaming Ralph” sample, you should find code that moves a character over uneven terrain, and that animates the character in doing so.

(I will note that its approach to collisions with obstacles is not great, but you can perhaps look to other sources when you get to that point in your own project.)

[edit] In case you haven’t yet seen the sample-programs, you should find them amongst the links on the download page.

Wait you mean like it’s gonna ,say if ‘ralph’ is climbing a mountain he will look like he’s walking straight instead of tilting the body like he is climbing a mountain

I… don’t think that I addressed something along those lines, no.

What you’re describing there would likely be largely a matter of appropriate animations (whether hand-made, procedural, or a bit of both), I would think.

are you saying that it wont look like that

I believe that Roaming Ralph taking a very simple approach: Ralph stays vertical, regardless of slope, and has only one animation for walking, regardless of slope.

Now, you could change the code such that your character is tilted when climbing. But that won’t look like they’re climbing a mountain, because when a human climbs a mountain they don’t simply rotate their body–the way that they move changes, too. At the least their feet will be at a different angle relative to their legs, and I daresay that likely the movement of those legs will change, too, and perhaps other elements of their posture and movement, too. This, then, seems likely to be implemented via additional animations, or perhaps via procedural animation.

All that said… looking back, you indicated that you’re making a car-game. If your player-character is represented by a car, then additional animations are unlikely to be called for: cars don’t change the way that they move overmuch, I daresay!

Still, you might well want to add some code to adjust the vehicle’s angle on sloped surfaces, indeed.

i know but they will rotate a tiny bit

the approach of the car games is that if you go on this coordinate your 3d model will look like (which is the tilted thing) this

and what if you add some kind of border on some road or places will panda3d’s physics engine do the trick of tilting.

First, let me note that Panda has both a built-in collision system and a built-in physics system; while related and to some degree overlapping, they’re not quite the same, I believe. I’ll confess that while I’m familiar with the collision system, I’m not familiar with the physics system.

So, I’m not sure of what the physics system, specifically, does in such circumstances, I’m afraid.

The collision system, on the other hand, doesn’t automatically do what you ask, as far as I’m aware. However, you could write code that uses the collision system to so tilt your car, I do believe.

There are generally two ways to do physics on a character controller in Panda – the BulletVehicle and the BulletCharacterControllerNode.

The BulletVehicle is just what it sounds like – a way to simulate a vehicle’s suspension, tires, and body weight. Tilting can be simulated “automatically” in this manner, dependent on how you set your BulletVehicle suspension up.

As for the Bullet CCN, there are a number of methods you can set, such as set_max_slope() BulletCharacterControllerNode — Panda3D Manual

I imagine tilting with a first-person character would be largely a matter of custom camera handling, rather than something you’d need physics for.

In all fairness, a simpler simulation can be made, whether using Bullet or Panda’s built-in systems, I do believe.

Much depends, I think, on the detail that the simulation is intended to have.

its beyond me now, its more likely to use the coordinate system,

lets anyway go to the original topic------
say i convert my terrain in blender to .bam file how do i use a .blend model in it like moving it,EXAMPLE NOT WHAT I REALLY WANT TO SAY-- say like i have ‘roaming ralph’ but just that i changed the model to my own model, how do i use my terrain in it.
#PYTHON

thank ya

if you guys help i’ll thank you guys so much that even in the credits of the car game i might add your names and even a “made with python and panda3d”

(might not be true)

I’m not entirely sure that I understand what it is that you’re asking–are you asking how to use multiple model-files in a single game?

If so, then it’s simply a matter of loading multiple models and storing them in separate variables, I believe.

Your problem is that you want to make games, but you don’t want to study samples and read the documentation.

I changed the bullet sample 18_Vehicle.


#from pandac.PandaModules import loadPrcFileData
#loadPrcFileData('', 'load-display tinydisplay')

import sys
import direct.directbase.DirectStart

from direct.showbase.DirectObject import DirectObject
from direct.showbase.InputStateGlobal import inputState

from panda3d.core import AmbientLight
from panda3d.core import DirectionalLight
from panda3d.core import Vec3
from panda3d.core import Vec4
from panda3d.core import Point3
from panda3d.core import TransformState
from panda3d.core import BitMask32
from panda3d.core import Filename
from panda3d.core import PNMImage

from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletBoxShape
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletDebugNode
from panda3d.bullet import BulletTriangleMesh
from panda3d.bullet import BulletTriangleMeshShape
from panda3d.bullet import BulletVehicle
from panda3d.bullet import ZUp

class Game(DirectObject):

  def __init__(self):
    base.setBackgroundColor(0.1, 0.1, 0.8, 1)
    base.setFrameRateMeter(True)

    base.cam.setPos(0, -20, 4)
    base.cam.lookAt(0, 0, 0)

    # Light
    alight = AmbientLight('ambientLight')
    alight.setColor(Vec4(0.5, 0.5, 0.5, 1))
    alightNP = render.attachNewNode(alight)

    dlight = DirectionalLight('directionalLight')
    dlight.setDirection(Vec3(1, 1, -1))
    dlight.setColor(Vec4(0.7, 0.7, 0.7, 1))
    dlightNP = render.attachNewNode(dlight)

    render.clearLight()
    render.setLight(alightNP)
    render.setLight(dlightNP)

    # Input
    self.accept('escape', self.doExit)
    self.accept('r', self.doReset)
    self.accept('f1', self.toggleWireframe)
    self.accept('f2', self.toggleTexture)
    self.accept('f3', self.toggleDebug)
    self.accept('f5', self.doScreenshot)

    inputState.watchWithModifiers('forward', 'w')
    inputState.watchWithModifiers('left', 'a')
    inputState.watchWithModifiers('reverse', 's')
    inputState.watchWithModifiers('right', 'd')
    inputState.watchWithModifiers('turnLeft', 'q')
    inputState.watchWithModifiers('turnRight', 'e')

    # Task
    taskMgr.add(self.update, 'updateWorld')

    # Physics
    self.setup()

  # _____HANDLER_____

  def doExit(self):
    self.cleanup()
    sys.exit(1)

  def doReset(self):
    self.cleanup()
    self.setup()

  def toggleWireframe(self):
    base.toggleWireframe()

  def toggleTexture(self):
    base.toggleTexture()

  def toggleDebug(self):
    if self.debugNP.isHidden():
      self.debugNP.show()
    else:
      self.debugNP.hide()

  def doScreenshot(self):
    base.screenshot('Bullet')

  # ____TASK___

  def processInput(self, dt):
    engineForce = 0.0
    brakeForce = 0.0

    if inputState.isSet('forward'):
      engineForce = 3000.0
      brakeForce = 0.0

    if inputState.isSet('reverse'):
      engineForce = 0.0
      brakeForce = 100.0

    if inputState.isSet('turnLeft'):
      self.steering += dt * self.steeringIncrement
      self.steering = min(self.steering, self.steeringClamp)

    if inputState.isSet('turnRight'):
      self.steering -= dt * self.steeringIncrement
      self.steering = max(self.steering, -self.steeringClamp)

    # Apply steering to front wheels
    self.vehicle.setSteeringValue(self.steering, 0);
    self.vehicle.setSteeringValue(self.steering, 1);

    # Apply engine and brake to rear wheels
    self.vehicle.applyEngineForce(engineForce, 2);
    self.vehicle.applyEngineForce(engineForce, 3);
    self.vehicle.setBrake(brakeForce, 2);
    self.vehicle.setBrake(brakeForce, 3);

  def update(self, task):
    dt = globalClock.getDt()

    self.processInput(dt)
    self.world.doPhysics(dt, 10, 0.008)

    #print self.vehicle.getWheel(0).getRaycastInfo().isInContact()
    #print self.vehicle.getWheel(0).getRaycastInfo().getContactPointWs()

    #print self.vehicle.getChassis().isKinematic()

    return task.cont

  def cleanup(self):
    self.world = None
    self.worldNP.removeNode()

  def setup(self):
    self.worldNP = render.attachNewNode('World')

    # World
    self.debugNP = self.worldNP.attachNewNode(BulletDebugNode('Debug'))
    self.debugNP.show()

    self.world = BulletWorld()
    self.world.setGravity(Vec3(0, 0, -9.81))
    #self.world.setDebugNode(self.debugNP.node())

    # Added for demonstration as an answer for the forum.
    terrain = base.loader.loadModel("landscape.bam")
    terrain.setPos(0, 0, -17)
    terrain.flattenLight()
    terrain.reparentTo(render)

    mesh = BulletTriangleMesh()

    for node_path in terrain.findAllMatches( '**/+GeomNode' ):
        for i in range(node_path.node().getNumGeoms()):
            geom = node_path.node().getGeom(i)
            mesh.addGeom(geom)
    shape = BulletTriangleMeshShape(mesh, True)

    np = self.worldNP.attachNewNode(BulletRigidBodyNode('Landscape'))
    np.node().addShape(shape)
    np.setCollideMask(BitMask32.allOn())
    self.world.attachRigidBody(np.node())
    
    # --------------------------------------------

    # Chassis
    shape = BulletBoxShape(Vec3(0.6, 1.4, 0.5))
    ts = TransformState.makePos(Point3(0, 0, 0.5))

    np = self.worldNP.attachNewNode(BulletRigidBodyNode('Vehicle'))
    np.node().addShape(shape, ts)
    np.setPos(0, 0, 1)
    np.node().setMass(800.0)
    np.node().setDeactivationEnabled(False)

    self.world.attachRigidBody(np.node())

    #np.node().setCcdSweptSphereRadius(1.0)
    #np.node().setCcdMotionThreshold(1e-7) 

    # Vehicle
    self.vehicle = BulletVehicle(self.world, np.node())
    self.vehicle.setCoordinateSystem(ZUp)
    self.world.attachVehicle(self.vehicle)

    self.yugoNP = loader.loadModel('models/yugo/yugo.egg')
    self.yugoNP.reparentTo(np)

    # Right front wheel
    np = loader.loadModel('models/yugo/yugotireR.egg')
    np.reparentTo(self.worldNP)
    self.addWheel(Point3( 0.70,  1.05, 0.3), True, np)

    # Left front wheel
    np = loader.loadModel('models/yugo/yugotireL.egg')
    np.reparentTo(self.worldNP)
    self.addWheel(Point3(-0.70,  1.05, 0.3), True, np)

    # Right rear wheel
    np = loader.loadModel('models/yugo/yugotireR.egg')
    np.reparentTo(self.worldNP)
    self.addWheel(Point3( 0.70, -1.05, 0.3), False, np)

    # Left rear wheel
    np = loader.loadModel('models/yugo/yugotireL.egg')
    np.reparentTo(self.worldNP)
    self.addWheel(Point3(-0.70, -1.05, 0.3), False, np)

    # Steering info
    self.steering = 0.0            # degree
    self.steeringClamp = 45.0      # degree
    self.steeringIncrement = 120.0 # degree per second

  def addWheel(self, pos, front, np):
    wheel = self.vehicle.createWheel()

    wheel.setNode(np.node())
    wheel.setChassisConnectionPointCs(pos)
    wheel.setFrontWheel(front)

    wheel.setWheelDirectionCs(Vec3(0, 0, -1))
    wheel.setWheelAxleCs(Vec3(1, 0, 0))
    wheel.setWheelRadius(0.25)
    wheel.setMaxSuspensionTravelCm(40.0)

    wheel.setSuspensionStiffness(40.0)
    wheel.setWheelsDampingRelaxation(2.3)
    wheel.setWheelsDampingCompression(4.4)
    wheel.setFrictionSlip(100.0);
    wheel.setRollInfluence(0.1)

game = Game()
run()

After spending five minutes on it.

Panda3DRacer.zip (1.6 MB)

1 Like