Cocos物理世界(box2)

Cocos物理世界(box2),第1张

概述Box2D 让游戏看起更加真实,比如东西掉下来等等,不用去Moveto模拟 #define PTM_RATIO 32.0fbool T06Box2D::init(){ Layer::init(); // 创建世界 // b2Vec2 gravity(0, -9.8f); b2Vec2 gravity(0, 0.0f);//x,y方向得到力 _world = new b2Worl

Box2D

让游戏看起更加真实,比如东西掉下来等等,不用去Moveto模拟

#define PTM_RATIO 32.0fbool T06Box2D::init(){	Layer::init();	// 创建世界	// b2Vec2 gravity(0,-9.8f);	b2Vec2 gravity(0,0.0f);//x,y方向得到力	_world = new b2World(gravity);	{		b2BodyDef def;		def.type = b2_dynamicBody; //类型设为动态的		//重力系统的位置跟我们屏幕位置不一样,所以我们要设置一个比例关系		def.position.Set(winSize.wIDth / 2/PTM_RATIO,winSize.height / 2/PTM_RATIO);		//创建物体,创建一个物体都是在世界里面创建的		b2Body* body = _world->CreateBody(&def);		// 让body受力		body->SetlinearVeLocity(b2Vec2(10,20));				Sprite* sprite = Sprite::create("Closenormal.png");		addChild(sprite);		sprite->setposition(body->Getposition().x*PTM_RATIO,body->Getposition().y*PTM_RATIO);		// 设置body的形状,让它和sprite相一致		b2CircleShape shape;		shape.m_radius = sprite->getContentSize().wIDth / 2 / PTM_RATIO;//圆的半径		b2Fixture* fixture = body->CreateFixture(&shape,1.0f /*密度系数*/);//密度系数0---1.		fixture->SetFriction(0.0f);  // 摩擦系数 0--1, 0代表没有摩擦		fixture->SetRestitution(1.0f); // d性系数  		// 关联body和精灵		body->SetUserData(sprite);	}	// 加个地板	{		b2BodyDef def;		// def.position.Set(0,0);		b2Body* body = _world->CreateBody(&def);		b2EdgeShape shape;//边界形状		shape.Set(b2Vec2(0,0),b2Vec2(winSize.wIDth / PTM_RATIO,0));		b2Fixture* fixture = body->CreateFixture(&shape,1.0f);		fixture->SetFriction(0.0f);  // 摩擦系数		fixture->SetRestitution(1.0f); // d性系数, 1没有消耗,每次d起高度都一样,越小越有消耗。	}	// 加个天花板	{		b2BodyDef def;		def.position.Set(0,winSize.height/PTM_RATIO);		b2Body* body = _world->CreateBody(&def);		b2EdgeShape shape;		shape.Set(b2Vec2(0,1.0f);		fixture->SetFriction(0.0f);  // 摩擦系数		fixture->SetRestitution(1.0f); // d性系数	}	// 左挡板	{		b2BodyDef def;		//def.position.Set(0,winSize.height / PTM_RATIO);		b2Body* body = _world->CreateBody(&def);		b2EdgeShape shape;		shape.Set(b2Vec2(0,b2Vec2(0,winSize.height/PTM_RATIO));		b2Fixture* fixture = body->CreateFixture(&shape,1.0f);		fixture->SetFriction(0.0f);  // 摩擦系数		fixture->SetRestitution(1.0f); // d性系数	}	// 右挡板	{		b2BodyDef def;		def.position.Set(winSize.wIDth / PTM_RATIO,0);		b2Body* body = _world->CreateBody(&def);		b2EdgeShape shape;		shape.Set(b2Vec2(0,winSize.height / PTM_RATIO));		b2Fixture* fixture = body->CreateFixture(&shape,1.0f);		fixture->SetFriction(0.0f);  // 摩擦系数		fixture->SetRestitution(1.0f); // d性系数	}	// 球拍	{		b2BodyDef def;		def.position.Set(winSize.wIDth / 2 / PTM_RATIO,winSize.height / 4 / PTM_RATIO);				b2Body* body = _world->CreateBody(&def);		_bat = body;		Sprite* sprite = Sprite::create("bat.png");		body->SetUserData(sprite);		addChild(sprite);		sprite->setposition(body->Getposition().x*PTM_RATIO,body->Getposition().y*PTM_RATIO);		Size batSize = Size(100,30);		Size content = sprite->getContentSize();		sprite->setScale(batSize.wIDth / content.wIDth,batSize.height / content.height);		b2polygonShape shape;//多边形		shape.SetAsBox(batSize.wIDth/2/PTM_RATIO,batSize.height/2/PTM_RATIO);		b2Fixture* fixture = body->CreateFixture(&shape,1.0f);		fixture->SetRestitution(1.0f);		fixture->SetFriction(0.0f);  // 摩擦系数		fixture->SetRestitution(1.0f); // d性系数		// touch		EventListenertouchOneByOne* ev = EventListenertouchOneByOne::create();		ev->ontouchBegan = [](touch*,Event*){return true; };		ev->ontouchmoved = [&](touch* touch,Event*){  //注意这里要加&哦,不然不能访问_bat.			float dx = touch->getDelta().x / PTM_RATIO;			b2Vec2 pos = _bat->Getposition();			pos.x += dx;			_bat->Settransform(pos,0);		};		_eventdispatcher->addEventListenerWithSceneGraPHPriority(ev,this);			}	scheduleUpdate();	return true;}voID T06Box2D::update(float dt){	// 时间在流逝	_world->Step(dt,8,1);	// 遍历这个世界的body	b2Body* body = _world->GetbodyList();	while (body)	{		// 设置body相关的精灵的位置		Sprite* sprite = (Sprite*)body->GetUserData();		if (sprite)		{			sprite->setposition(body->Getposition().x*PTM_RATIO,body->Getposition().y*PTM_RATIO);			//设置旋转,比如有时物体碰撞要旋转,精灵跟着旋转			sprite->setRotation(body->GetAngle()*180.0/M_PI);//弧度和角度的转换		}		body = body->GetNext();	}}
总结

以上是内存溢出为你收集整理的Cocos物理世界(box2)全部内容,希望文章能够帮你解决Cocos物理世界(box2)所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1063856.html

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

发表评论

登录后才能评论

评论列表(0条)

保存