unsafe void OnEnable()
    {
        if (this.enabled)
        {
            // Create entity prefab from the game object hierarchy once
            Entity sourceEntity  = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, World.Active);
            var    entityManager = World.Active.EntityManager;

            var positions = new NativeArray <float3>(count, Allocator.Temp);
            var rotations = new NativeArray <quaternion>(count, Allocator.Temp);
            RandomPointsOnCircle(transform.position, range, ref positions, ref rotations);

            PhysicsCollider collider = entityManager.GetComponentData <PhysicsCollider>(sourceEntity);

            Unity.Physics.Material material = ((ConvexColliderHeader *)collider.ColliderPtr)->Material;
            material.Restitution = 1.0f;
            ((ConvexColliderHeader *)collider.ColliderPtr)->Material = material;

            for (int i = 0; i < count; i++)
            {
                var instance = entityManager.Instantiate(sourceEntity);
                entityManager.SetComponentData(instance, new Translation {
                    Value = positions[i]
                });
                entityManager.SetComponentData(instance, new Rotation {
                    Value = rotations[i]
                });
                entityManager.SetComponentData(instance, collider);
            }

            positions.Dispose();
            rotations.Dispose();
        }
    }
示例#2
0
    private static unsafe bool IsTrigger(NativeArray <RigidBody> bodies, int rigidBodyIndex, ColliderKey colliderKey)
    {
        RigidBody hitBody = bodies[rigidBodyIndex];

        hitBody.Collider.Value.GetLeaf(colliderKey, out ChildCollider leafCollider);
        Unity.Physics.Material material = UnsafeUtility.AsRef <ConvexColliderHeader>(leafCollider.Collider).Material;
        return(material.CollisionResponse == CollisionResponsePolicy.RaiseTriggerEvents);
    }
    private unsafe static Entity createSingleEnemy(
        EntityManager entityManager,
        EntityArchetype archetype,
        float3 position,
        quaternion orientation,
        Mesh mesh,
        UnityEngine.Material material)
    {
        Entity entity = entityManager.CreateEntity(archetype);

        //Transform stuff
        entityManager.AddComponentData(entity, new Translation {
            Value = position
        });
        entityManager.SetComponentData(entity, new Rotation {
            Value = orientation
        });

        //custom stuff
        entityManager.AddComponentData(entity, new Temperature {
            Value = 0f
        });

        //RenderMesh & Bounds
        entityManager.AddSharedComponentData(entity, new RenderMesh
        {
            mesh     = mesh,
            material = material
        });
        entityManager.SetComponentData(entity, new RenderBounds {
            Value = mesh.bounds.ToAABB()
        });

        Unity.Physics.Material physicsMaterial = new Unity.Physics.Material {
            CollisionResponse = Unity.Physics.CollisionResponsePolicy.CollideRaiseCollisionEvents
        };


        BlobAssetReference <Collider> collider = Unity.Physics.SphereCollider
                                                 .Create(new SphereGeometry {
            Center = float3.zero,
            Radius = 1,
        },
                                                         CollisionFilter.Default,
                                                         physicsMaterial
                                                         );

        entityManager.SetComponentData(entity, new PhysicsCollider {
            Value = collider
        });

        return(entity);
    }
