Пример #1
0
	def init(self, browser):
		super(demo, self).init(browser)
		# 初始化
		iphy2d.init()
		# debug 显示
		iworld2d.init()
		iworld2d.camera_world_pos_center(0, 0)
		iphy2d.set_debug_draw(True)
		iworld2d.camera_world_scale(PHY_SCALE)
		# 设置重力
		iphy2d.set_gravity((0, 10.0))
		# 创建一个地面
		self.ground = iphy2d.body(iphy2d.POLYGON, math3d.vector2(35.0, 1))
		self.ground.body_type = iphy2d.STATIC_BODY
		self.ground.position = math3d.vector2(0, 25)
		self.ground.resititution = 0.0
		self.balls = []
		# 弹力
		restitutions = (0.0, 0.1, 0.3, 0.5, 0.75, 0.9, 1.0)
		for i in xrange(7):
			body = iphy2d.body(iphy2d.SPHERE, 1.0)
			body.body_type = iphy2d.DYNAMIC_BODY
			body.position = math3d.vector2(-10 + 3 * i, 20.0)
			body.restitution = restitutions[i]
			body.density = 1.0
			self.balls.append(body)
Пример #2
0
	def init(self, browser):
		super(demo, self).init(browser)
		# 初始化
		iphy2d.init()
		# debug 显示
		iworld2d.init()
		iworld2d.camera_world_pos_center(0, 0)
		iphy2d.set_debug_draw(True)
		iworld2d.camera_world_scale(PHY_SCALE)
		# 取消重力
		iphy2d.set_gravity((0, 0))
		# 创建一个封闭空间
		self.ground1 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(10, 1))
		self.ground1.position = math3d.vector2(0, -10)
		self.ground1.body_type = iphy2d.STATIC_BODY
		self.ground1.category = F_EDGE
		self.ground2 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(10, 1))
		self.ground2.position = math3d.vector2(0, 10)
		self.ground2.body_type = iphy2d.STATIC_BODY
		self.ground2.category = F_EDGE
		self.ground3 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1, 10))
		self.ground3.position = math3d.vector2(-10, 0)
		self.ground3.body_type = iphy2d.STATIC_BODY
		self.ground3.category = F_EDGE
		self.ground4 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1, 10))
		self.ground4.position = math3d.vector2(10, 0)
		self.ground4.body_type = iphy2d.STATIC_BODY
		self.ground4.category = F_EDGE
		# 创建物件
		self.objs = []
Пример #3
0
	def __init__(self):
		super(PhysicMgr, self).__init__()
		self.dynamicMap = {}
		self.staticMap = {}
		self.bDebugView = False
		
		#初始化物理引擎工作
		iphy2d.init(30)
		iphy2d.set_gravity(PhysicMgr.gravity)
Пример #4
0
def init():
	# 对物理模块初始化
	iphy2d.init(30)
	iphy2d.set_gravity((0, 10))

	global g_objs
	g_objs = {}

	# iworld2d模块初始化
	iworld2d.init()
	# 生成背景
	global background
	background = iworld2d.image2d("idemos/res/rabbit/world2d/txg/demo.txg|background", layer_id=0)
	background.pos = (0,0)
Пример #5
0
	def init(self, browser):
		super(demo, self).init(browser)
		# 初始化
		iphy2d.init()
		# debug 显示
		iworld2d.init()
		iworld2d.camera_world_pos_center(0, 0)
		iphy2d.set_debug_draw(True)
		iworld2d.camera_world_scale(PHY_SCALE)
		# 设置重力
		iphy2d.set_gravity((0, 10.0))
		# 创建一个地面
		self.ground = iphy2d.body(iphy2d.POLYGON, math3d.vector2(35.0, 1))
		self.ground.body_type = iphy2d.STATIC_BODY
		self.ground.position = math3d.vector2(0, 15)
		# 斜面1,2,3
		self.p1 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(13.0, 0.25))
		self.p1.body_type = iphy2d.STATIC_BODY
		self.p1.position = math3d.vector2(-4.0, -5.0)
		self.p1.rot = 0.25
		self.p1.friction = 0.2
		self.p1.restitution = 0.0
		self.p2 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(13.0, 0.25))
		self.p2.body_type = iphy2d.STATIC_BODY
		self.p2.position = math3d.vector2(4.0, 2.0)
		self.p2.rot = -0.25
		self.p2.friction = 0.2
		self.p2.restitution = 0.0
		self.p3 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(13.0, 0.25))
		self.p3.body_type = iphy2d.STATIC_BODY
		self.p3.position = math3d.vector2(-4.0, 9.0)
		self.p3.rot = 0.25
		self.p3.friction = 0.2
		self.p3.restitution = 0.0
		self.bricks = []
		# 摩擦
		frictions = (0.75, 0.5, 0.35, 0.1, 0.0)
		for i in xrange(5):
			body = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1, 1))
			body.body_type = iphy2d.DYNAMIC_BODY
			body.position = math3d.vector2(-10 + 4 * i, -7.0)
			body.restitution = 0.0
			body.density = 25.0
			body.friction = frictions[i]
			self.bricks.append(body)
