Box2d的使用

/************************************************************************/
/*场景页面                                                                     */
/************************************************************************/
#ifndef _PLAYGAMEVIEW_H_
#define _PLAYGAMEVIEW_H_
#include "cocos2d.h"
#include "ViewLayer.h"
#include "Box2D.h"
#include <vector>
#include "../controllers/Common.h"
#include "../utils/GLES-Render.h"
#include "../controllers/GameListener.h"
#include "../controllers/GameController.h"
using namespace cocos2d;
class PlayGameView:public ViewLayer,public GameListener
{
public:
	void onEnter();
	void onExit();
	bool init();
	void draw();
	virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
	virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
	virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
	static CCScene * createScene();
	void removeBody();
	CREATE_FUNC(PlayGameView);
public:
	PlayGameView();
	~PlayGameView();
private:
	b2World * world;
	b2Body  * bananaBody;//刚体(分为静态刚体和动态刚体)
	GLESDebugDraw* m_debugDraw;
	void setDebugLine();//调试信息
	void tick(float dt);
private:
	CCSprite * spriteBanana;
	b2Fixture * bananaFixture;//刚体修饰物
private:
	CCMotionStreak * streak;

private:
	std::vector<CCPoint> point;
	
};

#endif

 

#include "PlayGameView.h"
void PlayGameView::onEnter()
{
	ViewLayer::onEnter();
	setTouchEnabled(true);
	
}

void PlayGameView::onExit()
{
	ViewLayer::onExit();
}

bool PlayGameView::init()
{
	bool bRet=false;
	do
	{
		CC_BREAK_IF(! ViewLayer::init());

		CCSprite *bg=CCSprite::create(GAMERESOURCE("bg.jpg"));
		bg->setAnchorPoint(CCPointZero);
		this->addChild(bg);
		

		spriteBanana=CCSprite::create(GAMERESOURCE("banana.png"));
		this->addChild(spriteBanana,1);

		b2Vec2 gravity=b2Vec2(0,-5);
		world=new b2World(gravity);//用重力初始化一个世界


		b2BodyDef bananaBodyDef;
		bananaBodyDef.type=b2_dynamicBody;
		bananaBodyDef.userData=spriteBanana;//精灵和钢体绑定
		bananaBody=world->CreateBody(&bananaBodyDef);


		b2CircleShape circle;
		circle.m_radius=26.0/PTM_RATIO;

		b2FixtureDef bananaShapeDef;
		bananaShapeDef.shape=&circle;
		bananaShapeDef.density=1.0f;
		bananaShapeDef.friction=0.0f;
		bananaShapeDef.restitution=1.0f;

		bananaFixture=bananaBody->CreateFixture(&bananaShapeDef);

		b2Vec2 force=b2Vec2(10,30);
		bananaBody->ApplyLinearImpulse(force,bananaBodyDef.position);
		bRet=true;
		
		setDebugLine();
		this->schedule(schedule_selector(PlayGameView::tick));

		streak=CCMotionStreak::create(1, 3, 35, ccWHITE, GAMERESOURCE("banana.png"));
		addChild(streak,2);
		
	}while(false);

	return bRet;
}

void PlayGameView::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	CCSetIterator it = pTouches->begin();  
	CCTouch* touch = (CCTouch*)(*it);  

	CCPoint touchLocation = touch->getLocation();      
	//设置为拖层的位置。  
	streak->setPosition( touchLocation );
}
void PlayGameView::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
	//取得触点位置。  
	CCSetIterator it = pTouches->begin();  
	CCTouch* touch = (CCTouch*)(*it);  

	CCPoint touchLocation = touch->getLocation();      
	//设置为拖层的位置。  
	streak->setPosition( touchLocation );
	point.clear();
	point.push_back(touchLocation);//记录当前的点
}
void PlayGameView::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
	
	point.clear();
	CCLog("size:%d",point.size());

}


void PlayGameView::draw()
{
	ViewLayer::draw();
	ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
	kmGLPushMatrix();
	world->DrawDebugData();
	kmGLPopMatrix();
}

void PlayGameView::setDebugLine()
{
	m_debugDraw = new GLESDebugDraw( PTM_RATIO );
	world->SetDebugDraw(m_debugDraw);

	uint32 flags = 0;
	flags += b2Draw::e_shapeBit;
	flags += b2Draw::e_jointBit;
	flags += b2Draw::e_aabbBit;
	flags += b2Draw::e_pairBit;
	flags += b2Draw::e_centerOfMassBit;
	m_debugDraw->SetFlags(flags);
}

//让精灵的位置模拟变化
void PlayGameView::tick(float dt) {
	world->Step(dt, 10, 10);
	
	for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
		if (b->GetUserData() != NULL) {
			CCSprite *sprite = (CCSprite *)b->GetUserData();
			if(sprite==NULL)
				return;
			//showLog("x:%f,y:%f",(float)b->GetPosition().x * PTM_RATIO,(float)b->GetPosition().y * PTM_RATIO);
			float x=(float)b->GetPosition().x * PTM_RATIO;
			float y=(float)b->GetPosition().y * PTM_RATIO;
			//CCLog("x:%f,y:%f",x,y);
			sprite->setPosition(ccp(x,y));
			sprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));

			for(vector<CCPoint>::iterator iter=point.begin();iter!=point.end();iter++)
			{
				CCPoint pt=*iter;
				if(pt.x>=x-10&&pt.x<=x+10&&pt.y<=y+10&&pt.y>=y-10)
				{
					//删除刚体和精灵
					
				}

			}
		}

		
	}
	
}

void PlayGameView::removeBody()
{

	world->DestroyBody(bananaBody);
	this->removeChild(spriteBanana);
}

CCScene * PlayGameView::createScene()
{
	CCScene * scene=CCScene::create();
	CCLayer * layer=PlayGameView::create();
	scene->addChild(layer);
	return scene;
}
PlayGameView::PlayGameView()
{
	
	GameController::singleton()->addremoveBodyListener((GameListener *)this);
}
PlayGameView::~PlayGameView()
{
	GameController::singleton()->removeremoveBodyListener((GameListener *)this);
	CC_SAFE_DELETE(world);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值