Search Unity

Question Classic Physics equivalent APIs?

Discussion in 'Physics for ECS' started by optimise, Feb 28, 2020.

  1. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,129
    Currently how should I implement Physics.OverlapSphereNonAlloc, Physics.SphereCastNonAlloc equivalent in DOTS Physics? What I want to achieve is when the player attacks or cast skill, create a sphere collider to check collision whether it hits enemy then remove sphere collider after complete the attack in GC free way. The sphere collider able to change its size dynamically based on the type of attack or cast.
     
    Last edited: Feb 28, 2020
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    DTOS itself is GC free (in build and when all safety checks disabled in editor). You can look at docs and see collider cast. Sample implements exactly casting sphere. You can cast it with same From\To points and I guess it will be like overlap
    https://docs.unity3d.com/Packages/com.unity.physics@0.2/manual/collision_queries.html#collider-casts
     
    optimise likes this.
  3. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    70
    You can do OverlapSphere by doing a point query with a positive distance, avoiding the need to create a sphere collider. For example:

    PhysicsWorld.CalculateDistance(new PointDistanceInput { Position = center, MaxDistance = radius }, ... )


    I expect we will add more explicit query functions like OverlapSphere() etc to make this more discoverable.
     
  4. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,129
    Hi @Rory_Havok. Can you provide an example code? I try to get the API working inside SystemBase but keep getting the following error.

    AssertionException: Assertion failure. Value was False
    Expected: True
    UnityEngine.Assertions.Assert.Fail (System.String message, System.String userMessage) (at <94c5f4c38cdc42d2b006f8badef04394>:0)
    UnityEngine.Assertions.Assert.IsTrue (System.Boolean condition, System.String message) (at <94c5f4c38cdc42d2b006f8badef04394>:0)
    UnityEngine.Assertions.Assert.IsTrue (System.Boolean condition) (at <94c5f4c38cdc42d2b006f8badef04394>:0)
    Unity.Physics.Broadphase.CalculateDistance[T] (Unity.Physics.PointDistanceInput input, Unity.Collections.NativeSlice`1[T] rigidBodies, T& collector) (at Library/PackageCache/com.unity.physics@0.2.5-preview.1/Unity.Physics/Collision/World/Broadphase.cs:401)
    Unity.Physics.CollisionWorld.CalculateDistance[T] (Unity.Physics.PointDistanceInput input, T& collector) (at Library/PackageCache/com.unity.physics@0.2.5-preview.1/Unity.Physics/Collision/World/CollisionWorld.cs:157)
    Unity.Physics.PhysicsWorld.CalculateDistance[T] (Unity.Physics.PointDistanceInput input, T& collector) (at Library/PackageCache/com.unity.physics@0.2.5-preview.1/Unity.Physics/Dynamics/World/PhysicsWorld.cs:91)
    Unity.Physics.QueryWrappers.CalculateDistance[T] (T& target, Unity.Physics.PointDistanceInput input) (at Library/PackageCache/com.unity.physics@0.2.5-preview.1/Unity.Physics/Collision/Queries/Collidable.cs:157)
    Unity.Physics.PhysicsWorld.CalculateDistance (Unity.Physics.PointDistanceInput input) (at Library/PackageCache/com.unity.physics@0.2.5-preview.1/Unity.Physics/Dynamics/World/PhysicsWorld.cs:86)
    PhysicsOverlapSphereSystem.OnUpdate () (at Assets/Sources/Physics/PhysicsOverlapSphereSystem.cs:25)
    Unity.Entities.SystemBase.Update () (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/SystemBase.cs:150)
    Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:182)
    UnityEngine.Debug:LogException(Exception)
    Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/Stubs/Unity/Debug.cs:19)
    Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:186)
    Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:169)
    Unity.NetCode.ServerSimulationSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.netcode@0.1.0-preview.6/Runtime/ClientServerWorld/ServerSimulationSystemGroup.cs:72)
    Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystem.cs:107)
    Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:182)
    Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:169)
    Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystem.cs:107)
    Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:182)
    Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:169)
    Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystem.cs:107)
    Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ScriptBehaviourUpdateOrder.cs:152)
     
    Last edited: Mar 2, 2020
  5. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    70
    Not right now, but I expect you are not setting the collision filter in the input - just set input.Filter to e.g. CollisionFilter.Default.