protected override unsafe void Start()
    {
        init(float3.zero); // no gravity

        // Enable the joint viewer
        SetDebugDisplay(new Unity.Physics.Authoring.PhysicsDebugDisplayData
        {
            DrawJoints = 1
        });

        Entity *entities = stackalloc Entity[2];

        entities[1] = Entity.Null;
        for (int i = 0; i < 1; i++)
        {
            CollisionFilter filter = new CollisionFilter
            {
                MaskBits     = (uint)(1 << i),
                CategoryBits = (uint)~(1 << (1 - i))
            };
            BlobAssetReference <Collider> collider = BoxCollider.Create(
                float3.zero, Quaternion.identity, new float3(1.0f, 0.2f, 0.2f), 0.0f, filter);
            entities[i] = CreateDynamicBody(float3.zero, quaternion.identity, collider, float3.zero, new float3(0, 1 - i, 0), 1.0f);
        }

        float3 pivot         = float3.zero;
        float3 axis          = new float3(0, 1, 0);
        float3 perpendicular = new float3(0, 0, 1);

        //BlobAssetReference<JointData> hingeData = JointData.CreateLimitedHinge(pivot, pivot, axis, axis, perpendicular, perpendicular, 0.2f, (float)math.PI);
        BlobAssetReference <JointData> hingeData = JointData.CreateLimitedHinge(pivot, pivot, axis, axis, perpendicular, perpendicular, -(float)math.PI, -0.2f);

        CreateJoint(hingeData, entities[0], entities[1]);
    }
        static public BlobAssetReference <Collider> ProduceColliderBlob
            (this UnityEngine.BoxCollider shape, int groupIndex)
        {
            var worldCenter    = math.mul(shape.transform.localToWorldMatrix, new float4(shape.center, 1f));
            var shapeFromWorld = math.inverse(
                new float4x4(new RigidTransform(shape.transform.rotation, shape.transform.position))
                );

            var geometry = new BoxGeometry
            {
                Center      = math.mul(shapeFromWorld, worldCenter).xyz,
                Orientation = quaternion.identity
            };

            var linearScale = (float3)shape.transform.lossyScale;

            geometry.Size = math.abs(shape.size * linearScale);

            geometry.BevelRadius = math.min(ConvexHullGenerationParameters.Default.BevelRadius, math.cmin(geometry.Size) * 0.5f);

            return(BoxCollider.Create(
                       geometry, GetFilter(shape, groupIndex)
                       //ProduceCollisionFilter( shape ),
                       //ProduceMaterial( shape )
                       ));
        }
    protected override unsafe void Start()
    {
        init(); // no gravity

        Entity *entities = stackalloc Entity[2];

        entities[1] = Entity.Null;
        for (int i = 0; i < 1; i++)
        {
            CollisionFilter filter = new CollisionFilter
            {
                CollidesWith = (uint)(1 << i),
                BelongsTo    = (uint)~(1 << (1 - i))
            };
            BlobAssetReference <Collider> collider = BoxCollider.Create(
                new BoxGeometry
            {
                Center      = float3.zero,
                Orientation = quaternion.identity,
                Size        = new float3(1.0f, 0.2f, 0.2f),
                BevelRadius = 0.0f
            },
                filter, Material.Default);
            entities[i] = CreateDynamicBody(float3.zero, quaternion.identity, collider, float3.zero, new float3(0, 1 - i, 0), 1.0f);
        }

        var jointFrame = new BodyFrame {
            Axis = new float3(0, 1, 0), PerpendicularAxis = new float3(0, 0, 1)
        };
        PhysicsJoint hingeData =
            PhysicsJoint.CreateLimitedHinge(jointFrame, jointFrame, new FloatRange(-math.PI, -0.2f));

        CreateJoint(hingeData, entities[0], entities[1]);
    }
