Jump to content

Simple Car

Charrua

636 views

Here there is a way of making a simple car based on hinges

This is not a tutorial, is just simply a stating point for the ones that want/like to play arround physics and hinges...

I included the entire project and the distribution executable to test the scripts so, you have as I say a starting point and just that, hpe it helps someone :)

This is the editor view and the initial placement of the parts needed

1583188430_2018-07-05(1).thumb.png.79a432d7de17215c8c6f3617e9fce683.png

Basically I made 3 scripts

physicsProperties.lua

wheel.lua

steer.lua

First you have to create and place the car body or chassis, 4 wheels and 6 auxiliary pivots/ or any brush you like (a cube is fine) for the hinges

4 of the auxiliary entities are for the wheels hinges and 2 for the wheels steer.

Place the wheel hinge and steer centered with the wheel.

After that you may set some script parameters:

Wheel scritp:

1894129612_2018-07-05(2).png.d5a6157a7d062a09d1a091a139949fd6.png

Basically, the position of the entity script holder is used to create a hinge between the "parent" and the "child" you choose

(in the picture above: between the auxiliary entity SteerFL and the WheelFL)

If vel control is checked then, a motor is enabled for that hinge and keys Up/Down are used to increase/decrease speed

If vel control is not cheched, no motor is enabled and the wheel is free to run

Be carefull whit the Hinge Pin, which dictates the axis over which the wheel will rotate, in this case I used X axis, but if you use other pieces direction/alignement you should adjust this values.

 

Steer script:

1919501412_2018-07-05(3).png.56555be9d1ef053843a6753488b8f88f.png

The steer hinge is used to turn the wheel to handle car heading, so the pin is the Y axis

Limits and Motor are needed to control the steer

Limits is for how much the steer will turn right/left using the default keys left/right arrow

When you press left or ritght key, the right limit will be set as the hinge angle and the hinge will try to reach this angle at the "steer speed", the same, but whit the left limit happen if you press the left key.

 

physicsProperties just let you adjust the friction of the wheels and or the floor

Script.sfrict=0--float "static friction"
Script.kfrict=0--float "kinetic friction"

function Script:Start()
	System:Print("phy properties start")
	self.entity:SetFriction(self.sfrict, self.kfrict)
end

so simple, and in the editor it looks:

179339858_2018-07-05(4).png.aa9e660f12fc05e4f10827b55500455d.png

Here is a hand drawing of how scripts, objects, parent/child are connected

 

 

car.jpg.cc21707e39eced9b14598fbdd292dc4d.jpg

 

Here is the wheel script

Script.currspeed = 0

Script.parent = nil--Entity "Parent"
Script.child = nil--Entity "Child"
Script.pin = Vec3(0,0,1) --Vec3 "Hinge Pin"
Script.motorspeed=500--float "Motor speed"
Script.velcontrolled=false--bool "velControl"

function Script:Start()

	System:Print("wheel start")
	self.entity:Hide()
	local pos = self.entity:GetPosition(false)	--true for global

	if self.child ~= nil then
		if self.parent ~= nil then
			self.joint = Joint:Hinge(pos.x, pos.y, pos.z, self.pin.x, self.pin.y, self.pin.z, self.child, self.parent)
		else
			Debug:Error("no parent assigned")
		end
	else
		Debug:Error("no child assigned")
	end
end

function Script:setMotorSpeed(speed)
	if self.velcontrolled then
		System:Print("setMotorSpeed: "..speed)
		self.currspeed = speed
		if speed~=0 then
			self.joint:EnableMotor()
		end
		self.joint:SetMotorSpeed(self.currspeed)
	end
end

function Script:UpdateWorld()

	if self.motorspeed>0 then
		self.joint:SetAngle(self.joint:GetAngle()+100)
	else
		self.joint:SetAngle(self.joint:GetAngle()-100)
	end

	if App.window:KeyDown(Key.Space) then
		self:setMotorSpeed(0)
	end

	if self.velcontrolled then
		if App.window:KeyDown(Key.Up) then
			self.currspeed = self.currspeed + 10
			if self.currspeed>self.motorspeed then
				self.currspeed=self.motorspeed
			end
			if self.currspeed == 10 then self.joint:EnableMotor() end
			self.joint:SetMotorSpeed(self.currspeed)
			
		end
		if App.window:KeyDown(Key.Down) then
			self.currspeed = self.currspeed - 10
			if self.currspeed<-self.motorspeed then
				self.currspeed=-self.motorspeed
			end
			self.joint:SetMotorSpeed(self.currspeed)
		end
	end
end

Here is the steer scritp

Script.parent = nil--Entity "Parent"
Script.child = nil--Entity "Child"
Script.pin = Vec3(0,1,0) --Vec3 "Hinge Pin"
Script.useLimits=false--bool "use limits"
Script.limits = Vec2(-45,45) --Vec2 "Limits"
Script.useMotor=flase--bool "use motor"
Script.motorspeed=50--float "Steer speed"

function Script:Start()

	System:Print("steer start")
	if self.child == nil then Debug:Error("No child assigned.") end
	if self.parent == nil then Debug:Error("No parent assigned.") end

	self.entity:Hide()
	local pos = self.entity:GetPosition()

	self.joint = Joint:Hinge(pos.x, pos.y, pos.z, self.pin.x, self.pin.y, self.pin.z, self.child, self.parent)
	if self.useLimits then
		self.joint:EnableLimits() 
		self.joint:SetLimits(self.limits.x,self.limits.y)
	end
	if self.useMotor then
		self.joint:SetMotorSpeed(self.motorspeed)
		self.joint:EnableMotor() 
	end
end

function Script:UpdateWorld()
	local direction=0
	if App.window:KeyDown(Key.Left) then
		direction=self.limits.x
	end
	if App.window:KeyDown(Key.Right) then
		direction=self.limits.y
	end
	self.joint:SetAngle(direction)
end

 

here the distro:

simpleCarDistro.zip

here the project:

simpleCar.zip

and here a video...

 

enjoy

 

Juan

  • Like 2


0 Comments


Recommended Comments

There are no comments to display.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

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

×
×
  • Create New...