source: 2013/30/MiskaK/MW2(My Warfare 2)/Paranneltu Jypeli/Physics2DDotNet/DistanceGrid.cs @ 4507

Revision 4507, 5.2 KB checked in by anlakane, 6 years ago (diff)

Talletus.

Line 
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26#if UseDouble
27using Scalar = System.Double;
28#else
29using Scalar = System.Single;
30#endif
31using System;
32
33using AdvanceMath;
34using AdvanceMath.Geometry2D;
35using Physics2DDotNet.Shapes;
36
37
38namespace Physics2DDotNet
39{
40    /// <summary>
41    /// A class used by some Shape Objects for Narrow Phased collision.
42    /// </summary>
43#if !(WINDOWS_PHONE || XBOX)
44    [Serializable]
45#endif
46    public sealed class DistanceGrid
47    {
48        Scalar gridSpacing;
49        Scalar gridSpacingInv;
50        BoundingRectangle rect;
51        Scalar[][] nodes;
52        public DistanceGrid(IHasArea shape, Scalar spacing)
53        {
54            if (shape == null) { throw new ArgumentNullException("shape"); }
55            if (spacing <= 0) { throw new ArgumentOutOfRangeException("spacing"); }
56            Matrix2x3 ident = Matrix2x3.Identity;
57            shape.CalcBoundingRectangle(ref ident, out this.rect);
58           
59            this.gridSpacing = spacing;
60            this.gridSpacingInv = 1 / spacing;
61            int xSize = (int)Math.Ceiling((rect.Max.X - rect.Min.X) * gridSpacingInv) + 2;
62            int ySize = (int)Math.Ceiling((rect.Max.Y - rect.Min.Y) * gridSpacingInv) + 2;
63
64            this.nodes = new Scalar[xSize][];
65            for (int index = 0; index < xSize; ++index)
66            {
67                this.nodes[index] = new Scalar[ySize];
68            }
69            Vector2D point;
70            point.X = rect.Min.X;
71            for (int x = 0; x < xSize; ++x, point.X += gridSpacing)
72            {
73                point.Y = rect.Min.Y;
74                for (int y = 0; y < ySize; ++y, point.Y += gridSpacing)
75                {
76                    shape.GetDistance(ref point, out nodes[x][y]);
77                }
78            }
79        }
80        public bool TryGetIntersection(Vector2D point, out IntersectionInfo result)
81        {
82            ContainmentType contains;
83            rect.Contains(ref point,out contains);
84            if (contains == ContainmentType.Contains)
85            {
86                int x = (int)Math.Floor((point.X - rect.Min.X) * gridSpacingInv);
87                int y = (int)Math.Floor((point.Y - rect.Min.Y) * gridSpacingInv);
88
89                Scalar bottomLeft = nodes[x][y];
90                Scalar bottomRight = nodes[x + 1][y];
91                Scalar topLeft = nodes[x][y + 1];
92                Scalar topRight = nodes[x + 1][y + 1];
93
94                if (bottomLeft <= 0 ||
95                    bottomRight <= 0 ||
96                    topLeft <= 0 ||
97                    topRight <= 0)
98                {
99                    Scalar xPercent = (point.X - (gridSpacing * x + rect.Min.X)) * gridSpacingInv;
100                    Scalar yPercent = (point.Y - (gridSpacing * y + rect.Min.Y)) * gridSpacingInv;
101
102                    Scalar top, bottom, distance;
103
104                    MathHelper.Lerp(ref topLeft, ref topRight, ref xPercent, out top);
105                    MathHelper.Lerp(ref bottomLeft, ref bottomRight, ref xPercent, out bottom);
106                    MathHelper.Lerp(ref bottom, ref top, ref yPercent, out distance);
107
108                    if (distance <= 0)
109                    {
110                        Scalar right, left;
111
112                        MathHelper.Lerp(ref bottomRight, ref topRight, ref yPercent, out right);
113                        MathHelper.Lerp(ref bottomLeft, ref topLeft, ref yPercent, out left);
114
115                        Vector2D normal;
116                        normal.X = right - left;
117                        normal.Y = top - bottom;
118                        //Vector2D.Normalize(ref normal, out result.Normal);
119                        result.rawNormal = normal;
120                        result.normalized = false;
121                        result.Position = point;
122                        result.Distance = distance;
123                        return true;
124                    }
125                }
126            }
127            result = IntersectionInfo.Zero;
128            return false;
129        }
130    }
131}
Note: See TracBrowser for help on using the repository browser.