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)
def on_mouse_msg(msg, key): global obj_on_mouse global down_pos if obj_on_mouse is None: # 生成浣熊 import rabbit_bear obj_on_mouse = create_obj("bear", 100, 0, 0) obj_on_mouse.face.bring_to_front() if msg == game.MSG_MOUSE_DOWN: # 记录鼠标按下的位置 x = game.mouse_x y = game.mouse_y down_pos = math3d.vector2(x, y) obj_on_mouse.phy.linear_damping = 0 obj_on_mouse.stop_fly() elif msg == game.MSG_MOUSE_UP: # 鼠标松开时,按照距离计算推力的大小 newpos = math3d.vector2(game.mouse_x, game.mouse_y) if down_pos is None: return nn = (down_pos-newpos) if nn: nn.x *= 40 nn.y *= 40 print "bear linear impulse", nn.length obj_on_mouse.phy.body_type = iphy2d.DYNAMIC_BODY obj_on_mouse.phy.apply_linear_impulse(nn, obj_on_mouse.phy.position) obj_on_mouse.fly() down_pos = None
def __init__(self, ptype, prim, density=0.0): x0 = 70 y0 = 470 x1 = -20 y1 = A * math.cos(w * x1 + phi) + k self.phy_list = [] for i in range(MAX_SEGMENT): x2 = x1 + STEP y2 = A * math.cos(w * x2 + phi) + k param = [] param.append(math3d.vector2((x1+x0)/rabbit_obj.MAP_SCALE, (y1+y0)/rabbit_obj.MAP_SCALE)) param.append(math3d.vector2((x2+x0)/rabbit_obj.MAP_SCALE, (y2+y0)/rabbit_obj.MAP_SCALE)) phy = iphy2d.body(ptype, tuple(param)) phy.body_type = iphy2d.STATIC_BODY phy.density = 1 phy.restitution = 0 phy.friction = 100 phy.category = 1 phy.collide_mask = 2 # 只接受category=2的碰撞 self.phy_list.append(phy) # 物理引擎对象 x1 = x2 y1 = y2 self.face = None # 渲染对象 self.name = "" self.phy = self.phy_list[0] self.init()
def __init__(self): import rabbit_scene # 创建一个固定物体 self.ground = iphy2d.body(iphy2d.POLYGON, (math3d.vector2(0, 46), math3d.vector2(68, 46))) self.ground.body_type = iphy2d.STATIC_BODY self.ground.pos = (10, 15) self.body = rabbit_scene.create_obj("obj",3, start_x, start_y)
def __init__(self): import rabbit_scene # 创建网上的4个body self.body = [] self.body.append(rabbit_scene.create_obj("obj", 7, 100, 100)) self.body.append(rabbit_scene.create_obj("obj", 7, 300, 100)) self.body.append(rabbit_scene.create_obj("obj", 7, 100, 300)) self.body.append(rabbit_scene.create_obj("obj", 7, 300, 300)) # 创建一个固定物体 self.ground = iphy2d.body(iphy2d.POLYGON, (math3d.vector2(start_x-length, 1), math3d.vector2(start_x+length+length, 1))) self.ground.body_type = iphy2d.STATIC_BODY self.ground.pos = (0, 0)
def CreateBlockStage( self, bornx, borny, width, height, speedx, speedy, resName ): #出生位置 + 矩形宽高 + 速度向量 seName = "Test" se = ut.Create_SEWithRDPH( resName, seName, bornx, borny, width, height ) ut.Get_RenderDataBySE(se).obj2d.size = ( 2*width, 2*height ) pd = ut.Get_PhysicDataBySE(se) pd.body.pos = (bornx+width, borny+height) #图片的坐标点 跟 物理位置的坐标 不同 所以需要矫正 pd.body.apply_force( -PhysicMgr.gravity, math3d.vector2( se.pos[0], se.pos[1] ) ) #作用一个与重力相反的力 pd.body.linear_velocity = math3d.vector2( speedx, speedy ) #施加一个速度给这个stage self.blockVec[seName] = pd return se
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 = []
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) # 创建body # dynamic body # 创建一个body,默认形状为矩形 self.d_body = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1.0, 1.0)) self.d_body.body_type = iphy2d.DYNAMIC_BODY # dynamic body self.d_body.position = math3d.vector2(0.0, 0.0)# 起始位置 self.d_body.rot = 0.0 # 弧度 self.d_body.linear_velocity = math3d.vector2(-1.0, 1.0)# 线性速度 self.d_body.angular_velocity = math.pi# 角速度 # static body self.s_body = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1.0, 1.0)) self.s_body.body_type = iphy2d.STATIC_BODY # static body self.s_body.position = math3d.vector2(-10.0, 10.0)# 起始位置 self.s_body.rot = 0.0 # 弧度 # kinematic body self.k_body = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1.0, 1.0)) self.k_body.body_type = iphy2d.KINEMATIC_BODY # static body self.k_body.position = math3d.vector2(5.0, -5.0)# 起始位置 self.k_body.rot = 0.0 # 弧度 self.k_body.linear_velocity = math3d.vector2(-2.0, 2.0)# 线性速度 self.k_body.angular_velocity = math.pi# 角速度
def __init__(self): import rabbit_scene # 创建一个固定物体 self.ground = iphy2d.body(iphy2d.POLYGON, (math3d.vector2(0, 46), math3d.vector2(68, 46))) self.ground.body_type = iphy2d.STATIC_BODY self.ground.pos = (start_x/rabbit_obj.MAP_SCALE, (start_y+200)/rabbit_obj.MAP_SCALE) # 最底下的杆子1 self.crank = rabbit_scene.create_obj("obj", 13, start_x, start_y) # 稍长一点的杆子2 self.follower = rabbit_scene.create_obj("obj", 14, start_x, start_y) # 长杆子上绑的一个矩形1 self.piston = rabbit_scene.create_obj("obj", 15, start_x, start_y) # 最上面自由落体的矩形2 self.payload = rabbit_scene.create_obj("obj", 15, start_x, start_y+200)
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)
def restart(self): self.body.pos = (start_x, start_y) # 示范移动关节 axis = math3d.vector2(2, 1) axis.normalize() dj = iphy2d.prismatic_joint(self.ground, self.body.phy, \ axis = axis, enable_motor=True, motor_speed=10, max_torque = 1000, \ enable_limit=True, min_translation=0, max_translation=20) self.dj = dj
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
def __init__(self, ptype, param, density=0.0): if ptype == iphy2d.POLYGON: param = math3d.vector2(param[0]/MAP_SCALE, param[1]/MAP_SCALE) elif ptype == iphy2d.SPHERE: param = param/MAP_SCALE self.phy = iphy2d.body(ptype, param) # 物理引擎对象 self.phy.set_user_data(self) self.face = None # 渲染对象 self.name = "" self.init()
def on_key_msg(self, msg, key): super(demo, self).on_key_msg(msg, key) if msg == game.MSG_KEY_PRESSED and key == game.VK_W: f = math3d.vector2(math.cos(self.body.rot) * 200, math.sin(self.body.rot) * 200) self.body.apply_force(f, self.body.position) if msg == game.MSG_KEY_PRESSED and key == game.VK_A: self.body.apply_torque(-50) if msg == game.MSG_KEY_PRESSED and key == game.VK_D: self.body.apply_torque(50)
def restart(self): line_length = math3d.vector2((length/2-13)/rabbit_obj.MAP_SCALE, (length/2-15)/rabbit_obj.MAP_SCALE).length # 重置4个body位置 self.body[0].pos = (start_x, start_y) self.body[1].pos = (start_x+length, start_y) self.body[2].pos = (start_x, start_y+length) self.body[3].pos = (start_x+length, start_y+length) # 四个body分别连接到ground上 self.dj_lst = [] p1 = math3d.vector2((start_x-length/2)/15, (start_y-length/2)/15) p2 = math3d.vector2(-13/rabbit_obj.MAP_SCALE, -12/rabbit_obj.MAP_SCALE) dj = iphy2d.distance_joint(self.ground, self.body[0].phy, \ anchor_a=p1, anchor_b=p2, len=line_length, fre=2, damp=0.5) self.dj_lst.append(dj) p1 = math3d.vector2((start_x+length+length/2)/15, (start_y-length/2)/15) p2 = math3d.vector2(13/rabbit_obj.MAP_SCALE, -12/rabbit_obj.MAP_SCALE) dj = iphy2d.distance_joint(self.ground, self.body[1].phy, \ anchor_a=p1, anchor_b=p2, len=line_length, fre=2, damp=0.5) self.dj_lst.append(dj) p1 = math3d.vector2((start_x-length/2)/15, (start_y+length+length/2)/15) p2 = math3d.vector2(-13/rabbit_obj.MAP_SCALE, 12/rabbit_obj.MAP_SCALE) dj = iphy2d.distance_joint(self.ground, self.body[2].phy, \ anchor_a=p1, anchor_b=p2, len=line_length, fre=2, damp=0.5) self.dj_lst.append(dj) p1 = math3d.vector2((start_x+length+length/2)/15, (start_y+length+length/2)/15) p2 = math3d.vector2(13/rabbit_obj.MAP_SCALE, 12/rabbit_obj.MAP_SCALE) dj = iphy2d.distance_joint(self.ground, self.body[3].phy, \ anchor_a=p1, anchor_b=p2, len=line_length, fre=2, damp=0.5) self.dj_lst.append(dj) # 用四条边连接四个body line_length = length/rabbit_obj.MAP_SCALE dj = iphy2d.distance_joint(self.body[0].phy, self.body[1].phy, \ len=line_length, fre=2, damp=0.5) self.dj_lst.append(dj) dj = iphy2d.distance_joint(self.body[1].phy, self.body[3].phy, \ len=line_length, fre=2, damp=0.5) self.dj_lst.append(dj) dj = iphy2d.distance_joint(self.body[3].phy, self.body[2].phy, \ len=line_length, fre=2, damp=0.5) self.dj_lst.append(dj) dj = iphy2d.distance_joint(self.body[2].phy, self.body[0].phy, \ len=line_length, fre=2, damp=0.5) self.dj_lst.append(dj)
def __init__(self): import rabbit_scene # 创建一个固定物体 self.ground = iphy2d.body(iphy2d.POLYGON, (math3d.vector2(-50, 1), math3d.vector2(50, 1))) self.ground.body_type = iphy2d.STATIC_BODY self.ground.pos = (20, 43) # 生成一段萝卜 self.body = [] for i in range(MAX_SEGMENT): b = rabbit_scene.create_obj("obj",12, start_x+i*10, start_y) self.body.append(b) # 另外模拟一个时钟的走动 self.b1 = rabbit_scene.create_obj("bear", 100, 300, 200) self.b2 = rabbit_scene.create_obj("obj", 15, 300, 400) self.r1 = iphy2d.revolute_joint(self.b1.phy, self.b2.phy, anchor_b=self.b1.phy.get_local_point(math3d.vector2(20,26)),\ #enable_limit=True, lower_angle=-math.pi/2, upper_angle=math.pi/2, enable_motor = True, motor_speed=math.pi*0.1, max_torque=10000, )
def start_test(self): self.destroy_bodies() xlo, xhi = -9, 9 ylo, yhi = -9, 9 for i in xrange(3): obj = self.create_obj(True, True) obj.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.objs.append(obj) obj = self.create_obj(False, True) obj.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.objs.append(obj) obj = self.create_obj(True, False) obj.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.objs.append(obj) obj = self.create_obj(False, False) obj.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.objs.append(obj)
def restart(self): self.dj_lst = [] pre_body = self.ground # 把旋转关节都分别连到每两个萝卜上 for i in range(MAX_SEGMENT): self.body[i].pos = (start_x+i*16, start_y) p = math3d.vector2((start_x-8+i*16)/rabbit_obj.MAP_SCALE, start_y/rabbit_obj.MAP_SCALE) a1 = pre_body.get_local_point(p) a2 = self.body[i].phy.get_local_point(p) dj = iphy2d.revolute_joint(pre_body, self.body[i].phy, \ anchor_a=a1, anchor_b=a2) self.dj_lst.append(dj) pre_body = self.body[i].phy p = math3d.vector2((start_x-8+MAX_SEGMENT*16)/rabbit_obj.MAP_SCALE, start_y/rabbit_obj.MAP_SCALE) a1 = pre_body.get_local_point(p) a2 = self.ground.get_local_point(p) dj = iphy2d.revolute_joint(self.body[MAX_SEGMENT-1].phy, self.ground, \ anchor_a=a1, anchor_b=a2) self.dj_lst.append(dj)
def restart(self): pre_body = self.ground # 杆子1和固定物体,用旋转关节连在一起,并启用一个马达 self.crank.pos = (start_x, start_y) p = math3d.vector2(start_x/rabbit_obj.MAP_SCALE, (start_y+40)/rabbit_obj.MAP_SCALE) a1 = pre_body.get_local_point(p) a2 = self.crank.phy.get_local_point(p) self.j1 = iphy2d.revolute_joint(pre_body, self.crank.phy, \ anchor_a=a1, anchor_b=a2, \ enable_motor=True, motor_speed=math.pi, max_torque = 10000) pre_body = self.crank.phy # 杆子1和杆子2,用旋转关节连在一起 self.follower.pos = (start_x, start_y-120) p = math3d.vector2(start_x/rabbit_obj.MAP_SCALE, (start_y-40)/rabbit_obj.MAP_SCALE) a1 = pre_body.get_local_point(p) a2 = self.follower.phy.get_local_point(p) self.j2 = iphy2d.revolute_joint(pre_body, self.follower.phy, \ anchor_a=a1, anchor_b=a2) pre_body = self.follower.phy # 杆子2和矩形1,用旋转关节连在一起 self.piston.pos = (start_x, start_y-200) p = math3d.vector2(start_x/rabbit_obj.MAP_SCALE, (start_y-200)/rabbit_obj.MAP_SCALE) a1 = pre_body.get_local_point(p) a2 = self.piston.phy.get_local_point(p) self.j3 = iphy2d.revolute_joint(pre_body, self.piston.phy, \ anchor_a=a1, anchor_b=a2) # 固定物体和矩形1,用移动关节连载一起,并启用一个马达 p = math3d.vector2(start_x/rabbit_obj.MAP_SCALE, (start_y-200)/rabbit_obj.MAP_SCALE) a1 = self.ground.get_local_point(p) a2 = self.piston.phy.get_local_point(p) axis = math3d.vector2(0, 1) self.j4 = iphy2d.prismatic_joint(self.ground, self.piston.phy, \ anchor_a=a1, anchor_b=a2, axis=axis, \ enable_motor=True, max_torque=1000) self.payload.pos = (start_x, start_y-320)
def ProEvt_KeyDown( key ): if key == game.VK_A: print("key A is Down") se = ut.Get_SEByName("mod") pd = ut.Get_PhysicDataBySE( se ) pd.body.apply_linear_impulse( math3d.vector2( -20, 0 ), math3d.vector2( se.pos[0], se.pos[1] ) ) if key == game.VK_D: print("key D is Down") se = ut.Get_SEByName("mod") pd = ut.Get_PhysicDataBySE( se ) pd.body.apply_linear_impulse( math3d.vector2( 20, 0), math3d.vector2( se.pos[0], se.pos[1] ) ) if key == game.VK_W: print("key W is Down") se = ut.Get_SEByName("mod") se.GetComponetData("PH").body.apply_force( math3d.vector2( 0, -300), math3d.vector2( se.pos[0], se.pos[1] ) ) ut.Time_Callback( 1, Cancle_Force ) if key == game.VK_SPACE: print("key Space is Down") Core.PhysicMgr.SwitchDebugView() if key == game.VK_S: se = ut.Get_SEByName("mod") print("key S is Down") print se.GetComponetData("PH").body.linear_velocity if key == game.VK_M: Core.CamMgrIns.MoveRight()
def create_fixs(self): if self.b1: self.b1.destroy() if self.b2: self.b2.destroy() if self.b3: self.b3.destroy() # 创建一个圆形 self.b1 = iphy2d.body(iphy2d.SPHERE, 1.0) self.b1.body_type = iphy2d.DYNAMIC_BODY # dynamic body self.b1.position = math3d.vector2(-10, 0)# 起始位置 # 创建一个矩形 self.b2 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1.0, 1.0)) self.b2.body_type = iphy2d.DYNAMIC_BODY # dynamic body self.b2.position = math3d.vector2(0, 0)# 起始位置 # 创建一个多边形 self.b3 = iphy2d.body(iphy2d.POLYGON, (math3d.vector2(-1.0, 2.0), math3d.vector2(-1, 0), math3d.vector2(0, -3), math3d.vector2(1, 0), math3d.vector2(1, 1), )) self.b3.body_type = iphy2d.DYNAMIC_BODY # dynamic body self.b3.position = math3d.vector2(10, 0)# 起始位置
def create_fixs_one_body(self): if self.b1: self.b1.destroy() if self.b2: self.b2.destroy() if self.b3: self.b3.destroy() self.b1 = iphy2d.body(iphy2d.SPHERE, 1.0) self.b1.body_type = iphy2d.DYNAMIC_BODY # dynamic body self.b1.position = math3d.vector2(0, 0)# 起始位置 # 中心处增加一个矩形 self.b1.create_fixture(iphy2d.POLYGON, math3d.vector2(1.0, 1.0), 1) # 中心处增加一个多边形 self.b1.create_fixture(iphy2d.POLYGON, (math3d.vector2(-1.0, 2.0), math3d.vector2(-1, 0), math3d.vector2(0, -3), math3d.vector2(1, 0), math3d.vector2(1, 1), ), 1) self.b2 = None self.b3 = None
def create_obj(self, is_friend, is_carrier): radius = 1 if is_carrier: radius = 2 if is_friend: body = iphy2d.body(iphy2d.SPHERE, radius) else: body = iphy2d.body(iphy2d.POLYGON, math3d.vector2(radius, radius)) # 设置category和mask if is_friend: if is_carrier: body.category = F_FRIENDLY_CARRIER body.collide_mask = F_EDGE | F_FRIENDLY_CARRIER | F_ENEMY_CARRIER else: body.category = F_FRIENDLY_AIRCRAFT body.collide_mask = F_EDGE | F_ENEMY_AIRCRAFT else: if is_carrier: body.category = F_ENEMY_CARRIER body.collide_mask = F_EDGE | F_FRIENDLY_CARRIER | F_ENEMY_CARRIER else: body.category = F_ENEMY_AIRCRAFT body.collide_mask = F_EDGE | F_FRIENDLY_AIRCRAFT return body
def Create_SEWithRDPH( resName, seName, x, y, halfW, halfH, bPolygon=True, bodyTypeStr="DB", lay_id =None): se = Create_SEWithRD( resName, seName, x, y, lay_id ) ph = Core.PhysicMgr.CreatePhyData( bPolygon, math3d.vector2(halfW, halfH), seName, bodyTypeStr, x, y ) se.AddComponetData( "PH", ph ) #创建physicData并添加到SceneElement中 return se
def Set_LinearVelocity(se, x, y): Get_PhysicDataBySE(se).body.linear_velocity = math3d.vector2(x,y)
def Cancle_Force(): print ( "Time is Over " ) se = ut.Get_SEByName("mod") pd = ut.Get_PhysicDataBySE( se ) pd.body.apply_force( math3d.vector2(0, 300), math3d.vector2( se.pos[0], se.pos[1] ) )
def start_test(self): self.destroy_bodies() xlo, xhi = -5, 5 ylo, yhi = -10, 10 # 一个小三角形 self.tr1 = iphy2d.body(iphy2d.POLYGON, (math3d.vector2(-1, 0), math3d.vector2(1.0, 0.0), math3d.vector2(0.0, 2.0))) self.tr1.body_type = iphy2d.DYNAMIC_BODY self.tr1.density = 1.0 self.tr1.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.tr1.flag = False # 用于判断是否需要销毁 # 一个大三角形 self.tr2 = iphy2d.body(iphy2d.POLYGON, (math3d.vector2(-2, 0), math3d.vector2(2.0, 0.0), math3d.vector2(0.0, 4.0))) self.tr2.body_type = iphy2d.DYNAMIC_BODY self.tr2.density = 1.0 self.tr2.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.tr2.flag = False # 用于判断是否需要销毁 # 一个小矩形 self.box1 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(1.0, 0.5)) self.box1.body_type = iphy2d.DYNAMIC_BODY self.box1.density = 1.0 self.box1.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.box1.flag = False # 用于判断是否需要销毁 # 一个大矩形 self.box2 = iphy2d.body(iphy2d.POLYGON, math3d.vector2(2.0, 1.0)) self.box2.body_type = iphy2d.DYNAMIC_BODY self.box2.density = 1.0 self.box2.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.box2.flag = False # 用于判断是否需要销毁 # 一个小圆形 self.circle1 = iphy2d.body(iphy2d.SPHERE, 1.0) self.circle1.body_type = iphy2d.DYNAMIC_BODY self.circle1.density = 1.0 self.circle1.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.circle1.flag = False # 用于判断是否需要销毁 # 一个大圆形 self.circle2 = iphy2d.body(iphy2d.SPHERE, 2.0) self.circle2.body_type = iphy2d.DYNAMIC_BODY self.circle2.density = 1.0 self.circle2.position = math3d.vector2(p2_common.randfloat_range(xlo, xhi), p2_common.randfloat_range(ylo, yhi)) self.circle2.flag = False # 用于判断是否需要销毁
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
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