public void Execute()
        {
            int index    = 0;
            int maxIndex = DeferredImpulseReader.ForEachCount;

            DeferredImpulseReader.BeginForEachIndex(index++);
            while (DeferredImpulseReader.RemainingItemCount == 0 && index < maxIndex)
            {
                DeferredImpulseReader.BeginForEachIndex(index++);
            }

            while (DeferredImpulseReader.RemainingItemCount > 0)
            {
                var impulse = DeferredImpulseReader.Read <DefferedCharacterControllerImpulse>();
                while (DeferredImpulseReader.RemainingItemCount == 0 && index < maxIndex)
                {
                    DeferredImpulseReader.BeginForEachIndex(index++);
                }

                PhysicsVelocity pv = PhysicsVelocityData[impulse.Entity];
                PhysicsMass     pm = PhysicsMassData[impulse.Entity];
                Translation     t  = TranslationData[impulse.Entity];
                Rotation        r  = RotationData[impulse.Entity];

                if (pm.InverseMass > 0.0f)
                {
                    pv.ApplyImpulse(pm, t, r, impulse.Impulse, impulse.Point);

                    PhysicsVelocityData[impulse.Entity] = pv;
                }
            }
        }
Esempio n. 2
0
    protected override void OnUpdate()
    {
        Entities.ForEach((Entity interactionEntity, ref ProjectileCarInteractionComponent interaction) => {
            PostUpdateCommands.DestroyEntity(interactionEntity);

            if (EntityManager.GetComponentData <ActiveComponent>(interaction.Projectile).IsActive)
            {
                var carPlayerId = EntityManager.GetComponentData <SynchronizedCarComponent>(interaction.Car).PlayerId;

                if (carPlayerId != EntityManager.GetComponentData <OwnerComponent>(interaction.Projectile).OwnerPlayerId)
                {
                    EntityManager.SetComponentData(interaction.Projectile, new ActiveComponent {
                        IsActive = false
                    });
                    interactionServerSystemGroup.PostUpdateCommands.DestroyEntity(interaction.Projectile);

                    if (!EntityManager.GetComponentData <SynchronizedCarComponent>(interaction.Car).IsShieldActive)
                    {
                        var healthComponent = EntityManager.GetComponentData <HealthComponent>(interaction.Car);

                        if (healthComponent.Health > 0)
                        {
                            healthComponent.Health -= SerializedFields.singleton.projectileDamage;

                            if (healthComponent.Health <= 0)
                            {
                                healthComponent.Health = 0;

                                Utils.KillPlayer(interaction.Car, EntityManager, Entities, PostUpdateCommands);
                            }

                            EntityManager.SetComponentData(interaction.Car, healthComponent);
                        }

                        PhysicsVelocity carVelocity = EntityManager.GetComponentData <PhysicsVelocity>(interaction.Car);

                        Translation carPosition = EntityManager.GetComponentData <Translation>(interaction.Car);
                        Rotation carRotation    = EntityManager.GetComponentData <Rotation>(interaction.Car);
                        float3 carTransformUp   = math.mul(carRotation.Value, new float3(0, 1, 0));
                        float3 impactPoint      = EntityManager.GetComponentData <Translation>(interaction.Projectile).Value - EntityManager.GetComponentData <PhysicsVelocity>(interaction.Projectile).Linear * 1 / 60f;

                        carVelocity.ApplyImpulse(
                            EntityManager.GetComponentData <PhysicsMass>(interaction.Car),
                            carPosition,
                            carRotation,
                            math.normalize(Vector3.ProjectOnPlane(EntityManager.GetComponentData <PhysicsVelocity>(interaction.Projectile).Linear, carTransformUp)) * SerializedFields.singleton.projectileImpulse,
                            impactPoint - (float3)(Vector3.Project(impactPoint - carPosition.Value, carTransformUp))
                            );

                        EntityManager.SetComponentData(interaction.Car, carVelocity);
                    }
                }
            }
        });
    }
