Jump to content

Simple Car

Charrua

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

  • Blog Entries

    • By tipforeveryone in tipforeveryone's Blog 5
      II. Implement advanced actions
      Previous tutorial guided you to create your own FPS Character Controller with basic actions: Move, Look.
      This tutorial will help you add some advanced actions like Jump, Run, Crouch, and Lean
      *Note: Below steps will use code from previous tutorial (CharacterController.lua)
      1. Jump / Run / Crouch
      Firstly, build obtacle with space under it to test crouching, the space height shoud be at least 120cm (leadwerks editor will show you this height in other viewports) You can not crouch and pass space under 120cm, this was hardcoded


      Next, modify the code.
      Add new variables under --Character movement variables comment. Note that putting script variables at the top of script will allow you to modify character stat easier and faster than finding variables deep inside script's functions
      --Character movement variables Script.playerSpeed = 2 --higer = faster Script.playerRunSpeedMultipier = 2 --if this variable < 1, you can make slower movement Script.playerJumpForce = 7 --This defines how high you can jump Add new funtion to smooth movement of character components function Script:SmoothPosition(position,entity,rate) local smoothX = Math:Curve(position.x,entity:GetPosition().x,rate) local smoothY = Math:Curve(position.y,entity:GetPosition().y,rate) local smoothZ = Math:Curve(position.z,entity:GetPosition().z,rate) entity:SetPosition(smoothX,smoothY,smoothZ) end Replace code in Character_Movement() function by this code function Script:Character_Movement() local playerSpeed, playerJumpForce, playerCrouch --Press Space bar to Jump if window:KeyHit(Key.Space) then playerJumpForce = self.playerJumpForce else playerJumpForce = 0 end --Hold Ctrl key to Crouch if window:KeyDown(Key.ControlKey) then playerCrouch = true else if self.playerHeadBlocked then playerCrouch = true else playerCrouch = false end end --Hold Shift key + AWSD to Run if window:KeyDown(Key.Shift) then playerSpeed = self.playerSpeed * self.playerRunSpeedMultipier else playerSpeed = self.playerSpeed end local playerMove = ((window:KeyDown(Key.W) and 1 or 0) - (window:KeyDown(Key.S) and 1 or 0)) * playerSpeed local playerStrafe = ((window:KeyDown(Key.D)and 1 or 0) - (window:KeyDown(Key.A) and 1 or 0)) * playerSpeed --Using local playerSpeed varialbe instead of self.playerSpeed in the old code local playerTurn = self.playerNeck:GetRotation(true).y self.playerBase:SetInput(playerTurn,playerMove,playerStrafe,playerJumpForce,playerCrouch) end Replace code in Bind_Character_Components_Together() function too function Script:Bind_Character_Components_Together() --Must use this reposition process because playerBase is not playerNeck's parent, they are indipendent. local basePos = self.playerBase:GetPosition(true) local height if window:KeyDown(Key.ControlKey) then height = basePos.y + (self.playerHeight - self.neckLength) / 2 --You can adjust this variable to get desired crouch height for playerNeck (and playerEyes too) else if self.playerHeadBlocked then height = basePos.y + (self.playerHeight - self.neckLength) / 2 else height = basePos.y + self.playerHeight - self.neckLength end end self:SmoothPosition(Vec3(basePos.x,height,basePos.z),self.playerNeck,10) end Add a new function to script, this will keep player crouching if something block above character when release Ctrl key, prevent from being pushed around by obtacle when standing. function Script:Check_Head_Block() --We can use a raycast to check if something block character head when crouching local pickInfo = PickInfo() local point1 = self.playerNeck:GetPosition(true) local point2 = self.playerBase:GetPosition(true) + Vec3(0,self.playerHeight,0) if world:Pick(point1,point2,pickInfo,0.3,true) then self.playerHeadBlocked = true else self.playerHeadBlocked = false end end Update Script:UpdateWorld() function with new Check_Head_Block() function function Script:UpdateWorld() self:Bind_Character_Components_Together() self:Check_Head_Block() self:Character_Look() self:Character_Movement() end Now, you can crouch under obtacle, release Ctrl Key and you are still crouching. Continue moving for auto standing. 2. Leaning
      In Character_Movement() function, add this code after Crouching code
      --Hold Q/E to lean Left/Right local leanDistance = 0.025 --leanDistance should be < 0.025 or camera will pass through wall when you get too close if window:KeyDown(Key.Q) then self.playerEyes:Move(-leanDistance,-0.01,0) end if window:KeyDown(Key.E) then self.playerEyes:Move(leanDistance,-0.01,0) end And add this line at the bottom of Bind_Character_Components_Together() function self:SmoothPosition(Vec3(0,self.neckLength,0),self.playerEyes,10) You can lean now, so smooth. Final script file was attached to this entry. You can apply it to a pivot point (PlayerControl).
      CharacterController.lua
    • By 💎Yue💎 in The shock absorbers 2
      Coming to the end of my prototype of a 3d game, and with background music by Hanz Zimmer ( Time ). I saw my progress in many aspects, always something to learn, always something to improve, I didn't intend to make a game, that has never been the goal. 
      Rather, the effort and dedication immeasurably, was to improve on something learned. And here I was with the powerful leadwerks engine, where his greatest power lay in making everything very easy.  
      About the project
      The prototype is very simple, a third person character goes through a stage, an orbital camera that follows him with many interesting things when scripting.  A character who runs, walks, ducks, jumps, and suffers damage when he falls from different heights. 
      The interesting thing about all this is that just like when you're little and learn to write, repetition is key to learning to program, understand concepts and improve.  So as I've always said, you learn to program by programming, although I sincerely think that lua script is not programming, but the experts say it is, so I go into that elitism of those of us who think we are programmers. 
      The final part of the project consists of creating a death animation, this will be activated when the player falls from a high part and separates the legs (that is very cruel). 
      But that feeling of improvement is the same that I feel when in the mornings when I have a coffee I solve a riddle of the newspaper, but I don't know when I'm going to stop, it turns out and it happens that technology advances very fast and this continues and continues without stopping. Starting with LE 5, and the only thing that can happen is that I die or that my old computer doesn't work anymore. 
      Translated with www.DeepL.com/Translator





    • By 💎Yue💎 in The shock absorbers 1
      It's interesting that when you become an expert on something, you're not sparing any effort to see how something works, but rather you're focusing on creating something. And so everything becomes easier.
      At this point of learning there is a glimpse of a low idea of creating a game, but the secret of all this is to keep it simple and to be very clear that a game is a game, and not an exact simulation of the real world. For example anyone who has a low idea of the red planet, will understand no matter the colors of the scene that is a terrain of Mars, even if it is not very real what is transmitted, a game, that's just it.
      At this point I already have an astronaut character who runs from one place to another on a very large 4096 x 4046 terrain that would surely take a long walk. My previous prototype projects involve a vehicle, but I didn't get the best implementation prospect in that time and I always found performance problems in my machine, something that isn't happening with the character controller for a third person player. 
      As always, I think I'm a scavenger looking for game resources, that's where this community exposes links to websites with interesting hd textures, and one or another model searched on the net, but what I've greatly improved is learning to write code, I have a better workflow, writing Lua code focused on the paradigm of object programming.



      Something interesting is the system of putting rocks, all very nice from the point of implementing them. And it works very well with the character controller if you put collision in cube form.
      I've been thinking about implementing a car system, I think it would be necessary in such a large terrain, but I think it's not the time, my previous experience, involves deterioration in performance and something I think is the physics of the car with respect to the terrain and rocks that in the previous project involve deterioration in the fps. Although if you implement a car would have an option would be to remove the rocks, but I prefer not to have a car and if you have rocks. 
       
       
       
       
×
×
  • Create New...