You can download this code by clicking the button below.
This code is now available for download.
This code defines a function that creates a randomly sized box in Panda3D and sets up collision detection. When the box collides with other objects, it prints out collision information.
Technology Stack : Panda3D
Code Type : The type of code
Code Difficulty :
def create_random_box(name, size=(1, 1, 1)):
from panda3d.core import CollisionNode, CollisionHandlerQueue, GeomNode
from panda3d.geom import Box
from panda3d.task import Task
def create_box(root, name, size):
box = Box(size[0], size[1], size[2])
box_node = GeomNode('box_geom')
box_node.addGeom(box)
box_collision = CollisionNode('box_collision')
box_collision.addSolid(box)
root.attachNewNode(box_collision)
box_collision.setIntoCollideMask(1)
box_collision.setFromCollideMask(1)
box_collision.addIntoCollideMask(1)
box_collision.setCategoryBit(1, 1)
box_collision.set碰撞处理函数(create_box_collision_handler)
root.task.addTask(create_box_collision_handler)
def create_box_collision_handler(root, event):
if event.getIntoNode().getName() == name:
print(f"Collision detected with {name} at position {event.getSurfacePoint(root)}")
root = render
create_box(root, name, size)