Jump to content

Simple Vehicle function


Sweetgebus
 Share

Recommended Posts

Hello, I've been dissecting the vehicle script trying to understand how it work. Unfortunately despite weeks of headache I've gotten no where. Below is  the Lua script that I pieced together.  I do not get any errors, but my logic must be wrong. I can run the program, but nothing happens when the key is pressed. I've very new to scripting and looking for any help I can get. I appreciate it in advance. 

Script.acceleration=200 --float acceleration
function Script:Start()
	self.vehicle = Vehicle:Create(self.entity)
--Create and Update Camera
	self.camera = Camera:Create()
	self:UpdateCamera()
end

function Script:UpdateCamera()
--Update Camera Postion and Rotation
	self.camera:SetPosition(self.entity:GetPosition())
	self.camera:SetRotation(self.entity:GetRotation())
	self.camera:Move(0,4,-6)
end

function Script:UpdatePhysics()
	local groundspeed = self.entity:GetVelocity()
	groundspeed = groundspeed:xz()
	groundspeed = groundspeed:Length()

	local gas = 0
	local window = Window:GetCurrent()
	if window:KeyDown(Key.W) then gas = gas + self.acceleration end
	self.vehicle:SetGas(gas)
end

 

Link to comment
Share on other sites

 

Script.enabled=true--bool "Enabled"
Script.health=100--float "Health"
Script.teamid=1--choice "Team" "Neutral,Good,Bad"
Script.cameraangle=35--float "Camera Angle"
Script.cameradistance=5--float "Camera Distance"
Script.camerasmoothing=4--float "Cam Smoothing"
Script.acceleration=200--float "Acceleration"
Script.friction=2--float "Tire Friction"
Script.springDamping=10--float "Spring Damping"
Script.spring=300--float "Spring"
Script.springRelaxation=0.1--float "Spring Relaxation"
Script.Brakes=200--float
Script.reverseorientation=false--bool "Reverse Axis"
Script.tiremass=20--float "Tire Mass"

function Script:Enable()
	self.camera:Show()
	self.enabled=true
end

function Script:Hurt(damage)
	self.health = self.health-damage
end

function Script:Disable()
	self.camera:Hide()
	self.enabled=false
	self.started=false
end

function Script:FindTires(entity,tires)
	local n
	local count = entity:CountChildren()
	for n=0,count-1 do
		local child = entity:GetChild(n)
		local name = string.lower(child:GetKeyValue("name"))
		if String:Left(name,5)=="wheel" or String:Left(name,4)=="tire" then
			local model = tolua.cast(child,"Model")
			if model~=nil then
				table.insert(tires,model)
			end
		end
		self:FindTires(child,tires)
	end
end

function Script:Start()
	self.vehicle = Vehicle:Create(self.entity)
	self.camera = Camera:Create()
	if self.enabled==false then
		self.camera:Hide()
	end
	local aamode = tonumber((System:GetProperty("antialias")))
	if aamode ~= nil then
		self.camera:SetMultisampleMode(aamode)
	end
	if self.entity:GetMass()==0 then
		self.entity:SetMass(100)
	end
	self.camera:SetDebugPhysicsMode(true)
	
	local tires={}
	self:FindTires(self.entity,tires)

	local n,tiremesh
	for n,tiremesh in ipairs(tires) do
		local steering=false
		tiremesh:SetSweptCollisionMode(true)
		tiremesh:SetFriction(0.1,self.friction)
		tiremesh:Translate(0,-0.2,0,true)
		local pos = Transform:Point(0,0,0,tiremesh,self.entity)
		if self.reverseorientation==true then
			if pos.z < 0 then steering=true end
		else
			if pos.z > 0 then steering=true end
		end
		tiremesh:SetElasticity(0)
		tiremesh:SetMass(self.tiremass)
		self.vehicle:AddTire(tiremesh,steering,self.spring,self.springRelaxation,self.springDamping)
	end
end

