我想创造像过山车这样的游戏并尝试为它制作火车..
我试过用 boxbody 创建火车
火车代码:
-(void)addTrainAtPos:(CGPoint)pos:(int)n:(CCLayer *)Layer
{
b2BodyDef BodyDef;
BodyDef.type = b2_dynamicBody;
b2PolygonShape box;
b2FixtureDef fixtureDef;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
box.SetAsBox(ptm(15), ptm(10));
fixtureDef.shape = &box;
b2Body *Body;
b2Body *LastBody;
b2DistanceJointDef Djoint;
BodyDef.position.Set(ptm(pos.x - (0 * (30 + Spacing))), ptm(pos.y));
BodyDef.fixedRotation = true;
Body = world->CreateBody(&BodyDef);
Body->CreateFixture(&fixtureDef);
LastBody = Body;
for (int i=1; i<n; i++)
{
BodyDef.position.Set(ptm(pos.x - (i * (30 + Spacing))), ptm(pos.y));
BodyDef.fixedRotation = true;
Body = world->CreateBody(&BodyDef);
Body->CreateFixture(&fixtureDef);
//Djoint.Initialize(LastBody, Body, LastBody->GetWorldCenter(), Body->GetWorldCenter());
//world->CreateJoint(&Djoint);
//LastBody = Body;
}
}
我还尝试在火车之间创建关节并尝试使用 continue LinearVelocity 移动它
-(void)MoveTrain
{
for(NSValue *Val in Bodies)
{
b2Body *Body = (b2Body *)[Val pointerValue];
b2Vec2 old = Body->GetLinearVelocity();
if(old.x < 20)
Body->SetLinearVelocity(b2Vec2(Speed, old.y));
}
}
但我的问题是,当我跳火车时,我对第一个引擎施力,但我的整个火车跳得并不完美,而不是我试图对整个火车一个接一个地施力,所有转向架都有一些延迟,但我也没有变得完美,所以请给我任何相关代码来创建火车和移动并完美地跳跃它..