上一篇的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.0class 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;};#endifGameLayer.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,(visibleSize.wIDth / 2),0); //CEIL adDWall(10,visibleSize.height,(visibleSize.height / 2) ); //left adDWall(10,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;//获得与初始坐标相反的向量,d弓的反向力 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 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); points[i]->setposition(Vec2(ballBody->Getposition().x*SCALE_RATIO,ballBody->Getposition().y*SCALE_RATIO)); //设置每一个轨迹小球应该在的地方 points[i]->setVisible(true); } world->ClearForces();//清除世界的力 world->DestroyBody(ballBody);//摧毁这个实体 //轨迹模拟需要通过原来定义的球的实体模拟获得,每次模拟之后删除}
效果图:
资源:
总结以上是内存溢出为你收集整理的Cocos2d-x 3.3 box2d测试-双轨抛物球全部内容,希望文章能够帮你解决Cocos2d-x 3.3 box2d测试-双轨抛物球所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)