(五)flax Engine 游戏引擎——载具

2021SC@SDUSC        

上次我们讲述了flax Engine游戏引擎中的最基本的组成结构刚体和其源代码的分析。本次我将讲述flax Engine游戏引擎中的WheeledVehicle(载具)的相应的源代码的分析。 

        按照惯例本次我将针对flaxEngine 游戏引擎中的WheeledVehicle(载具)中的WheeledVehicle.h文件进行源代码的分析工作。

API_CLASS() class FLAXENGINE_API WheeledVehicle : public RigidBody

        首先WheeledVehicle 载具简而言之就汽车,这个WheeledVehicle是提供了汽车的一个模板提供给游戏的编制者,编制者用模板来制造属于自己的汽车。其基本思想是使用刚体制作其基本形状用碰撞器代表其地盘形状和车轮以此来模拟相应的碰撞情况。WheeledVehicle类的定义如上,继承了刚体的相应内容。

 API_ENUM() enum class DriveTypes
    {
        // Four-wheel drive. Any additional wheels are non-drivable. Optimized for 4-wheel cars.
        Drive4W,
        // N-wheel drive. Up to 20 drivable wheels. Suits generic wheels configurations.
        DriveNW,
        // Non-drivable vehicle.
        NoDrive,
    };
  API_ENUM() enum class DifferentialTypes
    {
        // Limited slip differential for car with 4 driven wheels.
        LimitedSlip4W,
        // Limited slip differential for car with front-wheel drive.
        LimitedSlipFrontDrive,
        // Limited slip differential for car with rear-wheel drive.
        LimitedSlipRearDrive,
        // Open differential for car with 4 driven wheels.
        Open4W,
        // Open differential for car with front-wheel drive.
        OpenFrontDrive,
        // Open differential for car with rear-wheel drive.
        OpenRearDrive,
    };

这个代码代表了编制者可能用到的载具的类型 

1:Drive4W:代表四轮驱动,并且针对四轮车进行优化。

2:N-wheel:针对不同一般车轮,最大可以使用20轮驱动。

3:NoDrive:不动车,也就是观赏车,针对游戏中场景车(不用其内部细节,简化编程)。

4:前轮驱动汽车的限滑差速器。

5:后轮驱动汽车的限滑差速器。

6:4个驱动轮汽车的开式差速器。

7:前轮驱动汽车的开式差速器。

8:后轮驱动汽车的开式差速器

  API_STRUCT() struct EngineSettings : ISerializable
    {
    DECLARE_SCRIPTING_TYPE_MINIMAL(EngineSettings);
    API_AUTO_SERIALIZATION();

        /// <summary>
        /// Moment of inertia of the engine around the axis of rotation. Specified in kilograms metres-squared (kg m^2).
        /// </summary>
        API_FIELD() float MOI = 1.0f;

        /// <summary>
        /// Maximum torque available to apply to the engine when the accelerator pedal is at maximum. Specified in kilograms metres-squared per second-squared (kg m^2 s^-2).
        /// </summary>
        API_FIELD() float MaxTorque = 500.0f;

        /// <summary>
        /// Maximum rotation speed of the engine (Revolutions Per Minute is the number of turns in one minute).
        /// </summary>
        API_FIELD() float MaxRotationSpeed = 6000.0f;
    };

        上面的代码段是针对汽车引擎的中“引擎”的源代码。

1:定义了发动机绕旋转轴的惯性矩。规定单位为千克平方米

2:定义了当油门踏板处于最大位置时,可应用于发动机的最大扭矩。规定单位为千克米平方/秒平方

