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 /// Contains information about a contact point. <see cref="PointA"/> and <see cref="PointB"/>
30 /// are the contact positions on the surface of each shape. <see cref="Distance"/> is the
31 /// penetration distance of the two, which is a negative value. This value is calculated as
32 /// dot(point2 - point1), normal) and is ignored when you set the
33 /// <see cref="Arbiter.ContactPointSet"/>.
35 [EditorBrowsable(EditorBrowsableState.Never)]
36 public sealed class ContactPoint : IEquatable<ContactPoint>
38 #pragma warning disable IDE0032
39 private readonly Vect pointA;
40 private readonly Vect pointB;
41 private readonly double distance;
42 #pragma warning restore IDE0032
45 /// Point A in the contact point.
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public Vect PointA => pointA;
51 /// Point B in the contact point.
53 [EditorBrowsable(EditorBrowsableState.Never)]
54 public Vect PointB => pointB;
57 /// The penetration distance of the two shapes (as a negative value). This value is
58 /// calculated as dot(point2 - point1), normal) and is ignored when you set the
59 /// <see cref="Arbiter.ContactPointSet"/>.
61 [EditorBrowsable(EditorBrowsableState.Never)]
62 public double Distance => distance;
64 private ContactPoint(Vect pointA, Vect pointB, double distance)
68 this.distance = distance;
72 /// Returns true if neither <see cref="ContactPoint"/> is null and the points are within
73 /// <see cref="float.Epsilon"/> distance of each other.
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 public bool Equals(ContactPoint other)
78 if (ReferenceEquals(other, null))
83 return other.pointA.Equals(pointA)
84 && other.pointB.Equals(pointB)
85 && Math.Abs(other.distance - distance) < float.Epsilon;
90 /// Check if this <see cref="ContactPoint"/> is equal to an object.
92 [EditorBrowsable(EditorBrowsableState.Never)]
93 public override bool Equals(object obj)
95 var other = obj as ContactPoint;
102 return Equals(other);
106 /// Get the <see cref="ContactPoint"/> hash set.
108 [EditorBrowsable(EditorBrowsableState.Never)]
109 public override int GetHashCode()
111 var hashCode = -1285340573;
113 hashCode = hashCode * -1521134295 + pointA.GetHashCode();
114 hashCode = hashCode * -1521134295 + pointB.GetHashCode();
115 hashCode = hashCode * -1521134295 + distance.GetHashCode();
121 /// Returns a string in the format of "a: {pointA}, b: {pointB}, distance: {distance}".
123 [EditorBrowsable(EditorBrowsableState.Never)]
124 public override string ToString()
126 return $"a: {pointA}, b: {pointB}, distance: {distance}";
130 /// Returns true if both <see cref="ContactPoint"/>s are the same object or the dimensions
131 /// are within <see cref="float.Epsilon"/> distance of each other.
133 [EditorBrowsable(EditorBrowsableState.Never)]
134 public static bool operator ==(ContactPoint left, ContactPoint right)
136 if (ReferenceEquals(left, null))
138 return ReferenceEquals(right, null);
141 return left.Equals(right);
145 /// Returns false if both <see cref="ContactPoint"/>s are the same object or the dimensions
146 /// are within <see cref="float.Epsilon"/> distance of each other.
148 [EditorBrowsable(EditorBrowsableState.Never)]
149 public static bool operator !=(ContactPoint left, ContactPoint right)
151 return !(left == right);
154 internal static ContactPoint Empty => new ContactPoint(Vect.Zero, Vect.Zero, 0.0);
156 internal static ContactPoint FromCollidePoint(cpContactPoint contactPoint)
158 return new ContactPoint(
161 contactPoint.distance);
164 internal cpContactPoint ToContactPoint()
166 return new cpContactPoint