关于物理引擎的使用,及其重力感应的小test

好久没写博文了,过去那些天比较忙,一直在做点别的东西,不过作为小菜的我也不忘记学习,现在跟大家分享下我的小知识吧!

首先建立个带有box2d物理引擎的项目,在头文件中加入

public:
	// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
	virtual bool init();  

	// there's no 'id' in cpp, so we recommand to return the exactly class pointer
	static cocos2d::CCScene* scene();

	// a selector callback
	void menuCloseCallback(CCObject* pSender);

	// implement the "static node()" method manually
	CREATE_FUNC(HelloWorld);

	virtual void update(float dt);  

	// 启动重力感应后,重力方向改变会回调didAccelerate  
	void didAccelerate(cocos2d::CCAcceleration* pAccelerationValue);  


	b2World *m_world;  
	cocos2d::CCSprite *m_ball;

在源文件初始化init函数中添加

		// window size  
		CCSize size = CCDirector::sharedDirector()->getWinSize();  

		// add a sprite  
		m_ball = CCSprite::create("ball.png");  
		this->addChild(m_ball);  
		m_ball->setPosition(ccp(100, 100));  

		b2Vec2 gravity = b2Vec2(0.0f, -30.0f);                              // 重力系数  
		m_world = new b2World(gravity);                                     // 创建一个有重力的世界  

		// create edge ground  
		b2BodyDef groundBodyDef;                                            // 边缘刚体定义  

		b2Body *groundBody = m_world->CreateBody(&groundBodyDef);            // 创建边缘刚体  
		b2PolygonShape groundBox;   

 
//个人理解是第一个参数表示他的半宽,第二个表示半高,第三个参数表示向量的指向,如底部及得指向底部,顶部就指向顶部,最后一个是角度有待研究,建议边界还是用b2EdgeShape比较好,第一个向量是起点,第二个向量是终点,即可
                  // bottom  
		groundBox.SetAsBox(size.width / PTM_RATIO, 0, b2Vec2(0, 0), 0);  
		groundBody->CreateFixture(&groundBox, 0);  

		// up  
		groundBox.SetAsBox(size.width / PTM_RATIO, 0, b2Vec2(0,size.height/PTM_RATIO), 0);  
		groundBody->CreateFixture(&groundBox, 0);  

		// left  
		groundBox.SetAsBox(0, size.height / PTM_RATIO, b2Vec2(0,0), 0);  
		groundBody->CreateFixture(&groundBox, 0);  

		// right  
		groundBox.SetAsBox(0,  size.height / PTM_RATIO, b2Vec2(size.width/PTM_RATIO,0), 0);  
		groundBody->CreateFixture(&groundBox, 0);  

		// create ball body an shape  
		b2BodyDef ballBodyDef;  
		ballBodyDef.type = b2_dynamicBody;                              // 刚体类型:动态  
		ballBodyDef.position.Set(100 / PTM_RATIO, 100 / PTM_RATIO);     // 设置位置  
		ballBodyDef.userData = m_ball;                                  // 绑定数据:精灵  
		b2Body *body = m_world->CreateBody(&ballBodyDef);                        // 创建刚体  

		b2CircleShape circle;                                           // 圆形定义  
		circle.m_radius = 26.0 / PTM_RATIO;                             // 半径  

		b2FixtureDef ballFixtureDef;                                    // 定制器  
		ballFixtureDef.shape = &circle;                                 // 绑定圆形  
		ballFixtureDef.density = 1.0f;                                  // 密度  
		ballFixtureDef.friction = 0.2f;                                 // 摩擦系数  
		ballFixtureDef.restitution = 0.8f;                              // 恢复:用于弹力的  

		body->CreateFixture(&ballFixtureDef);  

		this->setAccelerometerEnabled(true);    // 在层里启用重力感应  

		this->scheduleUpdate();  

//在边界那里,这种写法个人觉得比较难理解一下有种比较容易理解的
		b2EdgeShape groundBox;
		CCSize s=CCSize(bg->getContentSize().width,bg->getContentSize().height*ratio);
		// bottom
		groundBox.Set(b2Vec2(0,slingshot2->getPositionY()/PTM_RATIO/2.1), b2Vec2(s.width/PTM_RATIO,slingshot2->getPositionY()/PTM_RATIO/2.1));
		groundBody->CreateFixture(&groundBox,0);
		// top
		groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO));
		groundBody->CreateFixture(&groundBox,0);
		// left
		groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(0,0));
		groundBody->CreateFixture(&groundBox,0);
		// right
		groundBox.Set(b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,0));
		groundBody->CreateFixture(&groundBox,0);

//在update函数中写


	int velocityIterations = 8;  
	int positionIterations = 1; 
	m_world->Step(dt, velocityIterations, positionIterations);  

	for (b2Body *b = m_world->GetBodyList(); b; b = b->GetNext())  
	{  
		if (b->GetUserData() != NULL)  
		{  
			CCSprite *ballData = (CCSprite*) b->GetUserData();  
			ballData->setPosition(ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO));  
			ballData->setRotation(CC_RADIANS_TO_DEGREES(b->GetAngle()));  
		}  
	} 
//在didAccelerate函数中加入
	b2Vec2 gravity(pAccelerationValue->x * 15, pAccelerationValue->y * 15);//重力感应方向的改变,表示向量力的变化
	m_world->SetGravity(gravity);//这两句表示判断重力的感应方向的转变

上面可能要更正一下groundBox.SetAsBox(hx,hy,vector(x,y),angle);

第一个数是半长,第二个数的半高,第三个是向量,第四个数是角度

groundBodyDef.position.Set着几个数都与定义的刚体的位置有关,如果默认及位置在中心点,所以第一跟第二个数是为了获取到边界的长度,向量是位移到所要在的位置上,如刚体默认设置位置在中心点设置右边边界,及得groundBox.SetAsBox(0, winside.height/2/PTM_RATIO, b2Vec2(winside.width/2/PTM_RATIO, 0), 0);,这样就能使得上边位置拉伸到低端(第一跟第二个参数的设置),使得中心点的边拉到右侧(及第三个参数的设置),第四个参数是设置边的倾斜度。

cocos2dx 【小菜】物理引擎点击发力代码下载http://download.csdn.net/detail/five50/5663743


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值