Build
This function finalizes a vehicle and makes it ready to use. This command must be called after a vehicle is created, before the next call to World::Update().
Syntax
Returns
Returns true if successful, otherwise false is returned.
Example
#include "Leadwerks.h"
using namespace Leadwerks;
Vehicle* vehicle = NULL;
Model* tiremodel[4];
int main(int argc, const char *argv[])
{
Leadwerks::Window* window = Leadwerks::Window::Create();
Context* context = Context::Create(window);
World* world = World::Create();
Camera* camera = Camera::Create();
//Create a window
window = Window::Create("Vehicle Example", 0, 0, 1024, 768);
//Create a graphics context
context = Context::Create(window, 0);
//Create a world
world = World::Create();
//Create a camera
camera = Camera::Create();
camera->SetPosition(0, 2, -5);
//Create the ground
Entity* ground = Model::Box();
Shape* shape = Shape::Box();
ground->SetShape(shape);
shape->Release();
ground->SetScale(1000, 1, 1000);
ground->SetPosition(0, -0.5, 0);
ground->SetCollisionType(Collision::Scene);
ground->SetColor(0, 1, 0, 1);
Light* light = DirectionalLight::Create();
light->SetRotation(45, 35, 0);
//Create the vehicle chassis
Model* chassis = Model::Box();
shape = Shape::Box();
chassis->SetShape(shape);
shape->Release();
chassis->SetScale(1.5, 1, 4);
chassis->SetMass(2000);
chassis->SetCollisionType(Collision::Prop);
chassis->SetColor(1, 0, 0, 1);
chassis->SetPosition(0, 1, 0);
//Create the vehicle
vehicle = Vehicle::Create(chassis);
//Add tires
float tireradius = 0.5;
float tiremass = 100;
float tirewidth = 0.5;
float tireheight = -0.5;
float tirespacing = 0.6;
tiremodel[0] = Model::Cylinder();
tiremodel[0]->SetScale(tireradius * 2, tirewidth, tireradius * 2);
tiremodel[0]->SetRotation(0, 0, 90);
tiremodel[0]->SetPosition(tirespacing, tireheight, 1.5);
vehicle->AddTire(tirespacing, tireheight, 1.5, tiremass, tireradius, tirewidth, true);
tiremodel[1] = Model::Cylinder();
tiremodel[1]->SetScale(tireradius * 2, tirewidth, tireradius * 2);
tiremodel[1]->SetRotation(0, 0, 90);
tiremodel[1]->SetPosition(-tirespacing, tireheight, 1.5);
vehicle->AddTire(-tirespacing, tireheight, 1.5, tiremass, tireradius, tirewidth, true);
tiremodel[2] = Model::Cylinder();
tiremodel[2]->SetScale(tireradius * 2, tirewidth, tireradius * 2);
tiremodel[2]->SetRotation(0, 0, 90);
tiremodel[2]->SetPosition(tirespacing, tireheight, -1.5);
vehicle->AddTire(tirespacing, tireheight, -1.5, tiremass, tireradius, tirewidth, false);
tiremodel[3] = Model::Cylinder();
tiremodel[3]->SetScale(tireradius * 2, tirewidth, tireradius * 2);
tiremodel[3]->SetRotation(0, 0, 90);
tiremodel[3]->SetPosition(-0.5, tireheight, -1.5);
vehicle->AddTire(-tirespacing, tireheight, -1.5, tiremass, tireradius, tirewidth, false);
//Add axles
vehicle->AddAxle(0, 1);
vehicle->AddAxle(2, 3);
//Finalize the vehicle
Debug::Assert(vehicle->Build(), "Failed to build vehicle.");
//Start the engine
vehicle->SetEngineRunning(true);
//Release the handbrakes
vehicle->SetHandBrakes(0);
while (true)
{
//If window has been closed, end the program
if (window->Closed() || window->KeyDown(Key::Escape)) return false;
//Update the app timing
Leadwerks::Time::Update();
//Update steering
float steering = 0;
if (window->KeyDown(Key::D)) steering += 30;
if (window->KeyDown(Key::A)) steering -= 30;
vehicle->SetSteering(steering);
//Update acceleration
float gas = 0;
if (window->KeyDown(Key::W)) gas += 0.25;
if (window->KeyDown(Key::S)) gas -= 0.25;
vehicle->SetAcceleration(gas);
//Update brakes
if (window->KeyDown(Key::Space))
{
vehicle->SetBrakes(2000);
}
else
{
vehicle->SetBrakes(0);
}
//Update the world
world->Update();
//Update tire models to match physics
for (int n = 0; n < 4; n++)
{
Vec3 scale = tiremodel[n]->GetScale();
tiremodel[n]->SetMatrix(vehicle->GetTireMatrix(n));
tiremodel[n]->Turn(0, 0, 90);
tiremodel[n]->SetScale(scale);
}
//Render the world
world->Render();
//Refresh the screen
context->Sync(true);
}
return 0;
}