示例#4
0
        public static CompoundCollider.ColliderBlobInstance[][] GenerateBasePokemonColliderData(CollisionFilter[] collisionFilter = null,
                                                                                                Unity.Physics.Material[] material = null, int[] groupIndex = null, int[] exclude = null)
        {
            if (exclude == null)
            {
                exclude = new int[0];
            }
            if (collisionFilter == null)
            {
                collisionFilter = new CollisionFilter[MaxPokedexNumber];
            }
            if (material == null)
            {
                material = new Unity.Physics.Material[MaxPokedexNumber];
            }
            if (groupIndex == null)
            {
                groupIndex = new int[MaxPokedexNumber];
                for (int i = 0; i < MaxPokedexNumber; i++)
                {
                    groupIndex[i] = 1;
                }
            }
            CompoundCollider.ColliderBlobInstance[][] temp = new CompoundCollider.ColliderBlobInstance[MaxPokedexNumber][];
            PhysicsCollider physicsCollider = new PhysicsCollider {
            };
            NativeArray <CompoundCollider.ColliderBlobInstance> colliders;

            Quaternion rotation = new quaternion();

            for (int i = 1; i < MaxPokedexNumber; i++)
            {
                //	Debug.Log("Height = "+ PokemonBaseEntityData[i].Height+" i = "+i);
                if (collisionFilter[i].Equals(new CollisionFilter()))
                {
                    //	Debug.Log("Creating new Collision Filter");
                    collisionFilter[i] = new CollisionFilter
                    {
                        BelongsTo    = TriggerEventClass.Pokemon | TriggerEventClass.Collidable,
                        CollidesWith = TriggerEventClass.Collidable,
                        GroupIndex   = groupIndex[i]
                    };
                }
                if (material[i].Equals(new Unity.Physics.Material()))
                {
                    material[i] = GetPokemonColliderMaterial(i);
                }
                temp[i] = GeneratePokemonCollider(i, collisionFilter[i], material[i], groupIndex[i]);
            }
            return(temp);
        }
示例#5
0
    private static PhysicsMaterial CreatePhysicsMaterial(float friction, float bounciness, bool isTrigger)
    {
        PhysicsMaterial physicsMaterial = PhysicsMaterial.Default;

        physicsMaterial.Friction = friction;
        physicsMaterial.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Maximum;

        physicsMaterial.Restitution = bounciness;
        physicsMaterial.RestitutionCombinePolicy = PhysicsMaterial.CombinePolicy.Maximum;

        if (isTrigger)
        {
            physicsMaterial.Flags = PhysicsMaterial.MaterialFlags.IsTrigger;
        }

        return(physicsMaterial);
    }