function Script:Collision(entity,position,normal,speed)
	if speed>10 then
		if entity.script ~= nil then
			if type(entity.script.Hurt)=="function" then
				entity.script:Hurt(speed*2,self)
			end
		end
	end
end

function Script:UpdatePhysics()
	if self.enabled==false then return end
	if self.health<=0 then return end	

	local groundspeed = self.entity:GetVelocity()
	groundspeed = groundspeed:xz()
	groundspeed = groundspeed:Length()

	--Acceleration
	local gas = 0
	local window = Window:GetCurrent()
	if window:KeyDown(Key.W) then
		gas = gas + self.acceleration
	end
	if window:KeyDown(Key.S) then
		gas = gas - self.acceleration
	end
	if self.reverseorientation==true then
		gas = -gas
	end
	self.vehicle:SetGas(gas)
	
	--Brakes
	local brakeforce=0
	if window:KeyDown(Key.Space) then
		brakeforce=self.Brakes
	end
	self.vehicle:SetBrakes(brakeforce)

	--Steering
	local angle=0
	if window:KeyDown(Key.D) then
		angle = angle + 35
	end
	if window:KeyDown(Key.A) then
		angle = angle - 35
	end
	local angledamping = Math:Min(1,groundspeed/30.0)
	if brakeforce==0 then
		angledamping = math.sqrt(angledamping)
		angledamping = math.sqrt(angledamping)
	end
	angle = angle * (1 - angledamping)	
	self.vehicle:SetSteering(angle)

end

function Script:UpdateWorld()
	if self.enabled==false then return end
	local currentposition = self.camera:GetPosition(true)
	local currentrotation = self.camera:GetRotation(true)

	self.camera:SetPosition(self.entity:GetPosition(true))

	local modelrotation = self.entity:GetRotation(true)
	modelrotation.z=0	
	self.camera:SetRotation(modelrotation)
--self.camera:SetRotation(0,0,0)

	if self.reverseorientation==true then
		self.camera:Turn(0,180,0)
	end
	self.camera:Turn(self.cameraangle,0,0)
	self.camera:Move(0,0,-self.cameradistance)

	local newposition = self.camera:GetPosition(true)
	local newrotation = self.camera:GetRotation(true)

	if self.started==true then
		self.camera:SetPosition(Math:Curve(newposition.x,currentposition.x,self.camerasmoothing/Time:GetSpeed()),Math:Curve(newposition.y,currentposition.y,self.camerasmoothing/Time:GetSpeed()),Math:Curve(newposition.z,currentposition.z,self.camerasmoothing/Time:GetSpeed()),true)
		self.camera:SetRotation(Math:CurveAngle(newrotation.x,currentrotation.x,self.camerasmoothing/Time:GetSpeed()),Math:CurveAngle(newrotation.y,currentrotation.y,self.camerasmoothing/Time:GetSpeed()),0,true)
	end

	self.started=true
end

--This function will be called when the entity is deleted.
function Script:Detach()
	self.vehicle=nil
	self.camera:Release()
	self.camera = nil
end



 

 

 

Link to comment
Share on other sites

Script.chassis = nil --entity "Chassis"
Script.wheel0   = nil --entity "Wheel0"
Script.wheel1   = nil --entity "Wheel1"
function Script:Start()
	
	self.vehicle = Vehicle:Create(self.entity)
	self.vehicle:AddTire(self.wheel0,false)
	self.vehicle:AddTire(self.wheel1,false)

end



function Script:UpdateWorld()
 
end



function Script:UpdatePhysics()
self.vehicle:SetGas(5000)		
end

Hello, try this simple.

 

 

Link to comment
Share on other sites

30 minutes ago, Yue said:

Script.chassis = nil --entity "Chassis"
Script.wheel0   = nil --entity "Wheel0"
Script.wheel1   = nil --entity "Wheel1"
function Script:Start()
	
	self.vehicle = Vehicle:Create(self.entity)
	self.vehicle:AddTire(self.wheel0,false)
	self.vehicle:AddTire(self.wheel1,false)

end



function Script:UpdateWorld()
 
end



function Script:UpdatePhysics()
self.vehicle:SetGas(5000)		
end

Hello, try this simple.

does the model have to have wheels? like a hovercraft for example

 

Link to comment
Share on other sites

Hi, I am sorry for this but the vehicle does not currently work.

There were changes in the physics engine we use, and some of the changes were things we really needed, but it also broke the vehicles system.

I am looking into an updated system. The code is embedded in a demo and it' will take some time to pull out and integrate correctly.

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

8 hours ago, Sweetgebus said:

documentation is very vague on somethings for sure.  I'm unsure how to set up a vehicle model and the docs don't cover it from what I can tell.

My attempts to make a vehicle, have been focused with joints, but I always get stuck on something. 

 

 

Link to comment
Share on other sites

6 hours ago, Josh said:

Hi, I am sorry for this but the vehicle does not currently work.

There were changes in the physics engine we use, and some of the changes were things we really needed, but it also broke the vehicles system.

I am looking into an updated system. The code is embedded in a demo and it' will take some time to pull out and integrate correctly.

Thats unfortunate, I've been losing mind for the past few weeks trying to make it work.

 

Link to comment
Share on other sites

No need to bother, it won't work. You'll get this problem:

The only vehicle that works in Newton 3 is the custom one in the SDK demos:

https://github.com/MADEAPPS/newton-dynamics/tree/master/newton-3.14/applications/demosSandbox/sdkDemos/demos

Newton 4 reportedly fixes the collision problem, so it actually should be possible to construct a vehicle out of standard joints. I quite like the behavior of such a vehicle, aside from the problem above.

  • Sad 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Now that I know its broken I'm trying to find a work around. I'd like to have an object  gain speed over time. Below is what I've been attempting, but haven't been successful.

function Script:Start()
	self.camera = Camera:Create()
	self:UpdateCamera()
end

function Script:UpdateCamera()
	self.camera:SetPosition(self.entity:GetPosition())
	self.camera:SetRotation(self.entity:GetRotation())
	self.camera:Move(0,3,-4)
end

function Script:UpdatePhysics()
	local speed = 0
	local gas = false

-- Movement / Input
	if window:KeyDown(Key.W) then
		gas = true
		self.entity:Move(0,0,speed)
		else gas = false
			while gas == true do
				speed = speed +1
		end
	end
end

function Script:UpdateWorld()
self:UpdateCamera()
end

 

Link to comment
Share on other sites

Instead of defining a local variable for speed, do this:

function Script:UpdatePhysics()
	if self.speed == nil then self.speed = 0 end --initialize self.speed to zero if it is undefined
	local gas = false

	-- Movement / Input
	if window:KeyDown(Key.W) then
		gas = true
		self.entity:Move(0,0,self.speed)
	end	
	if gas == true then
		self.speed = self.speed + 1
	else
		self.speed = self.speed - 1
		if self.speed < 0 then self.speed = 0 end
	end
end

 

  • Like 2

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

5 minutes ago, Josh said:

Instead of defining a local variable for speed, do this:


function Script:UpdatePhysics()
	if self.speed == nil then self.speed = 0 end --initialize self.speed to zero if it is undefined
	local gas = false

	-- Movement / Input
	if window:KeyDown(Key.W) then
		gas = true
		self.entity:Move(0,0,self.speed)
	end	
	if gas == true then
		self.speed = self.speed + 1
	else
		self.speed = self.speed - 1
		if self.speed < 0 then self.speed = 0 end
	end
end

This seems to work, I haven't seen the self.speed command before. Not sure how it works.  Never the less, I appreciate the help with this. I should be able to manipulate this a bit to get the results I'm looking for, such as not stopping immediately after releasing W.

Link to comment
Share on other sites

By default if a value does not exist it will be nil / null. My code is check to see if the value has been set at all yet, and if not it is set to zero. (Zero and nil are not the same thing in Lua.)

My job is to make tools you love, with the features you want, and performance you can't live without.

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