Unity3d-based physical racing driver system sharing

Source: Internet
Author: User

Recently, unity3d is a powerful 3d engine. This article describes a physical racing driver Algorithm Based on this engine.

Modeling is ugly...

First, we need to understand how cars work. The power of each vehicle comes from the engine. We use torque to quantify engine power. The engine transmits power to the wheel through the gearbox, so that the wheel is turned and the entire car can be moved.

Note the following:

1. add or subtract a file system. The so-called gearbox can be abstracted as an array. Each number represents a linear correspondence between the engine force and the wheel force. Cars generally have 6 stalls, so the size of our array should be 6.

The power comes from the engine, and the torque of the wheel is calculated through the current gear to make the wheel rotate.

On the other hand, we capture the rotation speed of the wheel and calculate the current rotation speed of the engine through the current gear position. We have defined the maximum and minimum speed of the engine. The rule is: when the engine reaches the maximum speed, it is added; when it is reduced to the minimum speed, it is reduced.

In this way, the automatic addition and subtraction of vehicles can also be made manual.

2. unity3d wheelCollider (wheel collision tool ). We can give the wheelCollider a force to automatically implement the rolling physical effect. At the same time, we can capture its speed through code, which exactly meets our requirements.

What's more powerful is that we can set the horizontal and vertical friction of the wheel, set the suspension system parameters of the wheel, and through these settings, we can simulate the effects of the car's earthquake avoidance and drift.

Suspension Spring suspension system;

ForwardFriction vertical friction of the wheel (the friction of the car moving forward and backward );

Transverse Friction of the sideWays Friction wheel;

Extremun Slip; Extremum Value; Asymptote Slip; the four values of Asymptote Value are actually two coordinates in the coordinate system. They determine the relationship between the sliding distance and the friction.

The stiffness Factor can adjust the multiples of the friction curve as a whole.

3. unity3d Rigidbody (rigid body ). We can assign a rigid body mass, and the rigid body is affected by gravity. Different vehicle weights will also affect the driving effect of the car! We generally use values in the real world. For example, we can set the mass of a car to around 6000 (unit: kg), including the model size, it is also best to be consistent with the actual size (in meters ). In this way, we can obtain more real physical effects.

 

Here we will share the Code (from csdn ):

Using UnityEngine; using System. collections; public class CarControl: MonoBehaviour {// steering front wheel, used for steering public WheelCollider FrontLeftWheel; public WheelCollider FrontRightWheel; public WheelCollider handle; public WheelCollider BackRightWheel; // gear array public float [] GearRatio; // current gear range public int CurrentGear = 0; public float EngineTorgue = 600366f; public float MaxEngineRPM = 3000.0f; public float MinEngin ERPM = 1000.0f; private float EngineRPM = 0.0f; // Use this for initialization void Start () {// set the vehicle's center of gravity to make the car more stable. Vector3 centerOfMass = rigidbody. centerOfMass; centerOfMass. y =-1.5f; rigidbody. centerOfMass = centerOfMass;} // Update is called once per frame void Update () {// limit the maximum speed of the vehicle. Adjusting the resistance may not be the best practice. But it is very simple and does not interfere with the operation of the physical system. Rigidbody. drag = rigidbody. velocity. magnbench/250; // use the average rpm of two wheels to compute the engine rpm, and then switch the gear to EngineRPM = (FrontLeftWheel. rpm + FrontRightWheel. rpm)/2 * GearRatio [CurrentGear]; ShiftGears (); // sets the shift sound audio. pitch = Mathf. abs (EngineRPM/MaxEngineRPM) + 1.0f; if (audio. pitch> 2.0) {audio. pitch = 2.0f;} // Finally, set the wheel rotation torque. The engine torque is divided by the current position and multiplied by the user input value. // The wheel torque provides the driving force of an automobile. The rotation of the wheel will increase the gear position. BackLeftWheel. motorTorque = EngineTorgue/GearRatio [CurrentGear] * Input. getAxis ("Vertical"); BackRightWheel. motorTorque = EngineTorgue/GearRatio [CurrentGear] * Input. getAxis ("Vertical"); // The rotation angle is any number multiplied by the user input value FrontLeftWheel. steerAngle = 20 * Input. getAxis ("Horizontal"); FrontRightWheel. steerAngle = 20 * Input. getAxis ("Horizontal");} void ShiftGears () {int AppropriateGear = CurrentGear; if (EngineRPM> = MaxEngineRPM) {AppropriateGear = CurrentGear; for (int I = 0; I <GearRatio. length; I ++) {if (FrontLeftWheel. rpm * GearRatio [I] <MaxEngineRPM) {AppropriateGear = I; break ;}currentgear = AppropriateGear;} if (EngineRPM <= MaxEngineRPM) {AppropriateGear = CurrentGear; for (int j = GearRatio. length-1; j> = 0; j --) {if (FrontLeftWheel. rpm * GearRatio [j]> MinEngineRPM) {AppropriateGear = j; break ;}} CurrentGear = AppropriateGear ;}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.