3:定义了发动机的最大转速(每分钟转数是一分钟的转数)

 API_STRUCT() struct DifferentialSettings : ISerializable
    {
    DECLARE_SCRIPTING_TYPE_MINIMAL(DifferentialSettings);
    API_AUTO_SERIALIZATION();

        /// <summary>
        /// Type of differential.
        /// </summary>
        API_FIELD() DifferentialTypes Type = DifferentialTypes::LimitedSlip4W;

        /// <summary>
        /// Ratio of torque split between front and rear (higher then 0.5 means more to front, smaller than 0.5 means more to rear). Only applied to LimitedSlip4W and Open4W.
        /// </summary>
        API_FIELD(Attributes="Limit(0, 1)") float FrontRearSplit = 0.45f;

        /// <summary>
        /// Ratio of torque split between front-left and front-right (higher then 0.5 means more to front-left, smaller than 0.5 means more to front-right). Only applied to LimitedSlip4W and Open4W and LimitedSlipFrontDrive.
        /// </summary>
        API_FIELD(Attributes="Limit(0, 1)") float FrontLeftRightSplit = 0.5f;

        /// <summary>
        /// Ratio of torque split between rear-left and rear-right (higher then 0.5 means more to rear-left, smaller than 0.5 means more to rear-right). Only applied to LimitedSlip4W and Open4W and LimitedSlipRearDrive.
        /// </summary>
        API_FIELD(Attributes="Limit(0, 1)") float RearLeftRightSplit = 0.5f;

        /// <summary>
        /// Maximum allowed ratio of average front wheel rotation speed and rear wheel rotation speeds. The differential will divert more torque to the slower wheels when the bias is exceeded. Only applied to LimitedSlip4W.
        /// </summary>
        API_FIELD(Attributes="Limit(1)") float CentreBias = 1.3f;

        /// <summary>
        /// Maximum allowed ratio of front-left and front-right wheel rotation speeds. The differential will divert more torque to the slower wheel when the bias is exceeded. Only applied to LimitedSlip4W and LimitedSlipFrontDrive.
        /// </summary>
        API_FIELD(Attributes="Limit(1)") float FrontBias = 1.3f;

        /// <summary>
        /// Maximum allowed ratio of rear-left and rear-right wheel rotation speeds. The differential will divert more torque to the slower wheel when the bias is exceeded. Only applied to LimitedSlip4W and LimitedSlipRearDrive.
        /// </summary>
        API_FIELD(Attributes="Limit(1)") float RearBias = 1.3f;
    };

上述代码代表车辆差速器设置

1:差速器的类型

2:前后扭矩分配比(大于0.5表示前向扭矩更大,小于0.5表示后向扭矩更大)。

3:左前和右前之间的扭矩分配比(大于0.5表示左前更大,小于0.5表示右前更大)。

4:左后和右后之间的扭矩分配比(大于0.5表示左后更大,小于0.5表示右后更大)。

5: 左前轮和右前轮转速的最大允许比率。当超过偏差时,差速器会将更多扭矩转移到较慢的车轮上。仅适用于受限SLIP4W和受限SLIPFRONT驱动器。

6:左后轮和右后轮转速的最大允许比率。当超过偏差时,差速器会将更多扭矩转移到较慢的车轮上。

 API_STRUCT() struct GearboxSettings : ISerializable
    {
    DECLARE_SCRIPTING_TYPE_MINIMAL(GearboxSettings);
    API_AUTO_SERIALIZATION();

        /// <summary>
        /// If enabled the vehicle gears will be changes automatically, otherwise it's fully manual.
        /// </summary>
        API_FIELD() bool AutoGear = true;

        /// <summary>
        /// Time it takes to switch gear. Specified in seconds (s).
        /// </summary>
        API_FIELD(Attributes="Limit(0)") float SwitchTime = 0.5f;

        /// <summary>
        /// Strength of clutch. A stronger clutch more strongly couples the engine to the wheels, while a clutch of strength zero completely decouples the engine from the wheels.
        /// Stronger clutches more quickly bring the wheels and engine into equilibrium, while weaker clutches take longer, resulting in periods of clutch slip and delays in power transmission from the engine to the wheels.
        /// Specified in kilograms metres-squared per second (kg m^2 s^-1).
        /// </summary>
        API_FIELD(Attributes="Limit(0)") float ClutchStrength = 10.0f;
    };

上述代码是车辆变速器装置。

1:如果启用,车辆档位将自动改变,否则完全手动

2:切换档位所需的时间。以秒为单位指定

