Cocos2d-x 3.3 box2d测试-双轨抛物球

上一篇的cocos2dx游戏制作,在做素材优化和想把AI的FSM改为简单的状态&行为树,还有代码的优化

从刚开始什么都不懂写了一堆自己都看不下去的代码--比如把所有控制写在一个层超级臃肿,到稍微了解了下设计,大幅优化和观察者模式的引入.先坑下.尴尬

这篇是Box2d的测试,结果是实验了类似愤怒小鸟的抛物运动,并有留下轨迹,第二次抛蓄力的时候会显示当前轨迹和前一次的轨迹.

每一次按下会把球放在当前坐标,蓄力不会移动球,这些小改下就可以用了.几乎每句都有注释,应该不需要解释了

GameLayer.h

#ifndef _GAME_LAYER_H_
#define _GAME_LAYER_H_
#include "cocos2d.h"
#include <Box2D/Box2D.h>
USING_NS_CC;
#define SCALE_RATIO 32.0

class GameLayer : public Layer , public b2ContactListener
{
public:
	GameLayer();
	~GameLayer();
	virtual bool init();
	bool onTouchBegan(Touch* touch, Event* event);
	void onTouchMoved(Touch* touch, Event* event);
	void onTouchEnded(Touch* touch, Event* event);
	Sprite *ball;
	bool existBall;
	float ballX;
	float ballY;    
	Vec2 dragOffsetStart;
	Vec2 dragOffsetEnd;
	Vec2 face;   
	b2Body *ballBody;    
	b2CircleShape ballShape;
	b2BodyDef ballBodyDef;  
	void defineBall();
	b2World *world;
	float deltaTime;
	void addWall(float w,float h,float px,float py);
	CREATE_FUNC(GameLayer);
	void simulateTrajectory(b2Vec2 coord);
private:
	void update(float dt);
	Sprite *points[10];
	Sprite *propoints[10];
	float powerMultiplier;
};

#endif
GameLayer.cpp

#include "GameLayer.h"
GameLayer::GameLayer()
{
}
GameLayer::~GameLayer()
{
}
bool GameLayer::init()
{
	bool ret = false;
	do {
		CC_BREAK_IF( !Layer::init());
		auto visibleSize = Director::getInstance()->getVisibleSize();
		auto listener = EventListenerTouchOneByOne::create();
		listener->setSwallowTouches(true);

		listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this);
		listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this);
		listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this);

		_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

		existBall= false;    
		ballX = 100;
		ballY = 100;    
		ball =Sprite::create("ball.png");
		ball->setPosition(Vec2(ballX,ballY));
		this->addChild(ball);

		b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
		world = new b2World(gravity);
		addWall(visibleSize.width ,10,visibleSize.width/2,visibleSize.height);//TOP
		addWall(visibleSize.width ,10,(visibleSize.width / 2) ,0); //CEIL
		addWall(10 ,visibleSize.height ,0,(visibleSize.height / 2) ); //LEFT
		addWall(10 ,visibleSize.height ,visibleSize.width,(visibleSize.height / 2) ); //RIGHT

		for (int i = 0 ; i <10; i++)
		{
			points[i] =Sprite::create("dot.png");
			this->addChild(points[i]); 
			propoints[i] =Sprite::create("prodot.png");
			this->addChild(propoints[i]); 
		}
		powerMultiplier=10;

		this->scheduleUpdate();

		ret = true;
	} while(0);
	return ret;
}

void GameLayer::update(float dt)
{
	int positionIterations = 10; //位置迭代 
	int velocityIterations = 10;//速度迭代
	deltaTime = dt;//帧时间
	world->Step(dt, velocityIterations, positionIterations);  
	for (b2Body *body = world->GetBodyList(); body != NULL; body = body->GetNext())   //所有的body实体
		if (body->GetUserData()) //存在绑定的精灵
		{  
			Sprite *sprite = (Sprite *) body->GetUserData();  
			sprite->setPosition(Vec2(body->GetPosition().x * SCALE_RATIO,body->GetPosition().y * SCALE_RATIO));  //将其从b2Body坐标转换至Vec2坐标
			sprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(body->GetAngle())); //逆时针滚动
		}  
		world->ClearForces();//如果不取消上一个力将继续存在,导致此次状态不作为继续滚
		world->DrawDebugData();   
}
bool GameLayer::onTouchBegan(Touch* touch, Event* event)
{
	dragOffsetStart = touch->getLocation();
	if (existBall)//若是存在上一个实体球,删除它
	{        
		world->DestroyBody(ballBody);
	}
	ball->setPosition(dragOffsetStart);//将ball这个精灵设置到当前坐标
	//清除原来的球的轨迹
	return true;
}