Example #4
0
    protected override unsafe void Start()
    {
        init(float3.zero);

        // Enable the joint viewer
        SetDebugDisplay(new Unity.Physics.Authoring.PhysicsDebugDisplayData
        {
            DrawJoints = 1
        });

        BlobAssetReference <Collider> collider = BoxCollider.Create(new BoxGeometry
        {
            Center      = float3.zero,
            Orientation = quaternion.identity,
            Size        = new float3(0.2f, 0.2f, 0.2f),
            BevelRadius = 0.0f
        });

        // Make some 1d angular limit joints
        const int   size  = 6;
        const float speed = 1.0f;
        int         iDbg  = 0;
        int         jDbg  = 3;

        for (int i = 0; i < size; i++)
        {
            quaternion q1 = quaternion.AxisAngle(new float3(1, 0, 0), (float)math.PI * 2.0f * i / size);
            for (int j = 0; j < size; j++)
            {
                if (iDbg >= 0 && jDbg >= 0 && (i != iDbg || j != jDbg))
                {
                    continue;
                }

                // Choose the limited axis
                quaternion q2 = quaternion.AxisAngle(math.mul(q1, new float3(0, 1, 0)),
                                                     (float)math.PI * ((float)j / size));

                // Create a body with some angular velocity about the axis
                float3 pos  = new float3(i - (size - 1) / 2.0f, 0, j - (size - 1) / 2.0f);
                Entity body = CreateDynamicBody(pos, quaternion.identity, collider, float3.zero,
                                                math.mul(q2, new float3(speed, 0, 0)), 1.0f);

                // Create a 1D angular limit about the axis
                float3x3   rotationB  = float3x3.identity;
                float3x3   rotationA  = math.mul(new float3x3(q2), rotationB);
                MTransform transformA = new MTransform(rotationA, new float3(0, 0, 0));
                MTransform transformB = new MTransform(rotationB, pos);
                BlobAssetReference <JointData> jointData = JointData.Create(
                    transformA, transformB,
                    new[] {
                    Constraint.Twist(0, -(float)math.PI / 4.0f, (float)math.PI / 4.0f)
                }
                    );

                CreateJoint(jointData, body, Entity.Null);
            }
        }
    }
    protected override void Start()
    {
        base.Start();
        //base.init(float3.zero); // no gravity

        // Enable the joint viewer
        SetDebugDisplay(new Unity.Physics.Authoring.PhysicsDebugDisplayData
        {
            DrawJoints   = 1,
            DrawContacts = 1
        });

        const uint layerBody     = (1 << 0);
        const uint layerHead     = (1 << 1);
        const uint layerUpperArm = (1 << 2);
        const uint layerForearm  = (1 << 3);
        const uint layerHand     = (1 << 4);
        const uint layerThigh    = (1 << 5);
        //         const uint layerCalf = (1 << 6);
        //         const uint layerFoot = (1 << 7);
        const int layerGround = (1 << 8);

        // Floor
        {
            BlobAssetReference <Collider> collider = BoxCollider.Create(float3.zero, Quaternion.identity, new float3(20.0f, 0.2f, 20.0f), 0.01f, filter(layerGround, 0));
            CreateStaticBody(new float3(0, -0.1f, 0), quaternion.identity, collider);
        }

        // Body
        float3 bodyHalfExtents = new float3(0.2f, 0.3f, 0.075f);
        float3 bodyPosition    = new float3(0, 0.1f, 0);
        Entity body;
        {
            BlobAssetReference <Collider> collider = BoxCollider.Create(
                float3.zero, Quaternion.identity, 2.0f * bodyHalfExtents, 0.01f, filter(layerBody, layerHead | layerUpperArm | layerThigh));
            quaternion q = quaternion.AxisAngle(new float3(1, 0, 0), (float)math.PI / 2.0f);
            body = CreateDynamicBody(bodyPosition, quaternion.identity, collider, float3.zero, float3.zero, 10.0f);
            //body = createStaticBody(bodyPosition, quaternion.identity, collider);
        }

        // Arms
        {
            float handLength = 0.025f;
            float handRadius = 0.055f;
            BlobAssetReference <Collider> handCollider = CapsuleCollider.Create(new float3(-handLength / 2, 0, 0), new float3(handLength / 2, 0, 0), handRadius,
                                                                                filter(layerHand, layerForearm));

            for (int i = 0; i < 1; i++)
            {
                float s = i * 2 - 1.0f;

                float3 handPosition = new float3(0, 0.3f, 0);
                Entity hand         = CreateDynamicBody(handPosition, quaternion.identity, handCollider, float3.zero, float3.zero, 10.0f);
            }
        }
    }
