Skip to content

Instantly share code, notes, and snippets.

@wezu
Created April 15, 2018 12:55
Show Gist options
  • Select an option

  • Save wezu/fd987811d29095b558b06d1f28ce166c to your computer and use it in GitHub Desktop.

Select an option

Save wezu/fd987811d29095b558b06d1f28ce166c to your computer and use it in GitHub Desktop.
from panda3d.core import *
load_prc_file_data('','bullet-filter-algorithm groups-mask')
from panda3d.bullet import *
from direct.showbase.DirectObject import DirectObject
from direct.showbase import ShowBase
class App(DirectObject):
def __init__(self):
#init panda3d showbase
base = ShowBase.ShowBase()
self.world_np = render.attach_new_node('World')
#debug stuff
self.debug_np = self.world_np.attach_new_node(BulletDebugNode('Debug'))
self.debug_np.node().show_wireframe(True)
self.debug_np.node().show_constraints(True)
self.debug_np.node().show_bounding_boxes(False)
self.debug_np.node().show_normals(False)
#the bullet world
self.world = BulletWorld()
self.world.set_gravity(Vec3(0, 0, -9.81))
self.world.set_debug_node(self.debug_np.node())
self.debug_np.show()
#collision masks
floor =0
box =1
sphere=2
ray =3
self.world.set_group_collision_flag(floor, box , True)
self.world.set_group_collision_flag(floor, sphere , False)
self.world.set_group_collision_flag(floor, ray , True)
self.world.set_group_collision_flag(ray, box , False)
self.world.set_group_collision_flag(ray, floor , True)
self.world.set_group_collision_flag(ray, sphere , False)
#floor
shape = BulletPlaneShape(Vec3(0, 0, 1), 0)
np = self.world_np.attach_new_node(BulletRigidBodyNode('floor'))
np.node().add_shape(shape)
np.set_pos(0, 0, -1)
np.set_collide_mask(BitMask32.bit(floor))
self.world.attach_rigid_body(np.node())
# Box
shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))
np = self.world_np.attach_new_node(BulletRigidBodyNode('box'))
np.node().add_shape(shape)
np.node().set_mass(1.0)
np.set_pos(0, 0, 4)
np.set_collide_mask(BitMask32.bit(box))
self.world.attach_rigid_body(np.node())
# Sphere
shape = BulletSphereShape(0.6)
np = self.world_np.attach_new_node(BulletRigidBodyNode('sphere'))
np.node().add_shape(shape)
np.node().set_mass(1.0)
np.set_pos(0, 2, 4)
np.set_collide_mask(BitMask32.bit(sphere))
self.world.attach_rigid_body(np.node())
taskMgr.add(self.update, "update")
self.accept('mouse1', self.cast_ray)
def cast_ray(self):
pMouse = base.mouseWatcherNode.get_mouse()
pFrom = Point3()
pTo = Point3()
base.camLens.extrude(pMouse, pFrom, pTo)
# Transform to global coordinates
pFrom = render.get_relative_point(base.cam, pFrom)
pTo = render.get_relative_point(base.cam, pTo)
mask=BitMask32()
mask.set_bit(3) #ray =3
result = self.world.ray_test_closest(pFrom, pTo, mask)
#result = self.world.ray_test_closest(pFrom, pTo) #works with no mask
if result.has_hit():
smiley=loader.load_model('smiley')
smiley.reparent_to(render)
smiley.set_pos(result.get_hit_pos())
smiley.set_scale(0.1)
else:
print('miss!')
def update(self, task):
dt = globalClock.get_dt()
self.world.do_physics(dt)
return task.again
app=App()
base.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment