Jump to content

Driving in Leadwerks 4.5


Marcousik
 Share

Recommended Posts

It is possible to drive in Le 4.5, but first you have to build a little car system, here is what was made until now:

 

I will expose now how far I am with programming this, so that other members can maybe work on this:

This is the structure I created:

2018-07-11.thumb.png.cd091b8de73fd6ac04e7181d1eccca8a.png

 

In blue are the most important children to make the car:

- the "wheels" - are fake decorativ wheels to make the car looks like a car (not so important)

- The "axe" children are pivots that will build through the script the suspensions. (slider joint)

- The "direction" children are pivots that will build the steering system (hinge joint)

- The "roue" children are cylinder CSG to make the tires. (hinge joint)

Here is the script added on the "jeeplight" main object: that is CSG box with invisible material.

 

Okay let see the script:

 

------------------------------------------------------     Script variables properties:

Script.CarMass=200--int "Car Mass"        
Script.TireMass=10--int "Tire Mass"        
Script.SpeedMax=1000--int "Max speed"    
Script.Amort=0.3--float "Amort, long"    
Script.Force=2000--int "Amort, force"    
Script.Rayon=0.5--float "Roue, Rayon"
Script.Epaiss=1--float "Pneus, Épaisseur"
Script.SeeWheels = false --bool "Voir Roues"       -------  not so important
Script.traction=4--int "traction 2/4"    

 

-------------------------------------------------------
function Script:Start()


-- Catch the children:
self.Axes={}
    self.Axes[0]=self.entity:FindChild("AxeAvG")
    self.Axes[1]=self.entity:FindChild("AxeAvD")
    self.Axes[2]=self.entity:FindChild("AxeArG")
    self.Axes[3]=self.entity:FindChild("AxeArD")

self.Directions={}
    self.Directions[0]=self.entity:FindChild("DirectionAvG")
    self.Directions[1]=self.entity:FindChild("DirectionAvD")


self.Tires={}                                                                        -- "real" tires - invisible cylinders
    self.Tires[0]=self.entity:FindChild("RoueAvG")
    self.Tires[1]=self.entity:FindChild("RoueAvD")
    self.Tires[2]=self.entity:FindChild("RoueArG")
    self.Tires[3]=self.entity:FindChild("RoueArD")

self.Wheels={}                                                                   --- fake wheels "decorativ" not so important
    self.Wheels[0]=self.entity:FindChild("WheelAvG")
    self.Wheels[1]=self.entity:FindChild("WheelAvD")
    self.Wheels[2]=self.entity:FindChild("WheelArG")
    self.Wheels[3]=self.entity:FindChild("WheelArD")

    for n=0,3 do
        self.Wheels[n]:SetParent(self.entity) 
    end

self.PosWheels={}
    self.PosWheels[0]=self.Wheels[0]:GetPosition()
    self.PosWheels[1]=self.Wheels[1]:GetPosition()
    self.PosWheels[2]=self.Wheels[2]:GetPosition()
    self.PosWheels[3]=self.Wheels[3]:GetPosition()


    for n=0,3 do
        self.Tires[n]:SetScale(self.Epaiss,self.Rayon*2,self.Rayon*2) 
        self.Tires[n]:SetFriction(10,10)
    end

-- Joints:
self.Volants={}
self.Rouages={}
self.Amortisseurs={}

self.currspeed=0
self.entity:SetMass(self.CarMass)

-- Construit les amortisseurs: /  -------------------------------------     suspensions
local n
local pos
    for n=0,3 do
        pos=self.Axes[n]:GetPosition(true)
        self.Axes[n]:SetMass(self.TireMass)
        self.Amortisseurs[n]=Joint:Slider(pos.x, pos.y, pos.z, 0,1,0, self.Axes[n], self.entity)

        self.Amortisseurs[n]:EnableLimits()
        self.Amortisseurs[n]:SetLimits(-0.1/2,0.1/2)

        self.Amortisseurs[n]:SetTargetAngle(-self.Amort)    --at the middle if 0
        self.Amortisseurs[n]:SetMotorSpeed(1)    -- 1 m/s -- vitesse de la pompe
        self.Amortisseurs[n]:SetStrength(self.Force)    --defatul is 1000
        self.Amortisseurs[n]:EnableMotor()    
--        self.Amortisseurs[n]:SetSpring(50000)


    end

-- Double la force des amortisseurs arrieres /    ---------------------  make the backward suspensions with 2* forces
self.Amortisseurs[2]:SetStrength(self.Force*2)
self.Amortisseurs[3]:SetStrength(self.Force*2)
self.Amortisseurs[2]:SetMotorSpeed(2)
self.Amortisseurs[3]:SetMotorSpeed(2)


