本文整理汇总了Python中panda3d.bullet.BulletWorld.attach方法的典型用法代码示例。如果您正苦于以下问题:Python BulletWorld.attach方法的具体用法?Python BulletWorld.attach怎么用?Python BulletWorld.attach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类panda3d.bullet.BulletWorld
的用法示例。
在下文中一共展示了BulletWorld.attach方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestApplication
# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import attach [as 别名]
class TestApplication(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# game objects
self.controlled_obj_index_ = 0
self.level_sectors_ = []
self.controlled_objects_ = []
# active objects
self.active_sector_ = None
self.setupRendering()
self.setupControls()
self.setupPhysics()
self.clock_ = ClockObject()
self.kinematic_mode_ = False
# Task
logging.info("TestSectors demo started")
taskMgr.add(self.update, 'updateWorld')
def setupRendering(self):
self.setBackgroundColor(0.1, 0.1, 0.8, 1)
self.setFrameRateMeter(True)
# Light
alight = AmbientLight('ambientLight')
alight.setColor(Vec4(0.5, 0.5, 0.5, 1))
alightNP = self.render.attachNewNode(alight)
dlight = DirectionalLight('directionalLight')
dlight.setDirection(Vec3(1, 1, -1))
dlight.setColor(Vec4(0.7, 0.7, 0.7, 1))
dlightNP = self.render.attachNewNode(dlight)
self.render.clearLight()
self.render.setLight(alightNP)
self.render.setLight(dlightNP)
self.setupBackgroundImage()
def setupBackgroundImage(self):
image_file = Filename(rospack.RosPack().get_path(BACKGROUND_IMAGE_PACKAGE) + '/resources/backgrounds/' + BACKGROUND_IMAGE_PATH)
# check if image can be loaded
img_head = PNMImageHeader()
if not img_head.readHeader(image_file ):
raise IOError, "PNMImageHeader could not read file %s. Try using absolute filepaths"%(image_file.c_str())
sys.exit()
# Load the image with a PNMImage
w = img_head.getXSize()
h = img_head.getYSize()
img = PNMImage(w,h)
#img.alphaFill(0)
img.read(image_file)
texture = Texture()
texture.setXSize(w)
texture.setYSize(h)
texture.setZSize(1)
texture.load(img)
texture.setWrapU(Texture.WM_border_color) # gets rid of odd black edges around image
texture.setWrapV(Texture.WM_border_color)
texture.setBorderColor(LColor(0,0,0,0))
# creating CardMaker to hold the texture
cm = CardMaker('background')
cm.setFrame(-0.5*w,0.5*w,-0.5*h,0.5*h) # This configuration places the image's topleft corner at the origin (left, right, bottom, top)
background_np = NodePath(cm.generate())
background_np.setTexture(texture)
background_np.reparentTo(self.render)
background_np.setPos(BACKGROUND_POSITION)
background_np.setScale(BACKGROUND_IMAGE_SCALE)
def setupControls(self):
# Input (Events)
self.accept('escape', self.doExit)
self.accept('r', self.doReset)
self.accept('f1', self.toggleWireframe)
self.accept('f2', self.toggleTexture)
self.accept('f3', self.toggleDebug)
self.accept('f5', self.doScreenshot)
self.accept('n', self.selectNextControlledObject)
self.accept('k', self.toggleKinematicMode)
# Inputs (Polling)
self.input_state_ = InputState()
self.input_state_.watchWithModifiers("right","arrow_right")
self.input_state_.watchWithModifiers('left', 'arrow_left')
#.........这里部分代码省略.........
示例2: BulletBase
# 需要导入模块: from panda3d.bullet import BulletWorld [as 别名]
# 或者: from panda3d.bullet.BulletWorld import attach [as 别名]
class BulletBase(object):
""" Manages Panda3d Bullet physics resources and convenience methods."""
# Bitmasks for each object type. By setting ghost and static
# objects to different masks, we can filter ghost-to-static
# collisions.
ghost_bit = BitMask32.bit(1)
static_bit = BitMask32.bit(2)
dynamic_bit = ghost_bit | static_bit
bw_types = (BulletBaseCharacterControllerNode, BulletBodyNode,
BulletConstraint, BulletVehicle)
def __init__(self):
self.world = None
# Parameters for simulation.
self.sim_par = {"size": 1. / 100, "n_subs": 10, "size_sub": 1. / 1000}
# Initialize axis constraint so that there aren't constraints.
self.axis_constraint_fac = Vec3(1, 1, 1)
self.axis_constraint_disp = Vec3(nan, nan, nan)
# Attribute names of destructable objects.
self._destructables = ()
def init(self):
""" Initialize world and resources. """
# Initialize world.
self.world = BulletWorld()
def destroy(self):
""" Destroy all resources."""
for key in self._destructables:
getattr(self, key).destroy()
def setup_debug(self):
debug_node = BulletDebugNode('Debug')
debug_node.showWireframe(True)
debug_node.showConstraints(True)
debug_node.showBoundingBoxes(True)
debug_node.showNormals(True)
self.world.setDebugNode(debug_node)
return debug_node
@property
def bodies(self):
""" Return all bodies (rigid, soft, and ghost) in self.world."""
bodies = (self.world.getRigidBodies() + self.world.getSoftBodies() +
self.world.getGhosts())
return bodies
def _constrain_axis(self, body):
""" Apply existing axis constraints to a body."""
# Set displacements.
for axis, (f, d) in enumerate(zip(self.axis_constraint_fac,
self.axis_constraint_disp)):
if not f and not isnan(d):
nodep = NodePath(body)
pos = nodep.getPos()
pos[axis] = d
nodep.setPos(pos)
try:
# Linear and angular factors of 0 mean forces in the
# corresponding axis are scaled to 0.
body.setLinearFactor(self.axis_constraint_fac)
# Angular factors restrict rotation about an axis, so the
# following logic selects the appropriate axes to
# constrain.
s = sum(self.axis_constraint_fac)
if s == 3.:
v = self.axis_constraint_fac
elif s == 2.:
v = -self.axis_constraint_fac + 1
else:
v = Vec3.zero()
body.setAngularFactor(v)
except AttributeError:
# The body was not a rigid body (it didn't have
# setLinearFactor method).
pass
def set_axis_constraint(self, axis, on, disp=None):
""" Sets an axis constraint, so that bodies can/cannot
move in that direction."""
# Create the constraint vector.
self.axis_constraint_fac[axis] = int(not on)
self.axis_constraint_disp[axis] = disp if disp is not None else nan
# Iterate over bodies, applying the constraint.
for body in self.bodies:
self._constrain_axis(body)
def attach(self, objs, suppress_deact_warn=False):
""" Attach Bullet objects to the world."""
if not self.world:
raise BulletBaseError("No BulletWorld initialized.")
# Make sure they're iterable.
if not isinstance(objs, Iterable):
objs = [objs]
elif isinstance(objs, dict):
objs = objs.itervalues()
bw_objs = []
for obj in objs:
if isinstance(obj, NodePath):
#.........这里部分代码省略.........