内容介绍:
3.0以后最Box2d和chipmunk这两个物理引擎进行了封装,使用起来非常的便利。 offset:重心点 velocity:速度 dadamping:阻尼 rerestitution:弹力 mamaterial:材质 mass:质量 moment:力矩,当他碰到另一个刚体时候,会产生一股扭转力,做旋转运动 body:刚体,表示物理世界中的抽象实体,附带有物理属性 shape:刚体的形状,同一个body可以附加多个shape该shape们不会发生碰撞 joint:关节,可以连接>=2个刚体 【physicsBody】 /**创建一个bodymass和moment为默认值*/ staticPhysicsBody*create(); /**创建一个质量为mass的bodymoment为默认值.*/ staticPhysicsBody*create(floatmass); /**创建一个body并为mass和moment赋值*/ staticPhysicsBody*create(floatmass,floatmoment); /**创建一个shape为圆形的body*/ staticPhysicsBody*createCircle(floatradius,constPhysicsMaterial&material=PHYSICSBODY_MATERIAL_DEFAULT,constPoint&offset=Point::ZERO); /**创建一个shape为四边形的body.*/ staticPhysicsBody*createBox(constSize&size,constPoint&offset=Point::ZERO); /**创建一个动态多边形刚体,多边形的顶点存放在Pointarray[]中示例:Pointarray[]={point(1,1),point(2,2)}注意:顶点必须按顺时针存放,并且图形为凸状,不能是凹的*/ staticPhysicsBody*createPolygon(constPoint*points,intcount,constPoint&offset=Point::ZERO); /**创建一个静态的线状刚体.*/ staticPhysicsBody*createEdgeSegment(constPoint&a,constPoint&b,floatborder=1); /**创建一个静态四边形刚体.*/ staticPhysicsBody*createEdgeBox(constSize&size,floatborder=1,constPoint&offset=Point::ZERO); /**创建一个静态多边形刚体.*/ staticPhysicsBody*createEdgePolygon(constPoint*points,floatborder=1); /**创建一个链条状刚体*/ staticPhysicsBody*createEdgeChain(constPoint*points,floatborder=1); /* 添加一个shapemass和moment赋值true */ virtualPhysicsShape*addShape(PhysicsShape*shape,booladdMassAndMoment=true); /*通过shape移除shape*/ voidremoveShape(PhysicsShape*shape,boolreduceMassAndMoment=true); /*通过tag移除shape*/ voidremoveShape(inttag,boolreduceMassAndMoment=true); /*移除body的所有shape*/ voidremoveAllShapes(boolreduceMassAndMoment=true); /*获取body的shapes*/ inlineconstVector<PhysicsShape*>&getShapes()const{return_shapes;} /*获取第一个shape.*/ inlinePhysicsShape*getFirstShape()const{return_shapes.size()>=1?_shapes.at(0):nullptr;} /*通过tag从body中获取shape*/ PhysicsShape*getShape(inttag)const; /**给body施加一个循序渐进的力,物体会受加速度影响,越来越快,像火车一样*/ virtualvoidapplyForce(constVect&force); /**offset为偏移度指碰到物体时body旋转偏移一般设为默认值值越大旋转越快偏移角度越大*/ virtualvoidapplyForce(constVect&force,constPoint&offset); /**重置施加在body上的力清0了.*/ virtualvoidresetForces(); /**不会产生力,直接与body的速度叠加产生新的速度.*/ virtualvoidapplyImpulse(constVect&impulse); /**Appliesacontinuousforcetobody.*/ virtualvoidapplyImpulse(constVect&impulse,constPoint&offset); /**施加一个扭转力到刚体上就像向前翻转一块大石头一样.*/ virtualvoidapplyTorque(floattorque); /**设置刚体的速度*/ virtualvoidsetVelocity(constVect&velocity); /**获取刚体的速度*/ virtualPointgetVelocity(); /**设置刚体角速度就是单位时间内转动的弧度*/ virtualvoidsetAngularVelocity(floatvelocity); /**通过一个局部点获取刚体的角速度*/ virtualPointgetVelocityAtLocalPoint(constPoint&point); /**通过世界点获取刚体的角速度*/ virtualPointgetVelocityAtWorldPoint(constPoint&point); /**获取刚体的角速度*/ virtualfloatgetAngularVelocity(); /**设置速度的极限值*/ virtualvoidsetVelocityLimit(floatlimit); /**获取速度的极限值*/ virtualfloatgetVelocityLimit(); /**设置角速度极限值*/ virtualfloatgetAngularVelocityLimit(); /**从world中移除body*/ voidremoveFromWorld(); /**获取world*/ inlinePhysicsWorld*getWorld()const{return_world;} /**获取body的所有关节*/ inlineconststd::vector<PhysicsJoint*>&getJoints()const{return_joints;} /**取得body设置的sprite.*/ inlineNode*getNode()const{return_node;} /** *Amaskthatdefineswhichcategoriesthisphysicsbodybelongsto. *Everyphysicsbodyinascenecanbeassignedtoupto32differentcategories,eachcorrespondingtoabitinthebitmask.Youdefinethemaskvaluesusedinyourgame.InconjunctionwiththecollisionBitMaskandcontactTestBitMaskproperties,youdefinewhichphysicsbodiesinteractwitheachotherandwhenyourgameisnotifiedoftheseinteractions. *Thedefaultvalueis0xFFFFFFFF(allbitsset). */没搞懂 voidsetCategoryBitmask(intbitmask); /** *Amaskthatdefineswhichcategoriesofbodiescauseintersectionnotificationswiththisphysicsbody. *Whentwobodiessharethesamespace,eachbody’scategorymaskistestedagainsttheotherbody’scontactmaskbyperformingalogicalANDoperation.Ifeithercomparisonresultsinanon-zerovalue,anPhysicsContactobjectiscreatedandpassedtothephysicsworld’sdelegate.Forbestperformance,onlysetbitsinthecontactsmaskforinteractionsyouareinterestedin. *Thedefaultvalueis0x00000000(allbitscleared). */ voidsetContactTestBitmask(intbitmask); /** *Amaskthatdefineswhichcategoriesofphysicsbodiescancollidewiththisphysicsbody. *Whentwophysicsbodiescontacteachother,acollisionmayoccur.Thisbody’scollisionmaskiscomparedtotheotherbody’scategorymaskbyperformingalogicalANDoperation.Iftheresultisanon-zerovalue,thenthisbodyisaffectedbythecollision.Eachbodyindependentlychooseswhetheritwantstobeaffectedbytheotherbody.Forexample,youmightusethistoavoidcollisioncalculationsthatwouldmakenegligiblechangestoabody’svelocity. *Thedefaultvalueis0xFFFFFFFF(allbitsset). */ voidsetCollisionBitmask(intbitmask); /**getthecategorybitmask*/ inlineintgetCategoryBitmask()const{return_categoryBitmask;} /**getthecontacttestbitmask*/ inlineintgetContactTestBitmask()const{return_contactTestBitmask;} /**getthecollisionbitmask*/ inlineintgetCollisionBitmask()const{return_collisionBitmask;} /** *setthegroupofbody *Collisiongroupsletyouspecifyanintegralgroupindex.Youcanhaveallfixtureswiththesamegroupindexalwayscollide(positiveindex)ornevercollide(negativeindex) *ithavehighprioritythanbitmasks */ voidsetGroup(intgroup); /**getthegroupofbody*/ inlineintgetGroup()const{return_group;} /**获取body坐标*/ PointgetPosition()const; /**获取body角度.*/ floatgetRotation()const; /**判断body是否静止*/ inlineboolisDynamic()const{return_dynamic;} /**设置body状态false为静态true为动态*/ voidsetDynamic(booldynamic); /**设置mass值如果需要增加mass有addmass方法不要在这里做加减*/ voidsetMass(floatmass); /**取得mass.*/ inlinefloatgetMass()const{return_mass;} /** *@briefaddmasstobody. *if_mass(massofthebody)==PHYSICS_INFINITY,itremains. *ifmass==PHYSICS_INFINITY,_masswillbePHYSICS_INFINITY. *ifmass==-PHYSICS_INFINITY,_masswillnotchange. *ifmass+_mass<=0,_masswillequaltoMASS_DEFAULT(1.0) *otherwise,mass=mass+_mass; */增加质量 voidaddMass(floatmass); /** *@briefsetthebodymomentofinertia. *@noteifyouneedadd/subtractmomenttobody,don'tusesetMoment(getMoment()+/-moment),becausethemomentofbodymaybeequaltoPHYSICS_INFINITY,itwillcausesomeunexpectedresult,pleaseuseaddMoment()instead. */设置力矩 voidsetMoment(floatmoment); /**获取惯性的力矩.*/ inlinefloatgetMoment(floatmoment)const{return_moment;} /** *@briefaddmomentofinertiatobody. *if_moment(momentofthebody)==PHYSICS_INFINITY,itremains. *ifmoment==PHYSICS_INFINITY,_momentwillbePHYSICS_INFINITY. *ifmoment==-PHYSICS_INFINITY,_momentwillnotchange. *ifmoment+_moment<=0,_momentwillequaltoMASS_DEFAULT(1.0) *otherwise,moment=moment+_moment; */增加力矩 voidaddMoment(floatmoment); /**取得线性阻尼*/ inlinefloatgetLinearDamping()const{return_linearDamping;} /** *设置阻尼值 *它用来模拟body在气体或者液体中的摩擦力 *取值范围是0.0fto1.0f. */ inlinevoidsetLinearDamping(floatdamping){_linearDamping=damping;} /**获取角阻尼*/ inlinefloatgetAngularDamping()const{return_angularDamping;} /** *设置角阻尼 *它用来模拟body在气体或者液体中的角阻尼 *thevalueis0.0fto1.0f. */ inlinevoidsetAngularDamping(floatdamping){_angularDamping=damping;} /**判断body是否是休息状态*/ boolisResting()const; /** *判断body能否在物理世界中模拟 */ inlineboolisEnabled()const{return_enable;} /** 设置body能否在物理世界中模拟 */ voidsetEnable(boolenable); /**whetherthebodycanrotation*/ inlineboolisRotationEnabled()const{return_rotationEnable;} /**设置能否旋转*/ voidsetRotationEnable(boolenable); /**判断body是否受引力影响*/ inlineboolisGravityEnabled()const{return_gravityEnable;} /**设置body是否受引力影响*/ voidsetGravityEnable(boolenable); /**取得body的tag值*/ inlineintgetTag()const{return_tag;} /**设置bodytag值*/ inlinevoidsetTag(inttag){_tag=tag;} /**转换世界点到局部点类似世界坐标和局部坐标的转换*/ Pointworld2Local(constPoint&point); /**转换局部坐标到世界坐标*/ Pointlocal2World(constPoint&point); 【PhysicsShape】 /**通过shape取得body*/ inlinePhysicsBody*getBody()const{return_body;} /**返回shape的类型*/ inlineTypegetType()const{return_type;} /**返回shape的面积*/ inlinefloatgetArea()const{return_area;} /**取得moment力矩*/ inlinefloatgetMoment()const{return_moment;} /**Setmoment,itwillchangethebody'smomentthisshapeattaches*/ voidsetMoment(floatmoment);//设置力矩 inlinevoidsetTag(inttag){_tag=tag;}//设置标签tag inlineintgetTag()const{return_tag;}//取得tag标签 /**获取质量*/ inlinefloatgetMass()const{return_mass;} /**Setmass,itwillchangethebody'smassthisshapeattaches*/ voidsetMass(floatmass);//设置质量 inlinefloatgetDensity()const{return_material.density;}//density为密度 voidsetDensity(floatdensity);//获取密度 inlinefloatgetRestitution()const{return_material.restitution;}//获取弹性 voidsetRestitution(floatrestitution);//设置弹性 inlinefloatgetFriction()const{return_material.friction;}//friction为摩擦力 voidsetFriction(floatfriction);//设置摩擦力 constPhysicsMaterial&getMaterial()const{return_material;}//Material为材质 voidsetMaterial(constPhysicsMaterial&material);设置材质 /**返回默认力矩其值为0*/ virtualfloatcalculateDefaultMoment(){return0.0f;} /**取得重心初始值为zero*/ virtualPointgetOffset(){returnPoint::ZERO;} /**获取shape的重心点*/ virtualPointgetCenter(){returngetOffset();} /**shape是否包含该点*/ boolcontainsPoint(constPoint&point)const; /**改变重心点*/ staticvoidrecenterPoints(Point*points,constPoint¢er=Point::ZERO); /**取得多边形的重心点*/ staticPointgetPolyonCenter(constPoint*points,intcount); /** *Amaskthatdefineswhichcategoriesthisphysicsbodybelongsto. *Everyphysicsbodyinascenecanbeassignedtoupto32differentcategories,youdefinewhichphysicsbodiesinteractwitheachotherandwhenyourgameisnotifiedoftheseinteractions. *Thedefaultvalueis0xFFFFFFFF(allbitsset). */ inlinevoidsetCategoryBitmask(intbitmask){_categoryBitmask=bitmask;} inlineintgetCategoryBitmask()const{return_categoryBitmask;} /** *Amaskthatdefineswhichcategoriesofbodiescauseintersectionnotificationswiththisphysicsbody. *Whentwobodiessharethesamespace,onlysetbitsinthecontactsmaskforinteractionsyouareinterestedin. *Thedefaultvalueis0x00000000(allbitscleared). */ inlinevoidsetContactTestBitmask(intbitmask){_contactTestBitmask=bitmask;} inlineintgetContactTestBitmask()const{return_contactTestBitmask;} /** *Amaskthatdefineswhichcategoriesofphysicsbodiescancollidewiththisphysicsbody. *Whentwophysicsbodiescontacteachother,youmightusethistoavoidcollisioncalculationsthatwouldmakenegligiblechangestoabody’svelocity. *Thedefaultvalueis0xFFFFFFFF(allbitsset). */ inlinevoidsetCollisionBitmask(intbitmask){_collisionBitmask=bitmask;} inlineintgetCollisionBitmask()const{return_collisionBitmask;} voidsetGroup(intgroup); inlineintgetGroup(){return_group;}原文链接:https://www.f2er.com/cocos2dx/343267.html