Cocos2d-x_Box2D刚体自定义形状

Cocos2d-x_Box2D刚体自定义形状,第1张

概述#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "cocos-ext.h"#include "Box2D/Box2D.h"USING_NS_CC;USING_NS_CC_EXT;using namespace std;class HelloWo
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "cocos-ext.h"#include "Box2D/Box2D.h"USING_NS_CC;USING_NS_CC_EXT;using namespace std;class HelloWorld : public cocos2d::cclayer,public b2ContactListener{public:	HelloWorld();	~HelloWorld();    virtual bool init();      static cocos2d::CCScene* scene();	// 重写生命周期函数	virtual voID onEnter();	virtual voID onEnterTransitionDIDFinish();	virtual voID onExit();	// 重写CCTargettouchDelegate	virtual bool cctouchBegan(CCtouch *ptouch,CCEvent *pEvent);	virtual voID cctouchended(CCtouch *ptouch,CCEvent *pEvent);	virtual voID cctouchmoved(CCtouch *ptouch,CCEvent *pEvent);	virtual voID cctouchCancelled(CCtouch *ptouch,CCEvent *pEvent);	// 重写update回调函数	virtual voID update(float delta);	// 重写Bos2D监听函数	virtual voID BeginContact(b2Contact* contact);    CREATE_FUNC(HelloWorld);private:	b2World *world;	b2Body* groundBody;private:	voID initPhysics();	voID addNewSpriteAtposition(CCPoint &pt);};#endif // __HELLOWORLD_SCENE_H__


#include "HelloWorldScene.h"#include "Layer.h"#define PTM_RATIO 30USING_NS_CC;HelloWorld::HelloWorld(){}HelloWorld::~HelloWorld(){	CC_SAFE_DELETE(world);}CCScene* HelloWorld::scene(){	CCScene *scene = CCScene::create();	HelloWorld *layer = HelloWorld::create();	scene->addChild(layer);	return scene;}bool HelloWorld::init(){	if (!cclayer::init())	{		return false;	}	CCSize winSize = CCDirector::sharedDirector()->getWinSize();	CCSprite *sprite = CCSprite::create("HelloWorld.png");	sprite->setposition(ccp(winSize.wIDth/2.0,winSize.height/2.0));	this->addChild(sprite);	// 初始化物理引擎	this->initPhysics();	//开始游戏循环	this->scheduleUpdate();	return true;}voID HelloWorld::initPhysics(){	CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();	//重力参数	b2Vec2 gravity;	gravity.Set(0.0f,-10.0f);	//创建世界	world = new b2World(gravity);	// 允许物体是否休眠	world->SetAllowSleePing(true);	// 开启连续物理测试	world->SetContinuousPhysics(true);	// 设置物理碰撞的监听代理	world->SetContactListener(this);	//地面物体定义	b2BodyDef groundBodyDef;	// 左下角	groundBodyDef.position.Set(winSize.wIDth / 2.0 / PTM_RATIO,winSize.height / 2.0 / PTM_RATIO);	// 设置为静态物体	groundBodyDef.type = b2_staticBody;	// 创建地面物体	groundBody = world->CreateBody(&groundBodyDef);	// 定义一个有边的形状	b2polygonShape groundBox;	// 底部	groundBox.SetAsBox(winSize.wIDth / 2 / PTM_RATIO,b2Vec2(0,-winSize.height / 2 / PTM_RATIO),0);	//使用夹具固定形状到物体上	groundBody->CreateFixture(&groundBox,0);	// 顶部	groundBox.SetAsBox(winSize.wIDth / 2 / PTM_RATIO,winSize.height / 2 / PTM_RATIO),0);	groundBody->CreateFixture(&groundBox,0);	// 左边	groundBox.SetAsBox(0,winSize.height / 2 / PTM_RATIO,b2Vec2(-winSize.wIDth / 2 / PTM_RATIO,0),0);	// 右边	groundBox.SetAsBox(0,b2Vec2(winSize.wIDth / 2 / PTM_RATIO,0);}voID HelloWorld::addNewSpriteAtposition(CCPoint &pt){	cclOG("Add sprite x:%0.2f y:%02.f",pt.x,pt.y);	//创建物理引擎精灵对象	CCSprite *sprite = CCSprite::create("BoxA.png");	sprite->setposition(pt);	this->addChild(sprite);	float sprWIDth = sprite->getContentSize().wIDth;	float spriHeight = sprite->getContentSize().height;	//物体定义	b2BodyDef bodyDef;	bodyDef.type = b2_dynamicBody;	bodyDef.position.Set(pt.x / PTM_RATIO,pt.y / PTM_RATIO);	bodyDef.userData = sprite;	b2Body *body = world->CreateBody(&bodyDef);	// 定义2米见方的盒子形状	b2polygonShape dynamicBox;	// 注意这里的宽度和高度都是精灵的一半再除以PTM_RATIO	dynamicBox.SetAsBox(sprWIDth / 2 / PTM_RATIO,spriHeight / 2 / PTM_RATIO);	// 夹具定义	b2FixtureDef fixtureDef;	//设置夹具的形状	fixtureDef.shape = &dynamicBox;	//设置密度	fixtureDef.density = 1.0f;	//设置摩擦系数	fixtureDef.friction = 0.3f;	//设置d力系数	fixtureDef.restitution = 0.5f;	//使用夹具固定形状到物体上  	body->CreateFixture(&fixtureDef);}voID HelloWorld::update(float delta){	world->Step(delta,8,3);	for (b2Body* b = world->GetbodyList(); b; b = b->GetNext())	{		if (b->GetType() == b2_dynamicBody)		{			if (b->GetUserData() != NulL)			{				CCSprite* sprite = (CCSprite*)b->GetUserData();				sprite->setposition(ccp(b->Getposition().x * PTM_RATIO,b->Getposition().y * PTM_RATIO));				sprite->setRotation(-1 * CC_radians_TO_degrees(b->GetAngle()));			}		}	}}voID HelloWorld::BeginContact(b2Contact* contact){	if (contact->GetFixtureA()->Getbody() == groundBody || contact->GetFixtureB()->Getbody() == groundBody)	{		cclOG("检测到物理碰撞");	}}voID HelloWorld::onEnter(){	cclOG("HelloWorld::onEnter");	CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,-128,false);	cclayer::onEnter();}voID HelloWorld::onEnterTransitionDIDFinish(){	cclOG("HelloWorld::onEnterTransitionDIDFinish");	cclayer::onEnterTransitionDIDFinish();}voID HelloWorld::onExit(){	cclOG("HelloWorld::onExit");	CCDirector::sharedDirector()->gettouchdispatcher()->removeDelegate(this);	cclayer::onExit();}bool HelloWorld::cctouchBegan(CCtouch *ptouch,CCEvent *pEvent){	CCPoint pt = ptouch->getLocation();	this->addNewSpriteAtposition(pt);	return true;}voID HelloWorld::cctouchended(CCtouch *ptouch,CCEvent *pEvent){}voID HelloWorld::cctouchmoved(CCtouch *ptouch,CCEvent *pEvent){}voID HelloWorld::cctouchCancelled(CCtouch *ptouch,CCEvent *pEvent){}
总结

以上是内存溢出为你收集整理的Cocos2d-x_Box2D刚体自定义形状全部内容,希望文章能够帮你解决Cocos2d-x_Box2D刚体自定义形状所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1053421.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存