Esempio n. 3
0
    protected override void OnUpdate()
    {
        Entities
        .WithName("LinearDashpotUpdate")
        .WithBurst()
        .ForEach((in LinearDashpot dashpot) =>
        {
            if (0 == dashpot.strength)
            {
                return;
            }

            var eA = dashpot.localEntity;
            var eB = dashpot.parentEntity;

            var eAIsNull = eA == Entity.Null;
            if (eAIsNull)
            {
                return;
            }
            var eBIsNull = eB == Entity.Null;

            var hasVelocityA = !eAIsNull && HasComponent <PhysicsVelocity>(eA);
            var hasVelocityB = !eBIsNull && HasComponent <PhysicsVelocity>(eB);

            if (!hasVelocityA)
            {
                return;
            }

            Translation positionA = default;
            Rotation rotationA    = new Rotation {
                Value = quaternion.identity
            };
            PhysicsVelocity velocityA = default;
            PhysicsMass massA         = default;

            Translation positionB     = positionA;
            Rotation rotationB        = rotationA;
            PhysicsVelocity velocityB = velocityA;
            PhysicsMass massB         = massA;

            if (HasComponent <Translation>(eA))
            {
                positionA = GetComponent <Translation>(eA);
            }
            if (HasComponent <Rotation>(eA))
            {
                rotationA = GetComponent <Rotation>(eA);
            }
            if (HasComponent <PhysicsVelocity>(eA))
            {
                velocityA = GetComponent <PhysicsVelocity>(eA);
            }
            if (HasComponent <PhysicsMass>(eA))
            {
                massA = GetComponent <PhysicsMass>(eA);
            }

            if (HasComponent <LocalToWorld>(eB))
            {
                // parent could be static and not have a Translation or Rotation
                var worldFromBody = Math.DecomposeRigidBodyTransform(GetComponent <LocalToWorld>(eB).Value);
                positionB         = new Translation {
                    Value = worldFromBody.pos
                };
                rotationB = new Rotation {
                    Value = worldFromBody.rot
                };
            }
            if (HasComponent <Translation>(eB))
            {
                positionB = GetComponent <Translation>(eB);
            }
            if (HasComponent <Rotation>(eB))
            {
                rotationB = GetComponent <Rotation>(eB);
            }
            if (HasComponent <PhysicsVelocity>(eB))
            {
                velocityB = GetComponent <PhysicsVelocity>(eB);
            }
            if (HasComponent <PhysicsMass>(eB))
            {
                massB = GetComponent <PhysicsMass>(eB);
            }


            var posA = math.transform(new RigidTransform(rotationA.Value, positionA.Value), dashpot.localOffset);
            var posB = math.transform(new RigidTransform(rotationB.Value, positionB.Value), dashpot.parentOffset);
            var lvA  = velocityA.GetLinearVelocity(massA, positionA, rotationA, posA);
            var lvB  = velocityB.GetLinearVelocity(massB, positionB, rotationB, posB);

            var impulse = dashpot.strength * (posB - posA) + dashpot.damping * (lvB - lvA);
            impulse     = math.clamp(impulse, new float3(-100.0f), new float3(100.0f));

            velocityA.ApplyImpulse(massA, positionA, rotationA, impulse, posA);
            SetComponent(eA, velocityA);

            if (0 == dashpot.dontApplyImpulseToParent && hasVelocityB)
            {
                velocityB.ApplyImpulse(massB, positionB, rotationB, -impulse, posB);
                SetComponent(eB, velocityB);
            }
        }).Schedule();
Esempio n. 4
0
            public void Execute()
            {
                var index    = 0;
                var maxIndex = DeferredImpulseReader.ForEachCount;

                DeferredImpulseReader.BeginForEachIndex(index++);
                while (DeferredImpulseReader.RemainingItemCount == 0 && index < maxIndex)
                {
                    DeferredImpulseReader.BeginForEachIndex(index++);
                }

                while (DeferredImpulseReader.RemainingItemCount > 0)
                {
                    // Read the data
                    var impulse = DeferredImpulseReader.Read <DeferredCharacterControllerImpulse>();
                    while (DeferredImpulseReader.RemainingItemCount == 0 && index < maxIndex)
                    {
                        DeferredImpulseReader.BeginForEachIndex(index++);
                    }

                    if (!PhysicsMassData.HasComponent(impulse.Entity))
                    {
                        continue;
                    }

                    var pm = PhysicsMassData[impulse.Entity];

                    // Don't apply on kinematic bodies
                    if (!(pm.InverseMass > 0.0f) && CharacterMovePredictedData.HasComponent(impulse.Entity))
                    {
                        var movePredictedData = CharacterMovePredictedData[impulse.Entity];
                        movePredictedData.ImpulseVelocity          = impulse.Impulse / 10.0f;
                        movePredictedData.ImpulseDuration          = 1;
                        CharacterMovePredictedData[impulse.Entity] = movePredictedData;
                        //    FSLog.Info(
                        //    $"impulse.Entity:{impulse.Entity},impulse.Impulse:{impulse.Impulse},Linear:{ep.Linear}");
                    }
                    else
                    {
                        var ep        = VelocityPredictedData[impulse.Entity];
                        var transform = TransformPredictedData[impulse.Entity];

                        var rigidTransform = new PhysicsVelocity
                        {
                            Linear  = ep.Linear,
                            Angular = ep.Angular
                        };
                        // Apply impulse
                        rigidTransform.ApplyImpulse(pm, new Translation {
                            Value = transform.Position
                        }
                                                    , new Rotation {
                            Value = transform.Rotation
                        }, impulse.Impulse / 5, impulse.Point);

                        ep.Linear  = rigidTransform.Linear;
                        ep.Angular = rigidTransform.Angular;
                        // Write back
                        VelocityPredictedData[impulse.Entity] = ep;
                    }
                }
            }