Panda3d physic cube collision with place

Hello, i don’t understand in the doc how to make an object fall on my flat surface.

i create my surface with CollisionPlane i load my cube object, but the object falls to infinity (not colide with my plane)

import sys

from direct.showbase.ShowBase import ShowBase
from panda3d.core import *

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.physics import ForceNode, LinearVectorForce
import copy
from panda3d.physics import *

class Physic(ShowBase):

  def __init__(self):
    ShowBase.__init__(self)
    # enable physics
    base.enableParticles()
    base.cTrav = CollisionTraverser("base collision traverser")
    base.cTrav.setRespectPrevTransform(True)

    # setup default gravity
    gravityFN = ForceNode("world-forces")
    gravityFNP = render.attachNewNode(gravityFN)
    gravityForce = LinearVectorForce(0, 0, -9.81)  # gravity acceleration
    gravityFN.addForce(gravityForce)
    base.physicsMgr.addLinearForce(gravityForce)

    # Ground Plane
    plane = CollisionPlane(Plane(Vec3(0, 0, 1), Point3(0, 0, -4)))
    self.ground = render.attachNewNode(CollisionNode("Ground"))
    self.ground.node().addSolid(plane)
    self.ground.show()

    model = loader.loadModel('sphere.bam')
    model.setPos(-0.5, -0.5, 10)
    node = NodePath("PhysicsNode")
    node.reparentTo(render)
    an = ActorNode("sphere")
    anp = node.attachNewNode(an)
    base.physicsMgr.attachPhysicalNode(an)
    model.reparentTo(anp)

app = Physic()
app.run()

You will need a collision handler to actually detect and handle the collision. You do not need a physics system to accomplish what you are trying, but if you do use the built-in physics system, I believe you would need a PhysicsCollisionHandler.

it doesn’t work, I can’t find what I’m looking for in the panda doc or in the forum messages.
I managed to make a simpler code but there is still no colision

import direct.directbase.DirectStart
import random

from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *

loadPrcFileData("", """syn-video 0""")


base.cam.setPos(0, -30, 10)
base.cam.lookAt((0,0,4))

base.cTrav = CollisionTraverser()
base.enableParticles()

collisionHandler = PhysicsCollisionHandler()

collisionPlane = CollisionPlane(Plane(Vec3(0, 0, 1), Point3(0,0,0)))
plane = base.render.attachNewNode(CollisionNode('planecnode'))
plane.node().addSolid(collisionPlane)
plane.show()

globalForcesForceNode = ForceNode('world-forces')
globalForces = base.render.attachNewNode(globalForcesForceNode)

globalForcesGravity = LinearVectorForce(0, 0, -9.81)
globalForcesForceNode.addForce(globalForcesGravity)

base.physicsMgr.addLinearForce(globalForcesGravity)

def phyball_dispenser(modelname, scale=1.):
  ball = NodePath(PandaNode('phisicsball'))
  ballActor = ActorNode('ballactornode')
  ballActorNP = ball.attachNewNode(ballActor)
  ballModel = loader.loadModel(modelname)
  ballModel.reparentTo(ballActorNP)
  ballModel.setScale(scale)
  ballModel.setPos(0, 0, 10)

  ballCollider = ballActorNP.attachNewNode(CollisionNode('ballcnode'))
  ballCollider.node().addSolid(CollisionSphere(0, 0, 0, 1*scale))

  base.physicsMgr.attachPhysicalNode(ballActor)

  collisionHandler.addCollider(ballCollider, ballActorNP)
  base.cTrav.addCollider(ballCollider, collisionHandler)
  ball.reparentTo(base.render)
  return ball
smiley = phyball_dispenser('smiley')

run()

I think I used the PhysicsCollisionHandler well, the code doesn’t crash and I give it the right arguments

my goal is to make the smiley face fall on the plane surface.
using panda’s integrated physics engine

Do this and the problem will become apparent:

ballCollider.show()

You’ve put the ball model at a Z of 10 in relation to the physics node. So the ball model hovers over the actual physics shape. You need to call ballActorNP.setPos to position the ball, instead of ballModel. Then you’ll see that the code works.

thank you I understand, you have to put 0,0,0 in position