3:离合器的强度。更强的离合器更能将发动机与车轮紧密耦合,而强度为零的离合器则能将发动机与车轮完全分离。更强的离合器可以更快地使车轮和发动机达到平衡,而较弱的离合器需要更长的时间,从而导致离合器打滑以及发动机到车轮的动力传输延迟。

  API_ENUM() enum class WheelTypes
    {
        // Left wheel of the front axle.
        FrontLeft,
        // Right wheel of the front axle.
        FrontRight,
        // Left wheel of the rear axle.
        RearLeft,
        // Right wheel of the rear axle.
        RearRight,
        // Non-drivable wheel.
        NoDrive,
    };

上述代码是车轮类型:

分别代表 前桥的左轮,前桥的右车轮。后桥的左轮。后桥的右车轮。不可驱动车轮。

 API_STRUCT() struct Wheel : ISerializable
    {
    DECLARE_SCRIPTING_TYPE_MINIMAL(Wheel);
    API_AUTO_SERIALIZATION();

        /// <summary>
        /// Wheel placement type.
        /// </summary>
        API_FIELD(Attributes="EditorOrder(0)") WheelTypes Type = WheelTypes::FrontLeft;

        /// <summary>
        /// Combined mass of the wheel and the tire in kg. Typically, a wheel has mass between 20Kg and 80Kg but can be lower and higher depending on the vehicle.
        /// </summary>
        API_FIELD(Attributes="EditorOrder(1)") float Mass = 20.0f;

        /// <summary>

上述是轮胎类型代码:

在此我举例说明:首先是车轮和轮胎的组合质量(kg)。通常,一个车轮的质量介于20千克和80千克之间,但根据车辆的不同,可以更低或更高。其次是车轮中心和轮胎外缘之间的距离(米)

 API_STRUCT() struct WheelState
    {
    DECLARE_SCRIPTING_TYPE_MINIMAL(WheelState);

        /// <summary>
        /// True if suspension travel limits forbid the wheel from touching the drivable surface.
        /// </summary>
        API_FIELD() bool IsInAir = false;

        /// <summary>
        /// The wheel is not in the air then it's set to the collider of the driving surface under the corresponding vehicle wheel.
        /// </summary>
        API_FIELD() PhysicsColliderActor* TireContactCollider = nullptr;

上述代码车辆车轮动态模拟状态容器

1:如果悬架行程限制禁止车轮接触可行驶表面,则为真

2:车轮不在空中,然后将其设置到相应车轮下驾驶表面的碰撞器

3:车轮不在空中,然后将其设置为轮胎撞击的可行驶表面上的点

4:车轮不在空气中,然后在轮胎撞击的可驾驶表面上设置为正常

5:计算后轮胎类型和表面类型组合的轮胎所经历的摩擦力

6:关于“向上”矢量的车轮转向角(以度为单位),考虑输入转向和前束,以及阿克曼转向校正

7:指定车轮围绕滚动轴的旋转角度

本次分析的是flaxEngine游戏引擎中WheeledVehicle.h文件,下次我将分析WheeledVehicle.c文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flax is a popular deep learning library built on top of JAX, a high-performance scientific computing library for Python. It provides an easy-to-use API for defining and training neural network models, while leveraging the speed and efficiency of JAX's Just-In-Time (JIT) compilation and automatic differentiation. In the context of Flax, a model typically refers to a class or a set of functions that define the architecture of a neural network. It includes layers, activation functions, and parameters that are learned during training. Flax supports various types of models, such as feedforward networks, convolutional neural networks (CNNs), recurrent neural networks (RNNs), transformers, and more. Here are some key aspects of the Flax Model: 1. **Structured State**: Flax uses a structured state format, where all learnable parameters are stored in a single object, making it easier to manage and apply weight updates. 2. **Functional API**: The library encourages functional programming style, allowing users to create complex models using compositions of simple functions, which makes code more modular and testable. 3. **Module System**: Flax uses a hierarchical module system that allows you to create and reuse sub-modules, enabling code reusability and organization. 4. **Modularity**: Models are composed of individual modules, each with their own forward pass function, making it simple to experiment with different architectures. 5. **Dynamic Shapes**: Flax handles variable-size inputs and dynamic shapes efficiently, which is crucial for sequence modeling tasks.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值