Panda3D object collision in terrain with ShaderTerrainMesh and ODE physic Engine

0

Hello i have question with panda3D I generate 3D terrain with ShaderTerrainMesh and i want to have physic on this terrain (collsion detection)

how can i add collision between my lot of objects and terrain ?

i see tutorial https://www.panda3d.org/manual/index.php/Collision_Detection_with_ODE 2

and adapte my code, i have terrain, gravity, and load egg object but no collision

from direct.showbase.ShowBase import ShowBase
from panda3d.core import ShaderTerrainMesh, Shader, load_prc_file_data
from panda3d.core import SamplerState

from panda3d.ode import OdeWorld, OdeSimpleSpace, OdeJointGroup
from panda3d.ode import OdeBody, OdeMass, OdeBoxGeom, OdePlaneGeom
from panda3d.core import BitMask32, CardMaker, Vec4, Quat
from random import randint, random
from panda3d.core import Vec3, load_prc_file_data, ShaderTerrainMesh
from panda3d.core import ShaderTerrainMesh, Shader, load_prc_file_data

from direct.actor.Actor import Actor
from panda3d.core import NodePath, CardMaker, SamplerState


class ShaderTerrainDemo(ShowBase):
    def __init__(self):

        # Load some configuration variables, its important for this to happen
        # before the ShowBase is initialized
        load_prc_file_data("", """
            textures-power-2 none
            gl-coordinate-system default
            window-title Panda3D ShaderTerrainMesh Demo
        """)

        # Initialize the showbase
        ShowBase.__init__(self)

        # Increase camera FOV as well as the far plane
        self.camLens.set_fov(90)
        self.camLens.set_near_far(0.1, 50000)

        # Construct the terrain
        self.terrain_node = ShaderTerrainMesh()

        # Set a heightfield, the heightfield should be a 16-bit png and
        # have a quadratic size of a power of two.
        self.terrain_node.heightfield = self.loader.loadTexture("heightfield.png")

        # Set the target triangle width. For a value of 10.0 for example,
        # the terrain will attempt to make every triangle 10 pixels wide on screen.
        self.terrain_node.target_triangle_width = 10.0

        # Generate the terrain
        self.terrain_node.generate()

        # Attach the terrain to the main scene and set its scale. With no scale
        # set, the terrain ranges from (0, 0, 0) to (1, 1, 1)
        self.terrain = self.render.attach_new_node(self.terrain_node)


        self.terrain.set_scale(1024, 1024, 100)
        self.terrain.set_pos(-512, -512, -70.0)

        # Set a shader on the terrain. The ShaderTerrainMesh only works with
        # an applied shader. You can use the shaders used here in your own application
        terrain_shader = Shader.load(Shader.SL_GLSL, "terrain.vert.glsl", "terrain.frag.glsl")
        self.terrain.set_shader(terrain_shader)
        self.terrain.set_shader_input("camera", self.camera)

        # Shortcut to view the wireframe mesh
        self.accept("f3", self.toggleWireframe)

        # Set some texture on the terrain
        grass_tex = self.loader.loadTexture("textures/grass.png")
        grass_tex.set_minfilter(SamplerState.FT_linear_mipmap_linear)
        grass_tex.set_anisotropic_degree(16)
        self.terrain.set_texture(grass_tex)

        # Load a skybox - you can safely ignore this code
        skybox = self.loader.loadModel("models/skybox.bam")
        skybox.reparent_to(self.render)
        skybox.set_scale(20000)

        skybox_texture = self.loader.loadTexture("textures/skybox.jpg")
        skybox_texture.set_minfilter(SamplerState.FT_linear)
        skybox_texture.set_magfilter(SamplerState.FT_linear)
        skybox_texture.set_wrap_u(SamplerState.WM_repeat)
        skybox_texture.set_wrap_v(SamplerState.WM_mirror)
        skybox_texture.set_anisotropic_degree(16)
        skybox.set_texture(skybox_texture)

        skybox_shader = Shader.load(Shader.SL_GLSL, "skybox.vert.glsl", "skybox.frag.glsl")
        skybox.set_shader(skybox_shader)

        # Setup our physics world
        world = OdeWorld()
        world.setGravity(0, 0, -9.81)

        # The surface table is needed for autoCollide
        world.initSurfaceTable(1)
        world.setSurfaceEntry(0, 0, 150, 0.0, 9.1, 0.9, 0.00001, 0.0, 0.002)

        # Create a space and add a contactgroup to it to add the contact joints
        space = OdeSimpleSpace()
        space.setAutoCollideWorld(world)
        contactgroup = OdeJointGroup()
        space.setAutoCollideJointGroup(contactgroup)

        # Load the box
        box = loader.loadModel("box")
        # Make sure its center is at 0, 0, 0 like OdeBoxGeom
        box.setPos(-.5, -.5, -.5)
        box.flattenLight() # Apply transform
        box.setTextureOff()

        dancer = Actor("dancer.egg.pz")
        geomnode = dancer.find('**/-GeomNode').node()
        geomnode.setIntoCollideMask(BitMask32.bit(1))

        dancer.setPos(0,0,0)
        chorusline = NodePath('chorusline')

        boxes = []
        for i in range(5):
          placeholder = chorusline.attachNewNode("Dancer-Placeholder")
          placeholder.setPos(i*5,-.5, -.5)
          dancer.instanceTo(placeholder)

          boxBody = OdeBody(world)
          M = OdeMass()
          M.setBox(50, 1, 1, 1)
          boxBody.setMass(M)
          boxBody.setPosition(dancer.getPos(render))
          boxBody.setQuaternion(dancer.getQuat(render))
          # Create a BoxGeom
          boxGeom = OdeBoxGeom(space, 1, 1, 1)
          boxGeom.setCollideBits(BitMask32(0x00000002))
          boxGeom.setCategoryBits(BitMask32(0x00000001))
          boxGeom.setBody(boxBody)
          boxes.append((dancer, boxBody))

        for i in range(3):
          placeholder = render.attachNewNode("Line-Placeholder")
          placeholder.setPos(-.5,i*10,-.5)
          chorusline.instanceTo(placeholder)


        # Add a random amount of boxes

        for i in range(randint(15, 30)):
          # Setup the geometry
          boxNP = box.copyTo(render)
          boxNP.setPos(randint(-10, 10), randint(-10, 10), 10 + random())
          boxNP.setColor(random(), random(), random(), 1)
          boxNP.setHpr(randint(-45, 45), randint(-45, 45), randint(-45, 45))
          # Create the body and set the mass
          boxBody = OdeBody(world)
          M = OdeMass()
          M.setBox(50, 1, 1, 1)
          boxBody.setMass(M)
          boxBody.setPosition(boxNP.getPos(render))
          boxBody.setQuaternion(boxNP.getQuat(render))
          # Create a BoxGeom
          boxGeom = OdeBoxGeom(space, 1, 1, 1)
          boxGeom.setCollideBits(BitMask32(0x00000002))
          boxGeom.setCategoryBits(BitMask32(0x00000001))
          boxGeom.setBody(boxBody)
          boxes.append((boxNP, boxBody))

        #self.terrain.setPos(0, 0, 0); self.terrain.lookAt(0, 0, -1)

        self.terrain.setCollideMask(BitMask32.bit(0))

        # Set the camera position
        base.camera.setPos(40, 40, 20)
        base.camera.lookAt(0, 0, 0)

        # The task for our simulation
        def simulationTask(task):
          space.autoCollide() # Setup the contact joints
          # Step the simulation and set the new positions
          world.quickStep(globalClock.getDt())
          for np, body in boxes:
            np.setPosQuat(render, body.getPosition(), Quat(body.getQuaternion()))
          contactgroup.empty() # Clear the contact joints
          return task.cont

        # Wait a split second, then start the simulation  
        taskMgr.doMethodLater(0.5, simulationTask, "Physics Simulation")

ShaderTerrainDemo().run()

i want collision with my ShaderTerrainMesh and all objects load (box and dancer egg file)

python
panda3d
asked on Stack Overflow May 10, 2018 by sazearte

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0