Thursday, May 5, 2011

make car c++

{ // car body
b2PolygonDef poly1, poly2;

// bottom half
poly1.vertexCount = 5;
poly1.vertices[4].Set(-2.2f,-0.74f);
poly1.vertices[3].Set(-2.2f,0);
poly1.vertices[2].Set(1.0f,0);
poly1.vertices[1].Set(2.2f,-0.2f);
poly1.vertices[0].Set(2.2f,-0.74f);
poly1.filter.groupIndex = -1;

poly1.density = 20.0f;
poly1.friction = 0.68f;
poly1.filter.groupIndex = -1;

// top half
poly2.vertexCount = 4;
poly2.vertices[3].Set(-1.7f,0);
poly2.vertices[2].Set(-1.3f,0.7f);
poly2.vertices[1].Set(0.5f,0.74f);
poly2.vertices[0].Set(1.0f,0);
poly2.filter.groupIndex = -1;

poly2.density = 5.0f;
poly2.friction = 0.68f;
poly2.filter.groupIndex = -1;

b2BodyDef bd;
bd.position.Set(-35.0f, 5.8f);

m_vehicle = m_world->CreateBody(&bd);
m_vehicle->CreateShape(&poly1);
m_vehicle->CreateShape(&poly2);
m_vehicle->SetMassFromShapes();
}

{ // vehicle wheels
b2CircleDef circ;
circ.density = 40.0f;
circ.radius = 0.38608f;
circ.friction = 0.8f;
circ.filter.groupIndex = -1;

b2BodyDef bd;
bd.allowSleep = false;
bd.position.Set(-33.8f, 5.0f);

m_rightWheel = m_world->CreateBody(&bd);
m_rightWheel->CreateShape(&circ);
m_rightWheel->SetMassFromShapes();

bd.position.Set(-36.2f, 5.0f);
m_leftWheel = m_world->CreateBody(&bd);
m_leftWheel->CreateShape(&circ);
m_leftWheel->SetMassFromShapes();
}

{ // join wheels to chassis
b2Vec2 anchor;
b2RevoluteJointDef jd;
jd.Initialize(m_vehicle, m_leftWheel, m_leftWheel->GetWorldCenter());
jd.collideConnected = false;
jd.enableMotor = true;
jd.maxMotorTorque = 10.0f;
jd.motorSpeed = 0.0f;
m_leftJoint = (b2RevoluteJoint*)m_world->CreateJoint(&jd);

jd.Initialize(m_vehicle, m_rightWheel, m_rightWheel->GetWorldCenter());
jd.collideConnected = false;
m_rightJoint = (b2RevoluteJoint*)m_world->CreateJoint(&jd);
}

{ // ground
b2PolygonDef box;
box.SetAsBox(11.5f, 0.5f);
box.friction = 0.62f;
#pragma mark plank height
b2BodyDef bd;
bd.position.Set(-26.0f, 4.8f);

b2Body* ground = m_world->CreateBody(&bd);
ground->CreateShape(&box);
}

No comments:

FEEDJIT Live