AddTire
This command adds a tire to a vehicle.
Syntax
- int AddTire(float x, float y, float z, float mass, float radius, float width, bool steering, float suspensionDamper = 200.0, float suspensionSpring = 2000.0, float suspensionLength = 1.2, float lateralStiffness = 20.0, float longitudinalStiffness = 100000.0, float aligningMOmentTrail = 1.5, float friction = 1.0)
Parameters
- x: x component of the tire position.
- y: y component of the tire position.
- z: z component of the tire position.
- mass: mass of the tire.
- radius: radius of the tire.
- width: width of the tire.
- steering: if set to true steering is enabled for this tire, otherwise it is disabled.
- suspensionDamper: a value for damping suspension force.
- suspensionSpring: spring value for suspension.
- suspensionLength: length of suspension.
- lateralStiffness: lateral stiffness of the tire.
- longitudinalStiffness: longitudinal stiffness of the tire.
- aligningMomentTrail: a value for adjusting tire alignment.
- friction: friction of the tire.
Returns
Returns the index of a newly created tire, starting at zero.
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;
}