Jump to content

Vehicle WIP in Leadwerks 4.6



Here's a look at the new vehicle system that is being developed. The API has been simplified so you simply create a vehicle, add as many tires as you want, and start using it. The AddTire() command now accepts a model and the dimensions of the tire are calculated from the bounds of the mesh.

class Vehicle
	int AddTire(Model* model, bool steering=false, const float spring=500.0f, const float springrelaxation = 0.1f, const float springdamping = 10.0f);
	void SetGas(const float accel);
	void SetBrakes(const float brakes);
	void SetSteering(const float angle);
	static Vehicle* Create(Entity* entity);

A script will be provided which turns any vehicle model into a ready-to-use playable vehicle. The script searches for limb names that start with "wheel" or "tire" and turns those limbs into wheels. If they are positioned towards the front of the car, the script assumes the wheels can be turned with steering. You can also reverse the orientation of the vehicle if the model was designed backwards.

There is one big issue to solve still. When a vehicle drives on flat terrain the tires catch on the edges of the terrain triangles and the whole vehicle bounces around badly. I'm looking into how this can be solved with custom collision overrides. I do not know how long this will take, so it might or might not be ready by Christmas.

  • Like 3
  • Thanks 1


Recommended Comments

That video looks pretty solid.  I assume we create our own collision mesh(es) for the body?  For example, a car. a truck and a plough truck will have different looking meshes.

It would also be nice if the code that detects "wheel" and "tire" could also detect the words "front," "back," "left" and "right" in the names.  That way there would be no ambiguity and we can name a tire tire.front.left or FrontLeftWheel, for example.

Also, what do you mean by a script doing the interpretation?  How do people using only C++ get their car tires detected?

Share this comment

Link to comment

Yes, you can have your own physics shape for the chassis. In fact you can also now have your own physics shapes for the wheels.

A search for the term "wheel" will return a false positive if a limb called "steeringwheel" is encountered. Perhaps if the name contains "tire" or if the left five characters are "wheel" or if the name contains "wheel" AND does not contain "steer"?

It's easiest to just throw a script on a model, but a C++ example will also be provided.

Share this comment

Link to comment

Yeah, the rules seem pretty easy to set up and probably best to search for minimal terms, not combined terms.  If the name contains "steering" then it's safe to assume it's a steering wheel.  Name could also be "steering wheel" or "steering_wheel" so searching for "steeringwheel" might not work.  Though it's not too bad to ask devs to name limbs specific things (like how we have collisionhull right now), it's ideal to be flexible, if possible, to not even need to have devs alter their models.

Also, the model could include a spare tire on the back (or in the trunk), like Jeeps do (https://ddcfq0gxiontw.cloudfront.net/Review/39832078/17545495/medium_square.jpg) so not sure how you'd handle that.  I guess if the name includes "spare," it's disregarded.

Share this comment

Link to comment

This looks great.

But I have to ask: It would have been so interesting for me (and maybe others) to know the issue on how to get a solid car when using the joints system a few members here worked on for a few months.. ;) !! Could/is this now be possible ? 

Share this comment

Link to comment
1 hour ago, Jorns2 said:

So, that can be used for tanks ? and wil be avaiable for .lua?

You could make a tank with this using lots of wheels. I am not making a tread system right now, but that is possible simply by using the hinge joint:

32 minutes ago, Marcousik said:

This looks great.

But I have to ask: It would have been so interesting for me (and maybe others) to know the issue on how to get a solid car when using the joints system a few members here worked on for a few months.. ;) !! Could/is this now be possible ? 

Here is the source for the vehicle class. It just uses regular joints. There is one tricky part I have to customize though so the wheel collision is better at high speeds.

  • Confused 1

Share this comment

Link to comment

Here's another look at the physics. It looks very good but there are a couple of problems to work out still:


  • Like 1
  • Thanks 1

Share this comment

Link to comment

Okay great. Did you manage to use SetString() or slider joint() for the suspension ?

I could not find anything running ok with SetString() (well with 4.5)

The climb effect looks very nice, I think better than simple joints let do. how did you manage this ? Any tipps 😉



Share this comment

Link to comment

What does SetString have to do with vehicles?

i turned the friction way up on the tires. The climbing behavior is actually pretty realistic. A real 4x4 will do that, although the impact when it hit the hill probably would have ripped the wheels off

Share this comment

Link to comment

@Josh  "What does SetString have to do with vehicles?"

Well you recommended  to use SetSpring() for vehicles suspensions in Juli instead of enabling the slider joints limit. 

I wanted to know if you disable the limit on the suspension and use SetSpring() in 4.6 ?

Share this comment

Link to comment

SetSpring() not SetString() ;)

I am experimenting with the joint limit. It works about the same either way.

Share this comment

Link to comment

Okay yes, sure..little confusion.

Something more:

Do I have to replace the vehicles.h with the one you upload here ?

What should I do with the .cpp file? 

Can I use this with lua ? Im a newbie to this sorry




