Jump to content

Creating a vehicle whit lua?!


Gilmer
 Share

Recommended Posts

Hello everyone.

My team recently started using the leadwerks, so we are not totally familiar with it ... hehe

 

In the project, we're adding some vehicles, equal to Viperscout sample. But we are having some problems.

 

Here is what we did:

 

1. Finished the modeling of the car, export only the car whit a cubes to guide the wheels as pivot:

2j3mm9s.png

2qitjbs.png

 

2. We use the max exporter to leadwerks, and exported the gmf, textures .dds and the .phy.

 

3. Soon after, we export two separate models of the wheel, the left and right, and put it in another directory.

 

4. In .lua file of the cara, we put the following code:

 

require("scripts/class")
require("scripts/loop")
require("scripts/math/math")

local class=CreateClass(...)

function class:CreateObject(model)
       local object=self.super:CreateObject(model)     

       function object:UpdateMatrix()
               self:UpdateTires()
       end

       function object:UpdateTires()
               local speed
               for n=0,3 do
                       if self.tire[n]~=nil then
                               self.tire[n]:SetMatrix( self.vehicle:GetTireMatrix(n) )
                               if self.emitter[n]~=nil then
                                       self.emitter[n]:SetMatrix(self.vehicle:GetTireMatrix(n))
                                       self.emitter[n]:Pause()
                                       if self.vehicle:TireIsAirborne(n)==0 then
                                               speed = math.abs(self.model:GetVelocity(0).z)
                                               speed = (speed - 4) * 20.0
                                               speed = Clamp( speed, 0, 1 ) * 0.5
                                               if speed>0 then
                                                       self.emitter[n]:Resume()
                                                       self.emitter[n]:SetColorf(1,1,1,speed)
                                               end
                                       end
                               end
                       end
               end
       end

       local pivot
       local suspensionlength=0.1
       local springconstant=25.0
       local springdamper=115.0

       object.vehicle=CreateVehicle(object.model)
       object.tire={}
       object.emitter={}

       pivot=object.model:FindChild("tire1")
       if pivot~=nil then
               tireradius=pivot.aabb.h/2.0
               object.vehicle:AddTire(TFormPoint(pivot.position,pivot.parent,model),tireradius,suspensionlength,springconstant,springdamper)
               object.tire[0]=LoadMesh("abstract::tire_left.gmf",object.model)
               pivot:Hide()
       end

       pivot=object.model:FindChild("tire2")
       if pivot~=nil then
               tireradius=pivot.aabb.h/2.0
               object.vehicle:AddTire(TFormPoint(pivot.position,pivot.parent,model),tireradius,suspensionlength,springconstant,springdamper)
               object.tire[2]=LoadMesh("abstract::tire_left.gmf",object.model)
               pivot:Hide()
       end     

       pivot=object.model:FindChild("tire3")
       if pivot~=nil then
               tireradius=pivot.aabb.h/2.0
               object.vehicle:AddTire(TFormPoint(pivot.position,pivot.parent,model),tireradius,suspensionlength,springconstant,springdamper)
               object.tire[1]=LoadMesh("abstract::tire_right.gmf",object.model)
               pivot:Hide()
       end


       pivot=object.model:FindChild("tire4")
       if pivot~=nil then
               tireradius=pivot.aabb.h/2.0
               object.vehicle:AddTire(TFormPoint(pivot.position,pivot.parent,model),tireradius,suspensionlength,springconstant,springdamper)
               object.tire[3]=LoadMesh("abstract::tire_right.gmf",object.model)
               pivot:Hide()
       end     

       object.emitter={}
       object:UpdateTires()

       if world_transparency~=nil then
               if world_main~=nil then
                       SetWorld(world_transparency)
                       for n=0,3 do
                               if object.tire[n]~=nil then
                                       object.emitter[n]=CreateEmitter(50,2000,Vec3(0,1,0),0,object.tire[n])
                                       object.emitter[n]:SetPosition(Vec3(0,-object.tire[n].aabb.h/2.0,0))
                                       object.emitter[n]:SetParent(model,1)
                                       object.emitter[n]:SetRadius(1,6)
                                       object.emitter[n]:SetWaver(1.0)
                                       object.emitter[n]:Paint(LoadMaterial("abstract::roaddust.mat"))
                                       object.emitter[n]:SetRotationSpeed(0.1)
                                       object.emitter[n]:Pause()
                               end
                       end
                       SetWorld(world_main)
               end
       end

       object.model.buoyant=0
       object.model:SetKey("collisiontype",COLLISION_PROP)
       object.model:SetKey("mass",1.2)
       object:UpdateTires()
end

 

We can see, witch it find the tires, and hide the pivot normally...

 

21k9d0i.png

 

But the problems are the following: the car does not fall, he is floating in air, not having this reaction to gravity. I try to change the mass and enable the gravity of it in the properties editor, but even so, when I apply, close and open again, it resets the settings.

 

I wonder why this happening? Is there something wrong with the code, or I forgot something?

 

And other detail, in the file viperscout.lua, has a line like this:

 

object.turret=object.model:FindChild("turret")

 

whats this "turret"?

 

thanks

Link to comment
Share on other sites

The viperscout model has a turret which is controlled by the mouse. You can remove these lines since your car doesn't have a turret.

 

Try deleting your phy file and make a new one. It should be a convex hull physics file. You can find the phy converter in the tools folder from the sdk.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...