I want to create game like Roller Coaster and try to make Train for it..
I have tried created train with boxbody
code for train :
-(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;
}
}
I also tried to Create Joint Between trains and try to move it with 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));
}
}
But my problem is that when i jump the train i apply force to first engine but my whole train not jumped perfectly than i tried to apply force to whole train one by one all bogie in some delay but also i not get perfect so please give me any related code to create Train and move and jump it perfectly..