我是Cocos2dx v3.x的新手。我正在开发一款基本的赛车游戏,其中汽车可以在连续的滚动轨道内移动,这是直线和小曲线,如#34; Road Fighter"在谷歌播放商店中的游戏。为了移动汽车,我正在使用设备加速度计并调整车身的速度acc。对于路径体,我正在使用EdgeChain形状的5点,我已经在下面显示,这样它使得汽车运动和汽车的界限区域不应超过它。但是使用setVelocity功能,我的车身就到了外面我还设置了CollisionBitmask和ContactTestBitmask,检测到碰撞,甚至汽车的物理体也越过了路径的边链体。
auto *sprite = Sprite::create("Road.jpg");
sprite->setPosition(Vec2(winSize.width/2,winSize.height/2));
sprite->setScaleX(rX);
sprite->setScaleY(rY);
this->addChild(sprite, 0);
auto edge_body=PhysicsBody::createEdgeChain(vec ,5 ,PHYSICSBODY_MATERIAL_DEFAULT,3);
edge_body->setDynamic(false);
edge_body->addShape(PhysicsShapeEdgeChain::create(vec3, 5));
sprite->setPhysicsBody(edge_body);
edge_body->setCollisionBitmask(1);
edge_body->setContactTestBitmask(true);
//where vec and vec3 are vectors of 5 points acc to track boundary.
auto *mycar=Sprite::create(car_png_name);
mycar->setPosition(Point(winSize.width/2,winSize.height*0.35));
addChild(mycar,2);
mycar_body=PhysicsBody::createBox(mycar->getContentSize(),PhysicsMaterial(0,1,0));
mycar->setPhysicsBody(mycar_body);
mycar_body->setRotationEnable(false);
mycar_body->setCollisionBitmask(2);
mycar_body->setContactTestBitmask(true);
// DEVICE ACCELEROMETER
Device::setAccelerometerEnabled(true);
auto listener= EventListenerAcceleration::create (CC_CALLBACK_2(HelloWorld::OnAcceleration,this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
auto contact_listener=EventListenerPhysicsContact::create();
contact_listener->onContactBegin=CC_CALLBACK_1(HelloWorld::onContactBegin, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contact_listener, this);
void HelloWorld::OnAcceleration(cocos2d::Acceleration *acc, cocos2d::Event *event)
{
#define KFILTERINGFACTOR 0.7
#define KRESTACCELX 0
#define KsquirrelMAXPOINTSPERSEC (winSize.width*0.5)
#define KMAXDIFFX 0.15
double rollingX=0;
acc->y = acc->x;
rollingX = (acc->y * KFILTERINGFACTOR) + (rollingX * (1.0 - KFILTERINGFACTOR));
float accelX = acc->y - rollingX;
float accelDiff = accelX - KRESTACCELX;
float accelFraction = accelDiff / KMAXDIFFX;
carspeed = KsquirrelMAXPOINTSPERSEC * accelFraction*1.5;
if(mycar_body!=NULL)
mycar_body->setVelocity(Vec2(carspeed*1.5,0));
}
我在Cocos2dxv3.4中遇到过这个问题。同样的逻辑在Cocos2dxv2.2.6和box2d b2body中工作得很好。我没有得到我错在哪里或者在其他方面如何,我可以在滚动内用加速度计移动我的车border.Can any Suggest?
提前致谢。