public Physics2DUtility.Physics2DWorldDataImpl GetPhysicsWorldData(bool canInit)
        {
            var scene = ParentScene;

            if (scene != null)
            {
                return(Physics2DUtility.GetWorldData(scene, canInit));
            }
            return(null);
        }
Exemple #2
0
        public static void Physics2DRayTest(this Component_Scene scene, Physics2DRayTestItem[] items)
        {
            var worldData = Physics2DUtility.GetWorldData(scene, false);

            if (worldData != null)
            {
                //!!!!threading

                foreach (var item in items)
                {
                    var result = new List <Physics2DRayTestItem.ResultItem>();

                    var point1 = Physics2DUtility.Convert(item.Ray.Origin);
                    var point2 = Physics2DUtility.Convert(item.Ray.GetEndPoint());

                    worldData.world.RayCast((f, p, n, fr) =>
                    {
                        if (ShouldCollide(f, item))
                        {
                            var shape = f.Tag as Component_CollisionShape2D;
                            if (shape != null)
                            {
                                var resultItem           = new Physics2DRayTestItem.ResultItem();
                                resultItem.Body          = shape.ParentRigidBody;
                                resultItem.Shape         = shape;
                                resultItem.Position      = Physics2DUtility.Convert(p);
                                resultItem.Normal        = Physics2DUtility.Convert(n);
                                resultItem.DistanceScale = fr;

                                result.Add(resultItem);

                                if (item.Mode == Physics2DRayTestItem.ModeEnum.One)
                                {
                                    return(0);
                                }
                            }
                        }

                        return(1);
                    }, point1, point2);

                    RayTest_PostProcess(item, result);
                }
            }
            else
            {
                foreach (var item in items)
                {
                    item.Result = Array.Empty <Physics2DRayTestItem.ResultItem>();
                }
            }
        }
Exemple #3
0
        /////////////////////////////////////////

        public static void Physics2DContactTest(this Component_Scene scene, Physics2DContactTestItem[] items)
        {
            var worldData = Physics2DUtility.GetWorldData(scene, false);

            if (worldData != null)
            {
                foreach (var item in items)
                {
                    var aabb     = new AABB(Physics2DUtility.Convert(item.Bounds.Minimum), Physics2DUtility.Convert(item.Bounds.Maximum));
                    var fixtures = worldData.world.QueryAABB(ref aabb);

                    var result = new List <Physics2DContactTestItem.ResultItem>(fixtures.Count);

                    bool Contains(Component_PhysicalBody2D body, Component_CollisionShape2D shape)
                    {
                        for (int n = 0; n < result.Count; n++)
                        {
                            if (result[n].Body == body && result[n].Shape == shape)
                            {
                                return(true);
                            }
                        }
                        return(false);
                    }

                    PolygonShape polygonShape = null;
                    if (item.Convex != null)
                    {
                        var vertices = new Vertices(item.Convex.Length);
                        foreach (var p in item.Convex)
                        {
                            vertices.Add(Physics2DUtility.Convert(p));
                        }
                        polygonShape = new PolygonShape(vertices, 0);
                    }

                    foreach (var fixture in fixtures)
                    {
                        if (ShouldCollide(fixture, item))
                        {
                            bool skip = false;

                            if (polygonShape != null)
                            {
                                fixture.Body.GetTransform(out var bodyTransform);

                                var identity = tainicom.Aether.Physics2D.Common.Transform.Identity;
                                if (!Collision.TestOverlap(polygonShape, 0, fixture.Shape, 0, ref identity, ref bodyTransform))
                                {
                                    skip = true;
                                }
                            }

                            if (!skip)
                            {
                                var shape = fixture.Tag as Component_CollisionShape2D;
                                if (shape != null)
                                {
                                    var body = shape.ParentRigidBody;
                                    if (!Contains(body, shape))
                                    {
                                        var resultItem = new Physics2DContactTestItem.ResultItem();
                                        resultItem.Body  = body;
                                        resultItem.Shape = shape;
                                        result.Add(resultItem);

                                        if (item.Mode == Physics2DContactTestItem.ModeEnum.One)
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    item.Result = result.ToArray();
                }
            }
            else
            {
                foreach (var item in items)
                {
                    item.Result = Array.Empty <Physics2DContactTestItem.ResultItem>();
                }
            }
        }