2 * Copyright (c) 2023 Codefoco (codefoco@codefoco.com)
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 using System.ComponentModel;
26 namespace Tizen.NUI.Physics2D.Chipmunk
29 /// Segment queries return where a shape was hit and its surface normal at the point of contact.
30 /// Use <see cref="SegmentQueryInfo.Shape"/> == null to test if a shape was hit. Segment queries
31 /// are like ray casting, but because not all spatial indexes allow processing infinitely long
32 /// ray queries, it's limited to segments. In practice, this is still very fast and you don’t
33 /// need to worry too much about the performance as long as you aren’t using extremely long
34 /// segments for your queries.
36 [EditorBrowsable(EditorBrowsableState.Never)]
37 public sealed class SegmentQueryInfo : IEquatable<SegmentQueryInfo>
39 #pragma warning disable IDE0032
40 private readonly Shape shape;
41 private readonly Vect point;
42 private readonly Vect normal;
43 private readonly double alpha;
44 #pragma warning restore IDE0032
47 /// Shape that was hit, or None if no collision occured.
49 [EditorBrowsable(EditorBrowsableState.Never)]
50 public Shape Shape => shape;
53 /// The point of impact.
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 public Vect Point => point;
59 /// The normal of the surface that was hit.
61 [EditorBrowsable(EditorBrowsableState.Never)]
62 public Vect Normal => normal;
65 /// The normalized distance along the query segment in the range [0, 1]
67 [EditorBrowsable(EditorBrowsableState.Never)]
68 public double Alpha => alpha;
71 /// Construct a Segment query info.
73 [EditorBrowsable(EditorBrowsableState.Never)]
74 public SegmentQueryInfo(Shape s, Vect p, Vect n, double a)
83 /// Return true if the given object is reference-equal to this one.
85 /// <param name="obj"></param>
86 /// <returns></returns>
87 [EditorBrowsable(EditorBrowsableState.Never)]
88 public override bool Equals(object obj)
90 var other = obj as SegmentQueryInfo;
101 /// Return true if both objects are reference-equal to each other.
103 [EditorBrowsable(EditorBrowsableState.Never)]
104 public static bool operator ==(SegmentQueryInfo left, SegmentQueryInfo right)
106 if (ReferenceEquals(left, null))
108 return ReferenceEquals(right, null);
111 return left.Equals(right);
115 /// Return true if both objects are not reference-equal to each other.
117 [EditorBrowsable(EditorBrowsableState.Never)]
118 public static bool operator !=(SegmentQueryInfo a, SegmentQueryInfo b)
124 /// Create a SegmentQuery from a native struct cpSegmentQueryInfo.
126 internal static SegmentQueryInfo FromQueryInfo(cpSegmentQueryInfo queryInfo)
130 if (queryInfo.shape == IntPtr.Zero)
136 shape = Shape.FromHandle(queryInfo.shape);
139 return new SegmentQueryInfo(
147 /// Return true if the fields in both objects are equivalent and the <see cref="alpha"/>
148 /// field is within <see cref="Single.Epsilon"/> of the other's.
150 [EditorBrowsable(EditorBrowsableState.Never)]
151 public bool Equals(SegmentQueryInfo other)
153 if (ReferenceEquals(other, null) ||
154 shape?.Handle != other.shape?.Handle ||
155 point != other.point ||
156 normal != other.normal)
161 return Math.Abs(alpha - other.alpha) < float.Epsilon;
165 /// Get the hash code.
167 [EditorBrowsable(EditorBrowsableState.Never)]
168 public override int GetHashCode()
170 var hashCode = -1275187100;
171 hashCode = hashCode * -1521134295 + shape.GetHashCode();
172 hashCode = hashCode * -1521134295 + point.GetHashCode();
173 hashCode = hashCode * -1521134295 + normal.GetHashCode();
174 hashCode = hashCode * -1521134295 + alpha.GetHashCode();