Example #6
0
    protected override unsafe void Start()
    {
        init();

        BlobAssetReference <Collider> collider = BoxCollider.Create(new BoxGeometry
        {
            Center      = float3.zero,
            Orientation = quaternion.identity,
            Size        = new float3(0.2f, 0.2f, 0.2f),
            BevelRadius = 0.0f
        });

        // Make some 1d angular limit joints
        const int   size  = 6;
        const float speed = 1.0f;
        int         iDbg  = 0;
        int         jDbg  = 3;

        for (int i = 0; i < size; i++)
        {
            quaternion q1 = quaternion.AxisAngle(new float3(1, 0, 0), (float)math.PI * 2.0f * i / size);
            for (int j = 0; j < size; j++)
            {
                if (iDbg >= 0 && jDbg >= 0 && (i != iDbg || j != jDbg))
                {
                    continue;
                }

                // Choose the limited axis
                quaternion q2 = quaternion.AxisAngle(math.mul(q1, new float3(0, 1, 0)),
                                                     (float)math.PI * ((float)j / size));

                // Create a body with some angular velocity about the axis
                float3 pos  = new float3(i - (size - 1) / 2.0f, 0, j - (size - 1) / 2.0f);
                Entity body = CreateDynamicBody(pos, quaternion.identity, collider, float3.zero,
                                                math.mul(q2, new float3(speed, 0, 0)), 1.0f);

                // Create a 1D angular limit about the axis
                float3x3 rotationB = float3x3.identity;
                float3x3 rotationA = math.mul(new float3x3(q2), rotationB);
                var      jointData = new PhysicsJoint
                {
                    BodyAFromJoint = new RigidTransform(rotationA, new float3(0, 0, 0)),
                    BodyBFromJoint = new RigidTransform(rotationB, pos)
                };
                jointData.SetConstraints(new FixedList128 <Constraint>
                {
                    Length = 1,
                    [0]    = Constraint.Twist(0, new FloatRange(-math.PI / 4f, math.PI / 4f))
                });

                CreateJoint(jointData, body, Entity.Null);
            }
        }
    }
Example #7
0
    private void Start()
    {
        var entityManager  = World.DefaultGameObjectInjectionWorld.EntityManager;
        var entityAcretype = entityManager.CreateArchetype(
            typeof(LocalToWorld),
            typeof(Rotation),
            typeof(Translation),
            typeof(RenderBounds),
            typeof(WorldRenderBounds),
            typeof(PerInstanceCullingTag),
            typeof(RenderMesh),
            typeof(ChunkWorldRenderBounds),
            typeof(PhysicsCollider)
            );

        var entities = entityManager.CreateEntity(entityAcretype, 1000000, Allocator.Temp);

        for (int x = 0; x < 1000; x++)
        {
            for (int y = 0; y < 1000; y++)
            {
                var entity = entities[x + y * 1000];

                entityManager.SetComponentData(entity, new PhysicsCollider {
                    Value = BoxCollider.Create(new BoxGeometry
                    {
                        Size        = new float3(1f, 1f, 1f),
                        Orientation = Quaternion.identity
                    })
                });

                entityManager.SetSharedComponentData(entity, new RenderMesh {
                    mesh = CubeMesh, material = CubeMaterial
                });
                entityManager.SetComponentData(entity,
                                               new RenderBounds
                {
                    Value = new AABB
                    {
                        Center = float3.zero, Extents = new float3(.5f, .5f, .5f)
                    }
                });

                entityManager.SetComponentData(entity, new Translation()
                {
                    Value = new float3(x, 0, y) + .5f
                });
            }
        }

        entities.Dispose();
    }
