最近使用cocosjs-3.12在写捕鱼,总结一下使用box2d:
创建物理世界:
var b2World = Box2D.Dynamics.b2World; //创建物理世界; var b2Vec2 = Box2D.Common.Math.b2Vec2; //重力矢量,因为是捕鱼,所以不需要重力; this.m_b2World=new b2World(new b2Vec2(0, 0),true); //赋值给全局函数并且设置重力; this.m_b2World.SetContinuousPhysics(true); //连续碰撞检测,避免物体穿过另一个物体;
this.initPhysics(); //碰撞检测函数--自定义;
initPhysics: function () { var b2ContactListener = Box2D.Dynamics.b2ContactListener; //检测碰撞 设置接收碰撞回调函数 var listener = new b2ContactListener; listener.BeginContact = function (contact) { //两个物体开始接触时会响应,但只调用一次 var bodyA = contact.GetFixtureA().GetBody(); var bodyB = contact.GetFixtureB().GetBody(); var spriteA = bodyA.GetUserData(); var spriteB = bodyB.GetUserData(); if (spriteA != null && spriteB != null) { //碰撞检测逻辑编写; setTimeout(function(){********************* 延时0.05秒 当正在碰撞时不能删除,此时删除将无效,稍微延时; },50); } }; //另外3方法; //void EndContact(b2Contact* contact)。分离时响应。但只调用一次。 //virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)。持续接触时响应,它会被多次调用。 //virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)。持续接触时响应,调用完preSolve后调用。 this.m_b2World.SetContactListener(listener);},
/**[[-- --添加鱼的刚体]]*/addBodyForFish:function(fishObj){ //获取当前坐标 var x = fishObj.getFishPosition().x; var y = fishObj.getFishPosition().y; //鱼的刚体ID var fishRigidId = FISH_RIGID_PREFIS+fishObj.m_fishID; //鱼是否需要旋转 var isNeedRotate = fishObj.getFishIsNeedRotate(); //是否是子鱼 都需要获取父坐标 var isNeedParentPos = fishObj.getFishIsChild(); //是否需要获取父节点的旋转角度(这个用来处理 杂乱鱼群和环绕鱼在box2d中是否需要实时旋转) var isNeedParentAngle = fishObj.getFishIsNeedParentAngle(); //定义一个对象,用于将属性传个刚体; var mTransparentId={}; mTransparentId.g_Id=fishRigidId; mTransparentId.g_FishSprite=fishObj.FishComponent; //鱼的骨骼动画; mTransparentId.g_IsNeedRotate=isNeedRotate; mTransparentId.g_NeedParentPos=isNeedParentPos; mTransparentId.g_IsNeedParentAngle=isNeedParentAngle; mTransparentId.m_previousPos=cc.p(0,0); var b2BodyDef = Box2D.Dynamics.b2BodyDef //物体定义; , b2Body = Box2D.Dynamics.b2Body //刚体; , b2FixtureDef = Box2D.Dynamics.b2FixtureDef //夹具定义 , b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape; //形状; var bodyDef = new b2BodyDef(); //创建一个刚体; bodyDef.type = b2Body.b2_dynamicBody; //刚体属性; bodyDef.position.Set(x/PTM_RATIO, y/PTM_RATIO); //将像素转换为box2d的米 PTM_RATIO=32; //将数据部署到box2d中 bodyDef.userData = mTransparentId; //添加鱼的刚体 var _body =this.m_b2World.CreateBody(bodyDef); //添加刚体; //鱼的刚体信息 多边形 var fishBodyInfo = FishCommon.getFishBodyInfo(fishObj.fishType); var _pshape = new b2PolygonShape(); _pshape.SetAsBox(0.5, 0.5); //-------m_vertices[i]改变他的值----------- _pshape.Reserve(parseInt(Common.getTableSize(fishBodyInfo["posTable"]))); //刚体形状数组设置,js特有,lua与C++方法不一样,这地方需要注意; for (var i=0;i<Common.getTableSize(fishBodyInfo["posTable"]);i++) { var x = fishBodyInfo["posTable"][i][0] * fishObj.m_fishScale; var y = fishBodyInfo["posTable"][i][1] * fishObj.m_fishScale; _pshape.m_vertices[i].Set(x /PTM_RATIO, y /PTM_RATIO); } //Common.log("addBodyForFish =================") //可以理解成多边形的包装 var shapeDef = new b2FixtureDef(); shapeDef.shape = _pshape; shapeDef.isSensor = true; shapeDef.filter.groupIndex = RIGID_FISH; //刚体分类,用于区分子弹、鱼、网; _body.CreateFixture(shapeDef); this.body_list[fishRigidId] = _body;},
/**[[-- --删除刚体 (用ID) 通过id获取刚体 ]]*/removeBodyForRigidId:function(RigidId) { var body = FishBox2DConsole.body_list[RigidId]; if (body != null) { FishBox2DConsole.m_b2World.DestroyBody(body); body=null; FishBox2DConsole.body_list[RigidId] = null; delete FishBox2DConsole.body_list[RigidId]; //js特有,如果只是等于null的话,这属性还在,只是等于null了,应该删除; }},
时间: 2024-10-21 04:49:14