Пример #6
0
	def init(self, browser):
		super(demo, self).init(browser)
		# 初始化
		iphy2d.init()
		# debug 显示
		iworld2d.init()
		iworld2d.camera_world_pos_center(0, 0)
		iphy2d.set_debug_draw(True)
		iworld2d.camera_world_scale(PHY_SCALE)
		# 设置重力
		iphy2d.set_gravity((0, 10.0))
		# 设置碰撞回调
		iphy2d.set_first_collided_callback(body_collide)
		# 创建一个地面
		self.ground = iphy2d.body(iphy2d.POLYGON, math3d.vector2(35.0, 1))
		self.ground.body_type = iphy2d.STATIC_BODY
		self.ground.position = math3d.vector2(0, 15)
		self.tr1 = self.tr2 = self.box1 = self.box2 = self.circle1 = self.circle2 = None
Пример #7
0
	def init(self, browser):
		super(demo, self).init(browser)
		# 初始化
		iphy2d.init()
		# debug 显示
		iworld2d.init()
		iworld2d.camera_world_pos_center(0, 0)
		iphy2d.set_debug_draw(True)
		iworld2d.camera_world_scale(PHY_SCALE)
		# 取消重力
		iphy2d.set_gravity((0, 0))
		# 创建一个封闭空间
		self.ground1 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(15, 1))
		self.ground1.position = math3d.vector2(0, -15)
		self.ground1.body_type = iphy2d.STATIC_BODY
		self.ground1.resititution = 0.4
		self.ground2 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(15, 1))
		self.ground2.position = math3d.vector2(0, 15)
		self.ground2.body_type = iphy2d.STATIC_BODY
		self.ground2.resititution = 0.4
		self.ground3 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1, 15))
		self.ground3.position = math3d.vector2(-15, 0)
		self.ground3.body_type = iphy2d.STATIC_BODY
		self.ground3.resititution = 0.4
		self.ground4 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1, 15))
		self.ground4.position = math3d.vector2(15, 0)
		self.ground4.body_type = iphy2d.STATIC_BODY
		self.ground4.resititution = 0.4
		
		# 创建一个飞行器
		self.body = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1, 1))
		self.body.destroy_fixture(0) # 将默认的删除
		self.body.create_fixture(iphy2d.POLYGON, (math3d.vector2(2, 0),
			math3d.vector2(-1, 1), math3d.vector2(-1, -1)), 4.0)
		self.body.position = math3d.vector2(0, 2.0)
		self.body.angular_damping = 5.0
		self.body.linear_damping = 0.1
		self.body.allow_sleeping = False
Пример #8
0
def on_key_msg(msg, key):
	global ball
	if msg == game.MSG_KEY_DOWN:
		if key is game.VK_F1:
			for i in range(20):
				test = create_obj("bear", 100, random.randint(50, 1000), random.randint(0, 100))
				test.phy.body_type = iphy2d.DYNAMIC_BODY
		elif key is  game.VK_F2:
			iphy2d.set_gravity((10, 1))
		elif key is game.VK_F3:
			iphy2d.set_gravity((0, 10))
		elif key is game.VK_F4:
			# 球和曲面的弹性系数均为0
			test = create_obj("sin", 10, 250, 560)
			ball = create_obj("obj", 11, 250, 400)
			# 小球的碰撞参数是2,如果设为1,则不产生碰撞
			ball.phy.category = 2
		elif key == game.VK_F5:
			iphy2d.set_gravity((0, 10))
			destroy_map()
			create_map()
		elif key == game.VK_F7:
			if not background.is_hide():
				# 进入debug模式
				background.hide()
				import rabbit_obj
				iworld2d.camera_world_scale(rabbit_obj.MAP_SCALE)
				iworld2d.camera_world_pos(-512+rabbit_obj.MAP_SCALE*2+4, -368+rabbit_obj.MAP_SCALE*2-20)
				iphy2d.set_debug_draw(True)
			else:
				# 还原
				iphy2d.set_debug_draw(False)
				background.show()
				iworld2d.camera_world_scale(1)
				iworld2d.camera_world_pos(0, 0)
		elif key == game.VK_F11:
			global pflag
			pflag = not pflag
			idemo_glb.API.show_performance(pflag)
		elif key == game.VK_SPACE:
			if ball:
				ball.phy.apply_linear_impulse(math3d.vector2(0,100), ball.phy.position)
				ball.phy.density = 100
				ball.phy.restitution = 0
		elif key == game.VK_1:
			# 进入距离关节的演示(相当于box2d testbed的web)
			rabbit_test_1.start()
		elif key == game.VK_2:
			# 进入旋转关节的演示(相当于box2d testbed的bridge)
			rabbit_test_2.start()
		elif key == game.VK_3:
			# 进入移动关节的演示(相当于box2d testbed的prismatic)
			rabbit_test_3.start()
		elif key == game.VK_4:
			# 进入组合关节的演示(相当于box2d testbed的slider crank)
			rabbit_test_4.start()
		elif key == game.VK_5:
			# 进入摩擦关节的演示(相当于box2d testbed的apply force)
			rabbit_test_5.start()
	elif msg == game.MSG_KEY_PRESSED:
		if key == game.MOUSE_BUTTON_LEFT:
			# 如果按住鼠标左键,则更新浣熊位置
			x = game.mouse_x
			y = game.mouse_y
			if obj_on_mouse:
				obj_on_mouse.pos = (x, y)
				obj_on_mouse.phy.body_type = iphy2d.STATIC_BODY
	elif msg == game.MSG_KEY_UP:
		if key == game.VK_SPACE:
			if ball:
				ball.phy.density = 0.3
Пример #9
0
	def SetGravity( self, force ):
		iphy2d.set_gravity( force )