1. 添加依赖包

  1. cocos2d-android
  2. jbox2d-library-2.1.2

2. 创建物理世界

	/**
	 * 创建物理世界的函数
	 */
	private void createPhysicalWorld()
	{
		gravity = new Vec2(0.0f, -5.0f); // 设置重力向量
		world = new World(gravity, true); // 创建物理世界
		
		// 为world添加碰撞检测监听器
		world.setContactListener(new MyContactListener());

		useJBox2D = new UseJBox2D(world, RATE);
		float groundX = CCDirector.sharedDirector().winSize().width / 2;
		float groundY = 59.0f;
		float groundWidth = CCDirector.sharedDirector().winSize().width;
		float groundHeihgt = 1.0f;
		float groundFriction = 2.0f;
		// 创建地板刚体
		useJBox2D.createGroundBody(groundX, groundY, 
				groundWidth, groundHeihgt, groundFriction);
	}

3. 实现效果

 

 4. 完整源码下载地址:https://pan.baidu.com/s/1gQihcPQLMz2NsT3dO4R0EQ?pwd=jhc3
提取码:jhc3

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