Exemplo n.º 1
0
        protected override void OnUpdate()
        {
            _createPhysicsWorldSystem.GetOutputDependency().Complete();
            PhysicsWorld world = _createPhysicsWorldSystem.PhysicsWorld;

            var entitys    = this.carEntityQuery.ToEntityArrayAsync(Allocator.TempJob, out var jobhandle);
            var wheelInfos = new NativeArray <tmpData>(entitys.Length, Allocator.TempJob);
            var wheelJob   = new WheelJob()
            {
                physicsWorld  = world,
                Entitys       = entitys,
                wbcs          = GetComponentDataFromEntity <WheelBaseConfig>(),
                pps           = GetComponentDataFromEntity <PreviousParent>(),
                localToWorlds = GetComponentDataFromEntity <LocalToWorld>(),
                wbis          = GetComponentDataFromEntity <WheelBaseInfo>(),
                time          = Time.fixedDeltaTime,
                TmpDatas      = wheelInfos
            };

            wheelJob.Schedule(entitys.Length, 80, jobhandle).Complete();
            var drift = 0f;

            for (int i = 0; i < wheelInfos.Length; i++)
            {
                var data = wheelInfos[i];
                if (data.entity != Entity.Null)
                {
                    EntityManager.SetComponentData(data.entity, data.wbi);
                    world.ApplyImpulse(data.parentID, data.wbi.SuspensionForce, data.LocalToWorld.Position);

                    var v = Input.GetAxis("Vertical");
                    world.ApplyImpulse(data.parentID, data.LocalToWorld.Forward * (30) * v, data.hit.Position + (data.LocalToWorld.Position - data.hit.Position) / 10F);
                    var physicsVelocity = EntityManager.GetComponentData <PhysicsVelocity>(data.parentEntity);
                    var position        = EntityManager.GetComponentData <Translation>(data.parentEntity);
                    var rotation        = EntityManager.GetComponentData <Rotation>(data.parentEntity);

                    var h = Input.GetAxis("Horizontal");
                    world.ApplyAngularForce(data.parentID, h * data.LocalToWorld.Up * 7000);

                    var tmp = Matrix4x4.identity;
                    tmp.SetTRS(position.Value, rotation.Value, Vector3.one);

                    var linearVelocity  = world.GetLinearVelocity(data.parentID);
                    var angularVelocity = world.GetAngularVelocity(data.parentID);

                    float3 localAngleVelocity = tmp.inverse.MultiplyVector(angularVelocity);
                    localAngleVelocity.y   *= 0.9f + (drift / 10);
                    physicsVelocity.Angular = tmp.MultiplyVector(localAngleVelocity);

                    Vector3 localVelocity = tmp.inverse.MultiplyVector(linearVelocity);
                    localVelocity.x       *= 0.9f + (drift / 10);
                    physicsVelocity.Linear = tmp.MultiplyVector(localVelocity);

                    world.SetAngularVelocity(data.parentID, physicsVelocity.Angular);
                    world.SetLinearVelocity(data.parentID, physicsVelocity.Linear);
                }
            }


            entitys.Dispose();
            wheelInfos.Dispose();
        }