Smoothed car drive - Leadwerks 4.5
	I'm just happy to get this work 
 here is a demo:
This is actually better than driving in 4.3 because cars are now able to jump or crash without experimenting crazy rotations or whatever ?
Here is the script to make this:
> Here the values I used:
> And here what changed: Is written in dark:
-------------------------------------
function Script:Start()
-- [...........]
	-- Construit les amortisseurs: / suspensions
	local n
	local pos
	    for n=0,3 do
	        pos=self.entity: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.25/2,0.25/2)
	        self.Amortisseurs[n]:SetTargetAngle(-self.Amort)    --at the middle if 0
	        self.Amortisseurs[n]:SetMotorSpeed(10000)    ------------- vitesse de la pompe/ suspensions speed
	        self.Amortisseurs[n]:SetStrength(self.Force)    --defatul is 1000
	        self.Amortisseurs[n]:EnableMotor()    
	
	    end
-- [...........]
----- I removed the backward suspensions with 2* forces because this only depends on the positions of the suspensions on the chassis.
----- Making them symmetrical is ok to solve this.
	----------------------------------------
	function Script:UpdatePhysics()
	
	if self.MyCar==1 and InCar>0 then
	    
	-- Traite la direction:    
	local turning=0
	    local direction=self.Volants[0]:GetAngle()
	    if window:KeyDown(Key.Left) then
	        direction=direction-5 turning=-1
	    elseif window:KeyDown(Key.Right) then
	        direction=direction+5 turning=1
	    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:
	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,3 do
	            self.Rouages[n]:DisableMotor()
	            end
	        else
	            for n=0,3 do
	            if self.Rouages[n]:MotorEnabled()==false then self.Rouages[n]:EnableMotor() end
	            self.Rouages[n]:SetTargetAngle(self.Rouages[n]:GetAngle()+1000)
	            self.Rouages[n]:SetMotorSpeed(self.currspeed)
	            self.Tires[n]:SetOmega(self.Tires[n]:GetOmega()*Vec3(5,5,5))   ---------  make the car go faster
	            end
	        end
--Smoothing:
	if turning==0 then
	    self.entity:SetOmega(self.entity:GetOmega()*Vec3(-1,-1,-1))
	else self.entity:SetOmega(0,turning*Gas,0) end
	    self.Axes[0]:SetOmega(self.Axes[0]:GetOmega()*Vec3(-1,-1,-1))
	    self.Axes[1]:SetOmega(self.Axes[1]:GetOmega()*Vec3(-1,-1,-1))
	    self.Axes[2]:SetOmega(self.Axes[2]:GetOmega()*Vec3(-1,-1,-1))
	    self.Axes[3]:SetOmega(self.Axes[3]:GetOmega()*Vec3(-1,-1,-1))
	    self.Axes[0]:SetVelocity(self.Axes[0]:GetVelocity()*Vec3(1,0.5,1))
	    self.Axes[1]:SetVelocity(self.Axes[1]:GetVelocity()*Vec3(1,0.5,1))
	    self.Axes[2]:SetVelocity(self.Axes[2]:GetVelocity()*Vec3(1,0.5,1))
	    self.Axes[3]:SetVelocity(self.Axes[3]:GetVelocity()*Vec3(1,0.5,1))
	
	-- 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]+Vec3(0,self.Amortisseurs[0]:GetAngle()/6,0))
	        self.Wheels[1]:SetPosition(self.PosWheels[1]+Vec3(0,self.Amortisseurs[1]:GetAngle()/6,0))
	        self.Wheels[2]:SetPosition(self.PosWheels[2]+Vec3(0,self.Amortisseurs[2]:GetAngle()/6,0))
	        self.Wheels[3]:SetPosition(self.PosWheels[3]+Vec3(0,self.Amortisseurs[3]:GetAngle()/6,0))
	end
- 
					
						
					
							
								
							
							
								1
							
					
						
					
				 
			
		
							
12 Comments
Recommended Comments