本文整理汇总了Python中panda3d.bullet.BulletWorld.remove_rigid_body方法的典型用法代码示例。如果您正苦于以下问题:Python BulletWorld.remove_rigid_body方法的具体用法?Python BulletWorld.remove_rigid_body怎么用?Python BulletWorld.remove_rigid_body使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类panda3d.bullet.BulletWorld
的用法示例。
在下文中一共展示了BulletWorld.remove_rigid_body方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: World
# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import remove_rigid_body [as 别名]
#.........这里部分代码省略.........
np = self.scene.attach_new_node(debug_node)
np.show()
self.physics.set_debug_node(debug_node)
def get_incarn(self):
return random.choice(self.incarnators)
def attach(self, obj):
assert hasattr(obj, 'world') and hasattr(obj, 'name')
assert obj.name not in self.objects
obj.world = self
if obj.name.startswith('Incarnator'):
self.incarnators.append(obj)
if hasattr(obj, 'create_node') and hasattr(obj, 'create_solid'):
# Let each object define it's own NodePath, then reparent them.
obj.node = obj.create_node()
obj.solid = obj.create_solid()
if obj.solid:
if isinstance(obj.solid, BulletRigidBodyNode):
self.physics.attach_rigid_body(obj.solid)
elif isinstance(obj.solid, BulletGhostNode):
self.physics.attach_ghost(obj.solid)
if obj.node:
if obj.solid:
# If this is a solid visible object, create a new physics node and reparent the visual node to that.
phys_node = self.scene.attach_new_node(obj.solid)
obj.node.reparent_to(phys_node)
obj.node = phys_node
else:
# Otherwise just reparent the visual node to the root.
obj.node.reparent_to(self.scene)
elif obj.solid:
obj.node = self.scene.attach_new_node(obj.solid)
if obj.solid and obj.collide_bits is not None:
obj.solid.set_into_collide_mask(obj.collide_bits)
self.objects[obj.name] = obj
# Let the object know it has been attached.
obj.attached()
return obj
def create_hector(self, name=None):
# TODO: get random incarn, start there
h = self.attach(Hector(name))
h.move((0, 15, 0))
return h
def register_updater(self, obj):
assert isinstance(obj, WorldObject)
self.updatables.add(obj)
def register_updater_later(self, obj):
assert isinstance(obj, WorldObject)
self.updatables_to_add.add(obj)
def update(self, task):
"""
Called every frame to update the physics, etc.
"""
dt = globalClock.getDt()
for obj in self.updatables_to_add:
self.updatables.add(obj)
self.updatables_to_add = set()
for obj in self.updatables:
obj.update(dt)
self.updatables -= self.garbage
self.collidables -= self.garbage
while True:
if len(self.garbage) < 1:
break;
trash = self.garbage.pop()
if(isinstance(trash.solid, BulletGhostNode)):
self.physics.remove_ghost(trash.solid)
if(isinstance(trash.solid, BulletRigidBodyNode)):
self.physics.remove_rigid_body(trash.solid)
if hasattr(trash, 'dead'):
trash.dead()
trash.node.remove_node()
del(trash)
self.physics.do_physics(dt)
for obj in self.collidables:
result = self.physics.contact_test(obj.node.node())
for contact in result.get_contacts():
obj1 = self.objects.get(contact.get_node0().get_name())
obj2 = self.objects.get(contact.get_node1().get_name())
if obj1 and obj2:
# Check the collision bits to see if the two objects should collide.
should_collide = obj1.collide_bits & obj2.collide_bits
if not should_collide.is_zero():
pt = contact.get_manifold_point()
if obj1 in self.collidables:
obj1.collision(obj2, pt, True)
if obj2 in self.collidables:
obj2.collision(obj1, pt, False)
return task.cont
示例2: __init__
# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import remove_rigid_body [as 别名]
class PhysicsManager:
def __init__(self, root_nodepath, world):
self.world = BulletWorld()
self.world.setGravity((0, 0, -9.81))
self._timestep = 1 / world.tick_rate
# # Seems that this does not function
# on_contact_added = PythonCallbackObject(self._on_contact_added)
# self.world.set_contact_added_callback(on_contact_added)
# on_filter = PythonCallbackObject(self._filter_collision)
# self.world.set_filter_callback(on_filter)
self.listener = DirectObject()
self.listener.accept('bullet-contact-added', self._on_contact_added)
self.listener.accept('bullet-contact-destroyed', self._on_contact_removed)
self.tracked_contacts = defaultdict(int)
self.existing_collisions = set()
# Debugging info
debug_node = BulletDebugNode('Debug')
debug_node.showWireframe(True)
debug_node.showConstraints(True)
debug_node.showBoundingBoxes(False)
debug_node.showNormals(False)
# Add to world
self.debug_nodepath = root_nodepath.attachNewNode(debug_node)
self.world.set_debug_node(debug_node)
self.debug_nodepath.show()
def _on_contact_removed(self, node_a, node_b):
self.tracked_contacts[(node_a, node_b)] -= 1
def _on_contact_added(self, node_a, node_b):
self.tracked_contacts[(node_a, node_b)] += 1
def _dispatch_collisions(self):
# Dispatch collisions
existing_collisions = self.existing_collisions
for pair, contact_count in self.tracked_contacts.items():
# If is new collision
if contact_count > 0 and pair not in existing_collisions:
existing_collisions.add(pair)
# Dispatch collision
node_a, node_b = pair
entity_a = node_a.get_python_tag("entity")
entity_b = node_b.get_python_tag("entity")
if not (entity_a and entity_b):
continue
contact_result = ContactResult(self.world, node_a, node_b)
entity_a.messenger.send("collision_started", entity=entity_b, contacts=contact_result.contacts_a)
entity_b.messenger.send("collision_started", entity=entity_a, contacts=contact_result.contacts_b)
# Ended collision
elif contact_count == 0 and pair in existing_collisions:
existing_collisions.remove(pair)
# Dispatch collision
node_a, node_b = pair
entity_a = node_a.get_python_tag("entity")
entity_b = node_b.get_python_tag("entity")
if not (entity_a and entity_b):
continue
entity_a.messenger.send("collision_stopped", entity_b)
entity_b.messenger.send("collision_stopped", entity_a)
def add_entity(self, entity, component):
body = component.body
self.world.attach_rigid_body(body)
body.set_python_tag("entity", entity)
def remove_entity(self, entity, component):
body = component.body
self.world.remove_rigid_body(body)
body.clear_python_tag("entity")
def tick(self):
self.world.do_physics(self._timestep)
self._dispatch_collisions()
def resimulate_pawn(self, pawn):
pass
示例3: World
# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import remove_rigid_body [as 别名]
#.........这里部分代码省略.........
self.physics.attach_ghost(expl_body)
result = self.physics.contact_test(expl_body)
for contact in result.getContacts():
n0_name = contact.getNode0().get_name()
n1_name = contact.getNode1().get_name()
obj = None
try:
obj = self.objects[n1_name]
except:
break
if n0_name == "expl" and n1_name not in EXPLOSIONS_DONT_PUSH and not n1_name.startswith('Walker'):
# repeat contact test with just this pair of objects
# otherwise all manifold point values will be the same
# for all objects in original result
real_c = self.physics.contact_test_pair(expl_body, obj.solid)
mpoint = real_c.getContacts()[0].getManifoldPoint()
distance = mpoint.getDistance()
if distance < 0:
if hasattr(obj, 'decompose'):
obj.decompose()
else:
expl_vec = Vec3(mpoint.getPositionWorldOnA() - mpoint.getPositionWorldOnB())
expl_vec.normalize()
magnitude = force * 1.0/math.sqrt(abs(radius - abs(distance)))
obj.solid.set_active(True)
obj.solid.apply_impulse(expl_vec*magnitude, mpoint.getLocalPointB())
if hasattr(obj, 'damage'):
obj.damage(magnitude/5)
self.physics.remove_ghost(expl_body)
expl_bodyNP.detach_node()
del(expl_body, expl_bodyNP)
def do_plasma_push(self, plasma, node, energy):
obj = None
try:
obj = self.objects[node]
except:
raise
if node not in EXPLOSIONS_DONT_PUSH and not node.startswith('Walker'):
if hasattr(obj, 'decompose'):
obj.decompose()
else:
solid = obj.solid
dummy_node = NodePath('tmp')
dummy_node.set_hpr(plasma.hpr)
dummy_node.set_pos(plasma.pos)
f_vec = render.get_relative_vector(dummy_node, Vec3(0,0,1))
local_point = (obj.node.get_pos() - dummy_node.get_pos()) *-1
f_vec.normalize()
solid.set_active(True)
try:
solid.apply_impulse(f_vec*(energy*35), Point3(local_point))
except:
pass
del(dummy_node)
if hasattr(obj, 'damage'):
obj.damage(energy*5)
def update(self, task):
"""
Called every frame to update the physics, etc.
"""
dt = globalClock.getDt()
for obj in self.updatables_to_add:
self.updatables.add(obj)
self.updatables_to_add = set()
for obj in self.updatables:
obj.update(dt)
self.updatables -= self.garbage
self.collidables -= self.garbage
while True:
if len(self.garbage) < 1:
break;
trash = self.garbage.pop()
if(isinstance(trash.solid, BulletGhostNode)):
self.physics.remove_ghost(trash.solid)
if(isinstance(trash.solid, BulletRigidBodyNode)):
self.physics.remove_rigid_body(trash.solid)
if hasattr(trash, 'dead'):
trash.dead()
trash.node.remove_node()
del(trash)
self.physics.do_physics(dt)
for obj in self.collidables:
result = self.physics.contact_test(obj.node.node())
for contact in result.get_contacts():
obj1 = self.objects.get(contact.get_node0().get_name())
obj2 = self.objects.get(contact.get_node1().get_name())
if obj1 and obj2:
# Check the collision bits to see if the two objects should collide.
should_collide = obj1.collide_bits & obj2.collide_bits
if not should_collide.is_zero():
pt = contact.get_manifold_point()
if obj1 in self.collidables:
obj1.collision(obj2, pt, True)
if obj2 in self.collidables:
obj2.collision(obj1, pt, False)
return task.cont