示例#6
0
        public static Unity.Physics.Material GetPokemonColliderMaterial(int pokedexEntry,
                                                                        Unity.Physics.Material.MaterialFlags materialFlags = Unity.Physics.Material.MaterialFlags.EnableMassFactors)
        {
            Unity.Physics.Material material = Unity.Physics.Material.Default;
            switch (pokedexEntry)
            {
            case 101:
                material = Unity.Physics.Material.Default;
                break;

            default:
                Debug.LogWarning("PokemonDataRealted: GetPokemonColliderMaterial: Failed to get a ColliderMaterial for \"" + "\"");
                material = Unity.Physics.Material.Default;
                break;
            }
            return(material);
        }
    /// <summary>
    /// Generates a collider using a PhysicsShapeAuthoring and PhysicsMaterialsExtensionComponent
    /// </summary>
    /// <param name="shape"></param>
    /// <param name="shapeExt"></param>
    /// <param name="offsetPosition"></param>
    /// <param name="offsetRotation"></param>
    /// <returns></returns>
    BlobAssetReference <Unity.Physics.Collider> GenerateCollider(PhysicsShapeAuthoring shape, PhysicsMaterialsExtensionComponent shapeExt, out float3 offsetPosition, out quaternion offsetRotation)
    {
        CollisionFilter filter = new CollisionFilter
        {
            CollidesWith = shape.CollidesWith.Value,
            BelongsTo    = shape.BelongsTo.Value,
            GroupIndex   = 0
        };

        Unity.Physics.Material material = new Unity.Physics.Material
        {
            CollisionResponse        = shape.CollisionResponse,
            CustomTags               = shape.CustomTags.Value,
            Friction                 = shape.Friction.Value,
            FrictionCombinePolicy    = shape.Friction.CombineMode,
            Restitution              = shape.Restitution.Value,
            RestitutionCombinePolicy = shape.Restitution.CombineMode,
            EnableMassFactors        = shapeExt != null ? shapeExt.EnableMassFactors : false,
            EnableSurfaceVelocity    = shapeExt != null ? shapeExt.EnableSurfaceVelocity : false
        };
        switch (shape.ShapeType)
        {
        case ShapeType.Box:
            var boxProperties = shape.GetBoxProperties();
            offsetPosition = boxProperties.Center;
            offsetRotation = boxProperties.Orientation;
            return(Unity.Physics.BoxCollider.Create(boxProperties, filter, material));

        case ShapeType.Capsule:
            var capsuleProperties = shape.GetCapsuleProperties();
            var capsuleGeometry   = new CapsuleGeometry
            {
                Radius  = capsuleProperties.Radius,
                Vertex0 = capsuleProperties.Center - capsuleProperties.Height / 2 - capsuleProperties.Radius,
                Vertex1 = capsuleProperties.Center + capsuleProperties.Height / 2 - capsuleProperties.Radius
            };
            offsetPosition = capsuleProperties.Center;
            offsetRotation = capsuleProperties.Orientation;
            return(Unity.Physics.CapsuleCollider.Create(capsuleGeometry, filter, material));

        case ShapeType.Cylinder:
            var cylinderProperties = shape.GetCylinderProperties();
            offsetPosition = cylinderProperties.Center;
            offsetRotation = cylinderProperties.Orientation;
            return(CylinderCollider.Create(cylinderProperties, filter, material));

        case ShapeType.Sphere:
            var sphereProperties = shape.GetSphereProperties(out var orientation);
            var SphereGeometry   = new SphereGeometry
            {
                Center = sphereProperties.Center,
                Radius = sphereProperties.Radius
            };
            offsetPosition = sphereProperties.Center;
            offsetRotation = quaternion.identity;
            return(Unity.Physics.SphereCollider.Create(SphereGeometry, filter, material));

        case ShapeType.ConvexHull:
            NativeList <float3> points = new NativeList <float3>(Allocator.Temp);
            shape.GetConvexHullProperties(points);
            var ConvexCollider = Unity.Physics.ConvexCollider.Create(points, shape.ConvexHullGenerationParameters, filter, material);
            //    points.Dispose();
            offsetPosition = float3.zero;
            offsetRotation = quaternion.identity;
            return(ConvexCollider);

        case ShapeType.Mesh:
            NativeList <float3> verts = new NativeList <float3>(Allocator.Temp);
            NativeList <int3>   tris  = new NativeList <int3>(Allocator.Temp);
            shape.GetMeshProperties(verts, tris);
            offsetPosition = float3.zero;
            offsetRotation = quaternion.identity;
            return(Unity.Physics.MeshCollider.Create(verts, tris, filter, material));

        default:
            UnityEngine.Debug.LogWarning("GenerateCollider:: cannot generate collider for shapetype \"" + shape.ShapeType + "\"");
            offsetPosition = float3.zero;
            offsetRotation = quaternion.identity;
            return(new BlobAssetReference <Unity.Physics.Collider>());
        }
    }
示例#8
0
    private static PhysicsCollider CreateMeshCollider(Resource.Mesh mesh, float3 scale, CollisionFilter filter, PhysicsMaterial physicsMaterial)
    {
        UnityEngine.Vector3[] v3Verts = Resource.GetMesh(mesh).vertices;
        float3[] float3Verts          = new float3[v3Verts.Length];
        for (int i = 0; i < v3Verts.Length; i++)
        {
            float3Verts[i] = v3Verts[i] * scale;
        }
        NativeArray <float3> nativeVerts = new NativeArray <float3>(float3Verts, Allocator.Temp);

        return(new PhysicsCollider {
            Value = ConvexCollider.Create(nativeVerts, ConvexHullGenerationParameters.Default, filter, physicsMaterial)
        });
    }
示例#9
0
    private static PhysicsCollider CreateCapsuleCollider(float bottomPoint, float topPoint, float radius, CollisionFilter filter, PhysicsMaterial physicsMaterial)
    {
        CapsuleGeometry capsuleGeo = new CapsuleGeometry
        {
            Vertex0 = new float3(0f, bottomPoint, 0f),
            Vertex1 = new float3(0f, topPoint, 0f),
            Radius  = radius
        };

        return(new PhysicsCollider {
            Value = CapsuleCollider.Create(capsuleGeo, filter, physicsMaterial)
        });
    }