Example #8
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)
        });
    }
Example #9
0
        protected override void OnCreate()
        {
            base.OnCreate();
            _buildingCollider = EntityManager.CreateArchetype(
                ComponentType.ReadOnly <BuildingTag>(),
                typeof(Static),
                typeof(LocalToWorld),
                typeof(PhysicsCollider)
                );

            _cubeMesh = AssetData.BuildingCollider.ToRenderMesh();
            _collider = BoxCollider.Create(AssetData.BuildingCollider.BoxGeometry,
                                           new CollisionFilter
            {
                BelongsTo    = CollisionGroups.Buildings,
                CollidesWith = CollisionGroups.Cast,    //CollisionGroups.Drone | CollisionGroups.Cast, TODO ignore drones for now
                GroupIndex   = 0
            });
            var t        = AssetData.BuildingCollider.Parent;
            var entities = new NativeArray <Entity>(t.childCount, Allocator.TempJob);

            EntityManager.CreateEntity(_buildingCollider, entities);
            for (var i = 0; i < t.childCount; ++i)
            {
                var child = t.GetChild(i);
                var ltw   = math.mul(new float4x4(child.rotation, child.position), float4x4.Scale(child.localScale));
                EntityManager.SetComponentData(entities[i], new LocalToWorld {
                    Value = ltw
                });
                EntityManager.SetComponentData(entities[i], new PhysicsCollider {
                    Value = _collider
                });
                EntityManager.AddSharedComponentData(entities[i], _cubeMesh);
            }
            entities.Dispose();
            Enabled = false;
        }
    public static BlobAssetReference <Collider> CreateCollider(UnityEngine.Mesh mesh, ColliderType type)
    {
        switch (type)
        {
        case ColliderType.Sphere: {
            Bounds bounds = mesh.bounds;
            return(SphereCollider.Create(new SphereGeometry {
                    Center = bounds.center,
                    Radius = math.cmax(bounds.extents)
                }));
        }

        case ColliderType.Triangle: {
            return(PolygonCollider.CreateTriangle(mesh.vertices[0], mesh.vertices[1], mesh.vertices[2]));
        }

        case ColliderType.Quad: {
            // We assume the first 2 triangles of the mesh are a quad with a shared edge
            // Work out a correct ordering for the triangle
            int[] orderedIndices = new int[4];

            // Find the vertex in first triangle that is not on the shared edge
            for (int i = 0; i < 3; i++)
            {
                if ((mesh.triangles[i] != mesh.triangles[3]) &&
                    (mesh.triangles[i] != mesh.triangles[4]) &&
                    (mesh.triangles[i] != mesh.triangles[5]))
                {
                    // Push in order or prev, unique, next
                    orderedIndices[0] = mesh.triangles[(i - 1 + 3) % 3];
                    orderedIndices[1] = mesh.triangles[i];
                    orderedIndices[2] = mesh.triangles[(i + 1) % 3];
                    break;
                }
            }

            // Find the vertex in second triangle that is not on a shared edge
            for (int i = 3; i < 6; i++)
            {
                if ((mesh.triangles[i] != orderedIndices[0]) &&
                    (mesh.triangles[i] != orderedIndices[1]) &&
                    (mesh.triangles[i] != orderedIndices[2]))
                {
                    orderedIndices[3] = mesh.triangles[i];
                    break;
                }
            }

            return(PolygonCollider.CreateQuad(
                       mesh.vertices[orderedIndices[0]],
                       mesh.vertices[orderedIndices[1]],
                       mesh.vertices[orderedIndices[2]],
                       mesh.vertices[orderedIndices[3]]));
        }

        case ColliderType.Box: {
            Bounds bounds = mesh.bounds;
            return(BoxCollider.Create(new BoxGeometry {
                    Center = bounds.center,
                    Orientation = quaternion.identity,
                    Size = 2.0f * bounds.extents,
                    BevelRadius = 0.0f
                }));
        }

        case ColliderType.Capsule: {
            Bounds bounds  = mesh.bounds;
            float  min     = math.cmin(bounds.extents);
            float  max     = math.cmax(bounds.extents);
            int    x       = math.select(math.select(2, 1, min == bounds.extents.y), 0, min == bounds.extents.x);
            int    z       = math.select(math.select(2, 1, max == bounds.extents.y), 0, max == bounds.extents.x);
            int    y       = math.select(math.select(2, 1, (1 != x) && (1 != z)), 0, (0 != x) && (0 != z));
            float  radius  = bounds.extents[y];
            float3 vertex0 = bounds.center; vertex0[z] = -(max - radius);
            float3 vertex1 = bounds.center; vertex1[z] = (max - radius);
            return(CapsuleCollider.Create(new CapsuleGeometry {
                    Vertex0 = vertex0,
                    Vertex1 = vertex1,
                    Radius = radius
                }));
        }

        case ColliderType.Cylinder:
            // TODO: need someone to add
            throw new NotImplementedException();

        case ColliderType.Convex: {
            NativeArray <float3> points = new NativeArray <float3>(mesh.vertices.Length, Allocator.TempJob);
            for (int i = 0; i < mesh.vertices.Length; i++)
            {
                points[i] = mesh.vertices[i];
            }
            BlobAssetReference <Collider> collider = ConvexCollider.Create(points, default, CollisionFilter.Default);
            points.Dispose();
            return(collider);
        }
Example #11
0
    private async void CreateRectilinearPlane(int x, int y, float barWidth)
    {
        Material blackMaterial = await Addressables.LoadAssetAsync <Material>(
            PRC.Addressables.Materials.Black
            ).Task;

        Mesh barMesh = Copy(
            (await Addressables.LoadAssetAsync <GameObject>(PRC.Addressables.Prefabs.Cube).Task)
            .GetComponent <MeshFilter>()
            .sharedMesh
            );

        const float barDistance = 0.005f;

        DOTools.Meshing.ScaleMesh(ref barMesh, new float3(barWidth, 1, barWidth));
        barMesh.RecalculateNormals();

        EntityArchetype barArchetype = EntityManager.CreateArchetype(
            // Tags.
            ComponentType.ReadWrite <BarTag>(),
            // Transform.
            ComponentType.ReadWrite <LocalToWorld>(),
            ComponentType.ReadWrite <Translation>(),
            ComponentType.ReadWrite <Rotation>(),
            ComponentType.ReadWrite <Scale>(),
            // Rendering.
            ComponentType.ReadWrite <RenderMesh>(),
            ComponentType.ReadWrite <RenderBounds>(),
            ComponentType.ReadWrite <WorldRenderBounds>(),
            ComponentType.ReadWrite <ChunkWorldRenderBounds>(),
            // Physics.
            ComponentType.ReadWrite <PhysicsCollider>(),
            // Controller data.
            ComponentType.ReadWrite <ControllerInputData>(),
            ComponentType.ReadWrite <ControllerCharacteristics>()
            );

        ControllerProfile profile = await Addressables.LoadAssetAsync <ControllerProfile>(
            PRC.Addressables.Profiles.Controller
            ).Task;

        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                Entity bar      = World.EntityManager.CreateEntity(barArchetype);
                float3 position = new float3(i * (barWidth + barDistance), 0, j * (barWidth + barDistance));
                float3 scale    = new float3(barWidth, 1, barWidth);

                EntityManager.SetSharedComponentData(bar, new RenderMesh()
                {
                    mesh     = barMesh,
                    material = blackMaterial
                });

                EntityManager.SetSharedComponentData(bar, profile.characteristics);

                EntityManager.SetComponentData(bar, new Translation()
                {
                    Value = position
                });

                EntityManager.SetComponentData(bar, new Scale()
                {
                    Value = 1f
                });

                PhysicsCollider collider = new PhysicsCollider()
                {
                    Value = BoxCollider.Create(new BoxGeometry()
                    {
                        Center      = float3.zero,
                        Orientation = quaternion.identity,
                        Size        = scale,
                        BevelRadius = 0.01f
                    })
                };

                EntityManager.SetComponentData(bar, collider);

                /*EntityManager.SetComponentData(bar, PhysicsMass.CreateDynamic(collider.MassProperties, 1f));
                 *
                 * EntityManager.SetComponentData(bar, new PhysicsVelocity {
                 *  Linear = 0,
                 *  Angular = 0
                 * });
                 *
                 * EntityManager.SetComponentData(bar, new PhysicsDamping {
                 *  Linear = 0.01f,
                 *  Angular = 0.05f
                 * });*/
            }
        }
    }