Share this comment

Link to comment

Actually it helps me a lot because I try toread the c++ functions. I'm  actually wondering what is "mat" used for ?

Vec3 scale = entity->mat.GetScale();

Then it becames mat[1], mat[2] and mat[3] but I can't find the definition of this.

Can't find where does this "mat" comes out...



Share this comment

Link to comment

Join the conversation

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

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 Chris Vossen in Chris Vossen's Development Blog 6
      There are two types of days here at Leadwerks; days that we work and those that we werk. On werk days we hunker down at our desks, cups of caffeine in hand, and code (Unless you’re thinking… or pretending to think). Then there are work days, on these days we focus on business development: researching, planning, and occasionally field trips. So to kick off our intern hunt members of Leadwerks grabbed their sack lunches, kissed their monitors farewell, and ventured over to the Sacramento State University for some high octane presentations!

      These action packed presentations were given to students in both a game architecture course and a 3D modeling class, focusing on highlighting our four new internship openings:

      Programming Internship

      Assist the development team with core engine design
      Work with game development team to create a sample Leadwerks3D game
      Gain experience working in a professional software development environment
      C++ and Lua experience are a plus but not required


      3D Art internship

      Become proficient with the Leadwerks3D design tools
      Provide feedback to the development team to improve the art pipeline
      Assist game development team by producing 3D models, textures, animations, and game levels


      Web Development Internship

      Work with marketing to enhance our online identity
      Develop social features for our online community of game developers
      Foster online community relations


      Marketing Internship

      Author promotional materials for website, press releases, and newsletters
      Interact with the development team to communicate technical information to the public
      Enhance and promote our unique company brand

      We had a wonderful reception and have received an outpour of interest. The trip was well worth leaving our keyboards neglected for one day. A special thanks to everyone at Sac State and with any luck the Leadwerks intern family may be growing in the future!
    • By Josh in Josh's Dev Blog 2
      DPI scaling and the 2D drawing and GUI system were an issue I was a bit concerned about, but I think I have it worked out. This all goes back to the multi-monitor support that I designed back in September. Part of that system allows you to retrieve the DPI scale for each display. This gives you another piece of information in addition to the raw screen resolution. The display scale gives you a percentage value the user expects to see vector graphics at, with 100% being what you would expect with a regular HD monitor. If we scale our GUI elements and font sizes by the display scale we can adjust for screens with any pixel density.
      This shot shows 1920x1080 fullscreen with DPI scaling set to 100%:

      Here we see the same resolution, with scaling set to 125%:

      And this is with scaling set to 150%:

      The effect of this is that if the player is using a 4K, 8K, or any other type of monitor, your game can display finely detailed text at the correct size the user expects to see. It also means that user interfaces can be rendered at any resolution for VR.
      Rather than trying to automatically scale GUI elements I am giving you full control over the raw pixels. That means you have to decide how your widgets will be scaled yourself, and program it into the game interface, but there is nothing hidden from the developer. Here is my code I am working with now to create a simple game menu. Also notice there is no CreatePanel(), CreateButton(), etc. anymore, there is just one widget you create and set the script for. I might add an option for C++ actors as well, but since these are operating on the main logic thread there's not really a downside to running the code in Lua.
      local window = ActiveWindow() if window == nullptr then return end local framebuffer = window:GetFramebuffer() if framebuffer == nil then return end self.gui = CreateGUI(self.guispritelayer) --Main background panel self.mainpanel = CreateWidget(self.gui,"",0,0,framebuffer.size.x,framebuffer.size.y) self.mainpanel:SetScript("Scripts/GUI/Panel.lua", true) local scale = window.display.scale.y local w = 120 local h = 24 local sep = 36 local x = framebuffer.size.x / 6 local y = framebuffer.size.y / 2 - sep * 3 self.resumebutton = CreateWidget(self.mainpanel,"RESUME GAME",x,y,w,h) self.resumebutton:SetScript("Scripts/GUI/Hyperlink.lua", true) self.resumebutton:SetFontSize(14 * window.display.scale.y) y=y+sep*2 self.label2 = CreateWidget(self.mainpanel,"OPTIONS",x,y,w,h) self.label2:SetScript("Scripts/GUI/Hyperlink.lua", true) self.label2:SetFontSize(14 * window.display.scale.y) y=y+sep*2 self.quitbutton = CreateWidget(self.mainpanel,"QUIT", x,y, w,h) self.quitbutton:SetScript("Scripts/GUI/Hyperlink.lua", true) self.quitbutton:SetFontSize(14 * window.display.scale.y) w = 400 * scale h = 550 * scale self.optionspanel = CreateWidget(self.mainpanel,"QUIT", (framebuffer.size.x- w) * 0.5, (framebuffer.size.y - h) * 0.5, w, h) self.optionspanel:SetScript("Scripts/GUI/Panel.lua", true) self.optionspanel.color = Vec4(0.2,0.2,0.2,1) self.optionspanel.border = true self.optionspanel.radius = 8 * scale self.optionspanel.hidden = true  
    • By Josh in Josh's Dev Blog 2
      Previously I talked about the technical details of hardware tessellation and what it took to make it truly useful. In this article I will talk about some of the implications of this feature and the more advanced ramifications of baking tessellation into Turbo Game Engine as a first-class feature in the 
      Although hardware tessellation has been around for a few years, we don't see it used in games that often. There are two big problems that need to be overcome.
      We need a way to prevent cracks from appearing along edges. We need to display a consistent density of triangles on the screen. Too many polygons is a big problem. I think these issues are the reason you don't really see much use of tessellation in games, even today. However, I think my research this week has created new technology that will allow us to make use of tessellation as an every-day feature in our new Vulkan renderer.
      Per-Vertex Displacement Scale
      Because tessellation displaces vertices, any discrepancy in the distance or direction of the displacement, or any difference in the way neighboring polygons are subdivided, will result in cracks appearing in the mesh.

      To prevent unwanted cracks in mesh geometry I added a per-vertex displacement scale value. I packed this value into the w component of the vertex position, which was not being used. When the displacement strength is set to zero along the edges the cracks disappear:

      Segmented Primitives
      With the ability to control displacement on a per-vertex level, I set about implementing more advanced model primitives. The basic idea is to split up faces so that the edge vertices can have their displacement scale set to zero to eliminate cracks. I started with a segmented plane. This is a patch of triangles with a user-defined size and resolution. The outer-most vertices have a displacement value of 0 and the inner vertices have a displacement of 1. When tessellation is applied to the plane the effect fades out as it reaches the edges of the primitive:

      I then used this formula to create a more advanced box primitive. Along the seam where the edges of each face meet, the displacement smoothly fades out to prevent cracks from appearing.

      The same idea was applied to make segmented cylinders and cones, with displacement disabled along the seams.

      Finally, a new QuadSphere primitive was created using the box formula, and then normalizing each vertex position. This warps the vertices into a round shape, creating a sphere without the texture warping that spherical mapping creates.

      It's amazing how tessellation and displacement can make these simple shapes look amazing. Here is the full list of available commands:
      shared_ptr<Model> CreateBox(shared_ptr<World> world, const float width = 1.0); shared_ptr<Model> CreateBox(shared_ptr<World> world, const float width, const float height, const float depth, const int xsegs = 1, const int ysegs = 1); shared_ptr<Model> CreateSphere(shared_ptr<World> world, const float radius = 0.5, const int segments = 16); shared_ptr<Model> CreateCone(shared_ptr<World> world, const float radius = 0.5, const float height = 1.0, const int segments = 16, const int heightsegs = 1, const int capsegs = 1); shared_ptr<Model> CreateCylinder(shared_ptr<World> world, const float radius = 0.5, const float height=1.0, const int sides = 16, const int heightsegs = 1, const int capsegs = 1); shared_ptr<Model> CreatePlane(shared_ptr<World> world, cnst float width=1, const float height=1, const int xsegs = 1, const int ysegs = 1); shared_ptr<Model> CreateQuadSphere(shared_ptr<World> world, const float radius = 0.5, const int segments = 8); Edge Normals
      I experimented a bit with edges and got some interesting results. If you round the corner by setting the vertex normal to point diagonally, a rounded edge appears.

      If you extend the displacement scale beyond 1.0 you can get a harder extended edge.

      This is something I will experiment with more. I think CSG brush smooth groups could be used to make some really nice level geometry.
      Screen-space Tessellation LOD
      I created an LOD calculation formula that attempts to segment polygons into a target size in screen space. This provides a more uniform distribution of tessellated polygons, regardless of the original geometry. Below are two cylinders created with different segmentation settings, with tessellation disabled:

      And now here are the same meshes with tessellation applied. Although the less-segmented cylinder has more stretched triangles, they both are made up of triangles about the same size.

      Because the calculation works with screen-space coordinates, objects will automatically adjust resolution with distance. Here are two identical cylinders at different distances.

      You can see they have roughly the same distribution of polygons, which is what we want. The same amount of detail will be used to show off displaced edges at any distance.

      We can even set a threshold for the minimum vertex displacement in screen space and use that to eliminate tessellation inside an object and only display extra triangles along the edges.

      This allows you to simply set a target polygon size in screen space without adjusting any per-mesh properties. This method could have prevented the problems Crysis 2 had with polygon density. This also solves the problem that prevented me from using tessellation for terrain. The per-mesh tessellation settings I worked on a couple days ago will be removed since it is not needed.
      Parallax Mapping Fallback
      Finally, I added a simple parallax mapping fallback that gets used when tessellation is disabled. This makes an inexpensive option for low-end machines that still conveys displacement.

      Next I am going to try processing some models that were not designed for tessellation and see if I can use tessellation to add geometric detail to low-poly models without any cracks or artifacts.
  • Create New...