示例#10
0
    private static PhysicsCollider CreateBoxCollider(float sizeX, float sizeY, float sizeZ, float centerX, float centerY, float centerZ, CollisionFilter filter, PhysicsMaterial physicsMaterial)
    {
        BoxGeometry boxGeo = new BoxGeometry
        {
            Center      = new float3(centerX, centerY, centerZ),
            Orientation = quaternion.identity,
            Size        = new float3(sizeX, sizeY, sizeZ),
            BevelRadius = 0.05f
        };

        return(new PhysicsCollider {
            Value = BoxCollider.Create(boxGeo, filter, physicsMaterial)
        });
    }
示例#11
0
 /// <summary>
 /// Converts the Physics Material into a readable string
 /// </summary>
 /// <param name="material">material to stringify</param>
 /// <returns>string</returns>
 public static string PhysicsMaterialToString(Unity.Physics.Material material)
 {
     return("Unity.Physics.Material: \nEnableCollision: " + material.EnableCollisionEvents + ", EnableMassFactors: " + material.EnableMassFactors + ", EnableSurfaceVelocity: " + material.EnableSurfaceVelocity + ", IsTrigger: " + material.IsTrigger +
            "\nCustom Tags: " + material.CustomTags.ToString() + "\nFriction: " + material.Friction.ToString() + "\nRestitution: " + material.Restitution.ToString());
 }
示例#12
0
        /// <summary>
        /// creates and returns the pokemoon's PhysicsCollider
        /// </summary>
        /// <param name="pokemonName">Name of the pokemon</param>
        /// <returns>PhysicsCollider</returns>
        public static PhysicsCollider getPokemonPhysicsCollider(string pokemonName, PokemonEntityData ped,
                                                                CollisionFilter collisionFilter = new CollisionFilter(), float scale = 1f, Unity.Physics.Material material = new Unity.Physics.Material(),
                                                                int groupIndex = 1)
        {
            ///FUTURE UPDATE
            ///allow specific colliders to recieve specific filters and materials!

            //needs collision groups
            PhysicsCollider physicsCollider = new PhysicsCollider {
            };
            Quaternion rotation             = new quaternion();

            //if default collision filter is detected then create one realted to the pokemon
            if (collisionFilter.Equals(new CollisionFilter()))
            {
                Debug.Log("Creating new Collision Filter");
                collisionFilter = new CollisionFilter
                {
                    BelongsTo    = TriggerEventClass.Pokemon | TriggerEventClass.Collidable,
                    CollidesWith = TriggerEventClass.Collidable,
                    GroupIndex   = groupIndex
                };
            }
            if (material.Equals(new Unity.Physics.Material()))
            {
                material = GetPokemonColliderMaterial(StringToPokedexEntry(pokemonName));
            }
            switch (pokemonName)
            {
            case "Cubone":
                var colliders = new NativeArray <CompoundCollider.ColliderBlobInstance>(5, Allocator.Temp);
                colliders[0] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.SphereCollider.Create(new SphereGeometry {
                        Center = new float3(0, 0.27f, 0.03f), Radius = 0.225f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3 {
                            x = 0, y = 0, z = 0
                        }, rot = quaternion.identity
                    }
                };
                var a = GenerateCapsuleData(float3.zero, Vector3.right, 0.1f, 0.3f);
                rotation.SetFromToRotation(Vector3.right, new Vector3(0, 90f, 0));
                colliders[1] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry {
                        Vertex0 = a.pointA, Vertex1 = a.pointB, Radius = 0.1f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3(-0.17f, 0.19f, 0), rot = rotation
                    }
                };
                colliders[2] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry {
                        Vertex0 = a.pointA, Vertex1 = a.pointB, Radius = 0.1f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3(0.17f, 0.19f, 0), rot = rotation
                    }
                };
                colliders[3] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.SphereCollider.Create(new SphereGeometry {
                        Center = float3.zero, Radius = 0.23f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3(0, 0.75f, 0.03f), rot = rotation
                    }
                };
                a            = GenerateCapsuleData(float3.zero, Vector3.right, 0.1f, 0.3f);
                rotation     = Quaternion.Euler(0, 90f, 26f);
                colliders[4] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry {
                        Vertex0 = a.pointA, Vertex1 = a.pointB, Radius = 0.1f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3(0, 0.63f, 0.33f), rot = rotation
                    }
                };
                physicsCollider = new PhysicsCollider {
                    Value = CompoundCollider.Create(colliders)
                };
                if (scale > 1f)
                {
                    Debug.LogWarning("Cannot scale Cubone");
                }
                colliders.Dispose();
                break;

            case "Electrode":
                Debug.Log("Creating PHysicwsCollider for Electrode");
                physicsCollider = new PhysicsCollider {
                    Value = Unity.Physics.SphereCollider.Create(new SphereGeometry
                    {
                        Center = float3.zero,
                        Radius = ped.Height / 2 * scale
                    },
                                                                collisionFilter,
                                                                material
                                                                )
                };
                break;

            default:
                Debug.LogError("Failed to find collider for pokemon \"" + pokemonName + "\"");
                physicsCollider = new PhysicsCollider
                {
                    Value = Unity.Physics.SphereCollider.Create(new SphereGeometry
                    {
                        Center = float3.zero,
                        Radius = ped.Height / 2 * scale
                    },
                                                                collisionFilter,
                                                                material
                                                                )
                };
                break;
            }
            //		Debug.Log("Returning Physics Collide for \""+pokemonName+"\"");
            return(physicsCollider);
        }