-- Volant:                    -   ------------------------------------------steering: Forward
    for n=0,1 do
        pos=self.Directions[n]:GetPosition(true)
        self.Directions[n]:SetMass(self.TireMass)

        self.Volants[n]=Joint:Hinge(pos.x, pos.y, pos.z, 0,1,0, self.Directions[n], self.Axes[n])
        self.Volants[n]:SetAngle(0)
        self.Volants[n]:EnableLimits() 
        self.Volants[n]:SetLimits(-30,30)
        self.Volants[n]:SetMotorSpeed(200)
        self.Volants[n]:EnableMotor() 
    end

-- On attache les roues:    /--------------------------------------- attach the tires
    for n=0,3 do
        pos=self.Tires[n]:GetPosition(true)
        self.Tires[n]:SetMass(self.TireMass)
        if n<2 then
        self.Rouages[n]=Joint:Hinge(pos.x, pos.y, pos.z, 1,0,0, self.Tires[n], self.Directions[n])
        else
        self.Rouages[n]=Joint:Hinge(pos.x, pos.y, pos.z, 1,0,0, self.Tires[n], self.Axes[n])
        end    
        
    end

end

-------------------------------------


function Script:UpdatePhysics()


if self.MyCar==1 and InCar>0 then         ---   these ar my own variables to let the player ingame use the car (not so important for now)

    
-- Traite la direction:                               ---   --------------------manage the steering
    local direction=self.Volants[0]:GetAngle()

    if window:KeyDown(Key.Left) then
        direction=direction-5
    elseif window:KeyDown(Key.Right) then
        direction=direction+5
    elseif window:KeyDown(Key.Left)==false and window:KeyDown(Key.Right)== false then 
        if Math:Round(direction)>0 then direction=direction-5
        elseif Math:Round(direction)<0 then direction=direction+5
        end
    end
    self.Volants[0]:SetAngle(direction)
    self.Volants[1]:SetAngle(direction)

-- Traite l'acceleration:---------------------------------------------------------- manage acceleration

local Gas=0
        if window:KeyDown(Key.Up) then
            Gas=1
            self.currspeed = self.currspeed + 10
            if self.currspeed>self.SpeedMax then
                self.currspeed=self.SpeedMax
            end
        
        elseif window:KeyDown(Key.Down) then
            Gas=1
            self.currspeed = self.currspeed - 10
            if self.currspeed<-self.SpeedMax then
                self.currspeed=-self.SpeedMax
            end
        end

        if Gas==0 then  self.currspeed=0
            for n=0,self.traction-1 do
            self.Rouages[n]:DisableMotor()
            end
        else
            for n=0,self.traction-1 do
            if self.Rouages[n]:MotorEnabled()==false then self.Rouages[n]:EnableMotor() end
            self.Rouages[n]:SetTargetAngle(self.Rouages[n]:GetAngle()+500)

            self.Rouages[n]:SetMotorSpeed(self.currspeed)
            end
        end

--limite la velocite angulaire        /   ----------------------This limits the "twisting" of the car while fast driving, it is not enough but better
    self.entity:SetOmega(0,0,0)


-- fake wheels update:
    
        self.Wheels[0]:SetMatrix(self.Tires[0]:GetMatrix())
        self.Wheels[1]:SetMatrix(self.Tires[1]:GetMatrix())
        self.Wheels[2]:SetMatrix(self.Tires[2]:GetMatrix())
        self.Wheels[3]:SetMatrix(self.Tires[3]:GetMatrix())

        self.Wheels[0]:SetPosition(self.PosWheels[0])
        self.Wheels[1]:SetPosition(self.PosWheels[1])
        self.Wheels[2]:SetPosition(self.PosWheels[2])
        self.Wheels[3]:SetPosition(self.PosWheels[3])
end

 

---------------------------------

 

On the posted image above the used properties can be read.

Most important are the force and the car mass parameters.

Here is what I could test:

- I could make out of this a rapid car but I encounter the problem that the car is bouncing in chaos on the road as the acting forces on the   chassis are big big.

- Augmnenting the mass makes the joints too weak.

- I get the idea that the car actually doesn't have 2 axles as it maybe should have.

 The 2 wheels forward are not bound through an axle just like the backwards too.

- As the car drives, the back of the chassis tends to  sink to the floor/terrain, that's why I doubled the suspensions forces of the backwards wheels

 

There much more to test in this.

(sorry if my english is not so english ?)

 

 

 

 

 

 

 

 

 

 

  • Like 1
  • Upvote 2

 

 

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...