Esempio n. 1
0
 /// <summary>
 /// Create a new entity for a joint associated with the specified authoring component.
 /// Other systems can later find this joint by using <see cref="GetJointEntities"/>.
 /// Use this method when the behavior specified by the authoring component can be described with a single <see cref="PhysicsJoint"/> (see also <seealso cref="CreateJointEntities"/>).
 /// The new entity will have all required component types as well as a readable name in the Entity Debugger.
 /// </summary>
 /// <param name="authoringComponent">An authoring component being converted.</param>
 /// <param name="constrainedBodyPair">A component describing the bodies constrained by the joint.</param>
 /// <param name="joint">The reference frames and set of constraints to apply to the bodies.</param>
 /// <returns>A new entity with all required component types.</returns>
 public Entity CreateJointEntity(
     UnityComponent authoringComponent, PhysicsConstrainedBodyPair constrainedBodyPair, PhysicsJoint joint
     )
 {
     using (var joints = new NativeArray <PhysicsJoint>(1, Allocator.Temp)
     {
         [0] = joint
     })
         using (var jointEntities = new NativeList <Entity>(1, Allocator.Temp))
         {
             CreateJointEntities(authoringComponent, constrainedBodyPair, joints, jointEntities);
             return(jointEntities[0]);
         }
 }
Esempio n. 2
0
    protected override void OnUpdate()
    {
        var deltaTime = Time.DeltaTime;

        Entities
        .WithName("InvalidPhysicsJointTimerEvent")
        .ForEach((ref InvalidPhysicsJointSwapTimerEvent timer) =>
        {
            timer.Tick(deltaTime);
        }).Run();

        // swap motion type
        {
            Entities
            .WithName("InvalidPhysicsJointSwapBodies")
            .WithAll <InvalidPhysicsJointSwapBodies>()
            .ForEach((ref InvalidPhysicsJointSwapTimerEvent timer, ref PhysicsConstrainedBodyPair bodyPair) =>
            {
                if (timer.Fired(true))
                {
                    bodyPair = new PhysicsConstrainedBodyPair(bodyPair.EntityB, bodyPair.EntityA, bodyPair.EnableCollision != 0);
                }
            }).Run();


            using (var commandBuffer = new EntityCommandBuffer(Allocator.TempJob))
            {
                Entities
                .WithName("InvalidPhysicsJointSwapMotionType_S2D")
                .WithoutBurst()
                .WithAll <InvalidPhysicsJointSwapMotionType>()
                .WithNone <PhysicsVelocity>()
                .ForEach((Entity entity, ref InvalidPhysicsJointSwapTimerEvent timer) =>
                {
                    if (timer.Fired(true))
                    {
                        commandBuffer.AddComponent(entity, new PhysicsVelocity {
                        });
                        commandBuffer.AddComponent(entity, PhysicsMass.CreateDynamic(MassProperties.UnitSphere, 1));
                    }
                }).Run();

                Entities
                .WithName("InvalidPhysicsJointSwapMotionType_D2S")
                .WithoutBurst()
                .ForEach((Entity entity,
                          ref InvalidPhysicsJointSwapMotionType swapMotionType,
                          ref InvalidPhysicsJointSwapTimerEvent timer,
                          ref Translation position, ref Rotation rotation,
                          in PhysicsVelocity physicsVelocity) =>
                {
                    if (timer.Fired(true))
                    {
                        position.Value = swapMotionType.OriginalTransform.pos;
                        rotation.Value = swapMotionType.OriginalTransform.rot;
                        commandBuffer.RemoveComponent <PhysicsVelocity>(entity);
                        commandBuffer.RemoveComponent <PhysicsMass>(entity);
                    }
                }).Run();

                Entities
                .WithName("InvalidPhysicsJointKillBodies")
                .WithoutBurst()
                .WithAll <InvalidPhysicsJointKillBodies>()
                .ForEach((Entity entity,
                          ref InvalidPhysicsJointSwapTimerEvent timer,
                          ref Translation position, ref Rotation rotation,
                          in PhysicsVelocity physicsVelocity) =>
                {
                    if (timer.Fired(true))
                    {
                        commandBuffer.DestroyEntity(entity);
                    }
                }).Run();

                commandBuffer.Playback(EntityManager);
            }
        }
    }
Esempio n. 3
0
        /// <summary>
        /// Create several new entities for a joint associated with the specified authoring component.
        /// Other systems can later find these joints by using <see cref="GetJointEntities"/>.
        /// Use this method instead of <see cref="CreateJointEntity"/> when the behavior specified by the authoring component requires multiple <see cref="PhysicsJoint"/> components to describe.
        /// The new entities will have all required component types, <see cref="PhysiscsJointCompanion"/> buffers, as well as readable names in the Entity Debugger.
        /// </summary>
        /// <param name="authoringComponent">An authoring component being converted.</param>
        /// <param name="constrainedBodyPair">A component describing the bodies constrained by the joints.</param>
        /// <param name="joints">The set of reference frames and corresponding constraints to apply to the bodies.</param>
        /// <param name="newJointEntities">An optional list to populate with all of the new entities.</param>
        public void CreateJointEntities(
            UnityComponent authoringComponent,
            PhysicsConstrainedBodyPair constrainedBodyPair,
            NativeArray <PhysicsJoint> joints,
            NativeList <Entity> newJointEntities = default
            )
        {
            if (!joints.IsCreated || joints.Length == 0)
            {
                return;
            }

            if (newJointEntities.IsCreated)
            {
                newJointEntities.Clear();
            }
            else
            {
                newJointEntities = new NativeList <Entity>(joints.Length, Allocator.Temp);
            }

            // find existing joints associated with the authoring component, if any
            if (!m_JointEntitiesPerAuthoringComponent.TryGetValue(authoringComponent, out var allJointEntities))
            {
                m_JointEntitiesPerAuthoringComponent[authoringComponent]
                      = allJointEntities
                      = new NativeList <Entity>(joints.Length, Allocator.Persistent);
            }

            // create all new joints
            var multipleJoints = joints.Length > 1;

#if UNITY_EDITOR
            var nameEntityA = DstEntityManager.GetName(constrainedBodyPair.EntityA);
            var nameEntityB = constrainedBodyPair.EntityB == Entity.Null
                ? "PhysicsWorld"
                : DstEntityManager.GetName(constrainedBodyPair.EntityB);
            var baseName = $"Joining {nameEntityA} + {nameEntityB}";
#endif
            for (var i = 0; i < joints.Length; ++i)
            {
                var jointEntity = CreateAdditionalEntity(authoringComponent);
#if UNITY_EDITOR
                DstEntityManager.SetName(jointEntity, $"{baseName} ({joints[i].JointType})");
#endif

                DstEntityManager.AddComponents(
                    jointEntity, multipleJoints ? k_JointComponentsMultiple : k_JointComponentsSingle
                    );

                DstEntityManager.SetComponentData(jointEntity, constrainedBodyPair);
                DstEntityManager.SetComponentData(jointEntity, joints[i]);

                newJointEntities.Add(jointEntity);
                allJointEntities.Add(jointEntity);
            }

            if (!multipleJoints)
            {
                return;
            }

            // set companion buffers for new joints
            for (var i = 0; i < joints.Length; ++i)
            {
                var companions = DstEntityManager.GetBuffer <PhysicsJointCompanion>(newJointEntities[i]);
                for (var j = 0; j < joints.Length; ++j)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    companions.Add(new PhysicsJointCompanion {
                        JointEntity = newJointEntities[j]
                    });
                }
            }
        }