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

--Create a window
window=Window:Create("Vehicle Example",0,0,1024,768)

--Create the graphics context
context=Context:Create(window,0)
if context==nil then return false end

--Create a world
world=World:Create()

--Create a camera
camera = Camera:Create()
camera:SetPosition(0,2,-5)

--Create the ground
local ground = Model:Box()
local 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)

--Create a light
local light = DirectionalLight:Create()
light:SetRotation(45,35,0)

--Create the vehicle chassis
chassis = Model:Box()
local 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)
chassis:SetPosition(0,1,0)

--Create the vehicle
vehicle = Vehicle:Create(chassis)

--Add tires
local tireradius=0.5
local tiremass=100
local tirewidth=0.5
local tireheight=-0.5
local tirespacing=0.6

tiremodel={}

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
if vehicle:Build()==false then Debug:Error("Failed to build vehicle.") end

--Start the engine
vehicle:SetEngineRunning(true)

--Release the emergency brake
vehicle:SetHandBrakes(0)

while true do
--This is our main program loop and will be called continuously until the program ends

--If window has been closed, end the program
if window:Closed() or window:KeyDown(Key.Escape) then return false end

--Update the app timing
Time:Update()

--Update steering
local steering=0
if window:KeyDown(Key.D) then steering=steering+30 end
if window:KeyDown(Key.A) then steering=steering-30 end
vehicle:SetSteering(steering)

--Update acceleration
local gas=0
if window:KeyDown(Key.W) then gas=gas+0.25 end
if window:KeyDown(Key.S) then gas=gas-0.25 end
vehicle:SetAcceleration(gas)

--Update brakes
if window:KeyDown(Key.Space) then
vehicle:SetBrakes(2000)
else
vehicle:SetBrakes(0)
end

--Update the world
world:Update()

--Update tire models to match physics
for n=0,3 do
local scale = tiremodel[n]:GetScale()
tiremodel[n]:SetMatrix(vehicle:GetTireMatrix(n))
tiremodel[n]:Turn(0,0,90)
tiremodel[n]:SetScale(scale)
end

--Render the world
world:Render()

--Refresh the screen
context:Sync(true)

end