示例#13
0
        public static CompoundCollider.ColliderBlobInstance[] GeneratePokemonCollider(int i, CollisionFilter collisionFilter, Unity.Physics.Material material, int groupIndex = 1, float scale = 1f)
        {
            CompoundCollider.ColliderBlobInstance[] colliders = new CompoundCollider.ColliderBlobInstance[0];
            Quaternion rotation = new Quaternion();

            switch (i)
            {
            case 104:
                colliders    = new CompoundCollider.ColliderBlobInstance[5];
                colliders[0] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.SphereCollider.Create(new SphereGeometry {
                        Center = new float3(0, 0.27f, 0.03f), Radius = 0.225f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3 {
                            x = 0, y = 0, z = 0
                        }, rot = quaternion.identity
                    }
                };
                var a = GenerateCapsuleData(float3.zero, Vector3.right, 0.1f, 0.3f);
                rotation.SetFromToRotation(Vector3.right, new Vector3(0, 90f, 0));
                colliders[1] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry {
                        Vertex0 = a.pointA, Vertex1 = a.pointB, Radius = 0.1f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3(-0.17f, 0.19f, 0), rot = rotation
                    }
                };
                colliders[2] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry {
                        Vertex0 = a.pointA, Vertex1 = a.pointB, Radius = 0.1f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3(0.17f, 0.19f, 0), rot = rotation
                    }
                };
                colliders[3] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.SphereCollider.Create(new SphereGeometry {
                        Center = float3.zero, Radius = 0.23f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3(0, 0.75f, 0.03f), rot = rotation
                    }
                };
                a            = GenerateCapsuleData(float3.zero, Vector3.right, 0.1f, 0.3f);
                rotation     = Quaternion.Euler(0, 90f, 26f);
                colliders[4] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry {
                        Vertex0 = a.pointA, Vertex1 = a.pointB, Radius = 0.1f
                    }, collisionFilter, material),
                    CompoundFromChild = new RigidTransform {
                        pos = new float3(0, 0.63f, 0.33f), rot = rotation
                    }
                };
                break;

            case 101:
                colliders    = new CompoundCollider.ColliderBlobInstance[1];
                colliders[0] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.SphereCollider.Create(new SphereGeometry
                    {
                        Center = float3.zero,
                        Radius = PokemonBaseEntityData[i].Height / 2
                    },
                                                                   collisionFilter,
                                                                   material
                                                                   )
                };
                break;

            default:
                Debug.LogWarning("Failed to find collider for pokemon \"" + PokedexEntryToString((ushort)i) + "\"");
                colliders    = new CompoundCollider.ColliderBlobInstance[1];
                colliders[0] = new CompoundCollider.ColliderBlobInstance
                {
                    Collider = Unity.Physics.SphereCollider.Create(new SphereGeometry
                    {
                        Center = float3.zero,
                        Radius = PokemonBaseEntityData[i].Height > 0 ? PokemonBaseEntityData[i].Height / 2 : 1f
                    },
                                                                   collisionFilter,
                                                                   material
                                                                   )
                };
                break;
            }
            return(colliders);
        }