Example #12
0
    protected override void Start()
    {
        base.Start();

        const uint layerBody     = (1 << 0);
        const uint layerHead     = (1 << 1);
        const uint layerUpperArm = (1 << 2);
        const uint layerForearm  = (1 << 3);
        const uint layerHand     = (1 << 4);
        const uint layerThigh    = (1 << 5);
        //         const uint layerCalf = (1 << 6);
        //         const uint layerFoot = (1 << 7);
        const int layerGround = (1 << 8);

        // Floor
        {
            BlobAssetReference <Collider> collider = BoxCollider.Create(new BoxGeometry
            {
                Center      = float3.zero,
                Orientation = quaternion.identity,
                Size        = new float3(20.0f, 0.2f, 20.0f),
                BevelRadius = 0.01f
            },
                                                                        filter(layerGround, 0), Material.Default);
            CreateStaticBody(new float3(0, -0.1f, 0), quaternion.identity, collider);
        }

        // Body
        float3 bodyHalfExtents = new float3(0.2f, 0.3f, 0.075f);
        float3 bodyPosition    = new float3(0, 0.1f, 0);
        Entity body;
        {
            BlobAssetReference <Collider> collider = BoxCollider.Create(new BoxGeometry
            {
                Center      = float3.zero,
                Orientation = quaternion.identity,
                Size        = 2.0f * bodyHalfExtents,
                BevelRadius = 0.01f
            },
                                                                        filter(layerBody, layerHead | layerUpperArm | layerThigh), Material.Default);
            quaternion q = quaternion.AxisAngle(new float3(1, 0, 0), (float)math.PI / 2.0f);
            body = CreateDynamicBody(bodyPosition, quaternion.identity, collider, float3.zero, float3.zero, 10.0f);
            //body = createStaticBody(bodyPosition, quaternion.identity, collider);
        }

        // Arms
        {
            float handLength = 0.025f;
            float handRadius = 0.055f;
            BlobAssetReference <Collider> handCollider = CapsuleCollider.Create(new CapsuleGeometry
            {
                Vertex0 = new float3(-handLength / 2, 0, 0),
                Vertex1 = new float3(handLength / 2, 0, 0),
                Radius  = handRadius
            },
                                                                                filter(layerHand, layerForearm), Material.Default);

            for (int i = 0; i < 1; i++)
            {
                float s = i * 2 - 1.0f;

                float3 handPosition = new float3(0, 0.3f, 0);
                Entity hand         = CreateDynamicBody(handPosition, quaternion.identity, handCollider, float3.zero, float3.zero, 10.0f);
            }
        }
    }