void GameLayer::onTouchMoved(Touch* touch, Event* event)
{
	dragOffsetEnd = touch->getLocation();
	float distance = dragOffsetStart.getDistance(dragOffsetEnd);
	Vec2 direction = (dragOffsetStart - dragOffsetEnd).getNormalized();
	face = (distance*direction)*powerMultiplier/SCALE_RATIO;//获得与初始坐标相反的向量,弹弓的反向力
	GameLayer::simulateTrajectory(b2Vec2(face.x,face.y));//模拟轨迹
}

void GameLayer::onTouchEnded(Touch* touch, Event* event)
{
	existBall = true;
	GameLayer::defineBall();
	ballBody->SetLinearVelocity(b2Vec2(face.x,face.y));
	//这个实体球存在,设定属性,设置线速度
	face = Vec2::ZERO;
	//不清除,下一次按下按键不移动也会飞
		for (int i = 0; i < 10; i++)
	{
		propoints[i]->setPosition(points[i]->getPosition());
		points[i]->setVisible(false);
		propoints[i]->setVisible(true);
	}
}
void GameLayer::defineBall() {
	//设置球轨迹
	ballShape.m_radius = 45 / SCALE_RATIO;//这个实体的半径
	b2FixtureDef ballFixture;//固定的设备
	ballFixture.density=10;//密度
	ballFixture.friction=0.8f;//摩擦
	ballFixture.restitution=0.6f;//恢复原状
	ballFixture.shape=&ballShape;//塑性--这个东西的形状,半径为45/像素格子大小的一个球

	ballBodyDef.type= b2_dynamicBody;//刚体的类型,在这里设置为动态物体
	ballBodyDef.userData=ball;//映射body为ball,就是将这个物理实体绑定其那个ball精灵

	ballBodyDef.position.Set(ball->getPosition().x/SCALE_RATIO,ball->getPosition().y/SCALE_RATIO);
	//坐标设定,由于box2d中的单位是米,实际上是/PIXEL_TO_METER是一个比例尺,用来描述多少个像素对应1米,这里/SCALE_RATIO表示对应32个像素

	ballBody = world->CreateBody(&ballBodyDef);//世界创建这个ballbody
	ballBody->CreateFixture(&ballFixture);//创建设备
	ballBody->SetGravityScale(10);//设置重力规模,这个实体受重力影响
}
void GameLayer::addWall(float w,float h,float px,float py) {
	//宽,高,X坐标,Y坐标
	b2PolygonShape floorShape;//多边形
	floorShape.SetAsBox(w/ SCALE_RATIO,h/ SCALE_RATIO);//设为一个BOX
	b2FixtureDef floorFixture;
	floorFixture.density=0;
	floorFixture.friction=10;
	floorFixture.restitution=0.5;
	floorFixture.shape=&floorShape;//以上设定这个的物理属性
	b2BodyDef floorBodyDef;
	floorBodyDef.position.Set(px/ SCALE_RATIO,py/ SCALE_RATIO);//放置坐标
	b2Body *floorBody = world->CreateBody(&floorBodyDef);
	floorBody->CreateFixture(&floorFixture);
	//通过定义一个多边形确定了一个区域作为墙,没有绑定精灵图片所以是透明,如果绑定某燃烧火焰anmiation的话可以做成火墙等...
}
void GameLayer::simulateTrajectory(b2Vec2 coord)//模拟轨迹
{
	GameLayer::defineBall();//定义球的实体
	ballBody->SetLinearVelocity(b2Vec2(coord.x,coord.y));//设置线速度为X

	for (int i = 0; i < 10; i++)
	{
		world->Step(deltaTime,10,10);
		points[i]->setPosition(Vec2(ballBody->GetPosition().x*SCALE_RATIO,ballBody->GetPosition().y*SCALE_RATIO));
		//设置每一个轨迹小球应该在的地方
		points[i]->setVisible(true);
	}
	world->ClearForces();//清除世界的力
	world->DestroyBody(ballBody);//摧毁这个实体
	//轨迹模拟需要通过原来定义的球的实体模拟获得,每次模拟之后删除
}

效果图:

资源:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值