Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Ray.cs
Go to the documentation of this file.
1using System;
5using Microsoft.Xna.Framework.Design;
6
8
10[TypeConverter(typeof(RayConverter))]
11public struct Ray : IEquatable<Ray>
12{
13 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
15
16 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
18
19 public Ray(Vector3 position, Vector3 direction)
20 {
21 Position = position;
22 Direction = direction;
23 }
24
25 public bool Equals(Ray other)
26 {
27 if (Position.X == other.Position.X && Position.Y == other.Position.Y && Position.Z == other.Position.Z && Direction.X == other.Direction.X && Direction.Y == other.Direction.Y)
28 {
29 return Direction.Z == other.Direction.Z;
30 }
31 return false;
32 }
33
34 public override bool Equals(object obj)
35 {
36 bool result = false;
37 if (obj != null && obj is Ray)
38 {
39 result = Equals((Ray)obj);
40 }
41 return result;
42 }
43
44 public override int GetHashCode()
45 {
46 return Position.GetHashCode() + Direction.GetHashCode();
47 }
48
49 public override string ToString()
50 {
51 CultureInfo currentCulture = CultureInfo.CurrentCulture;
52 return string.Format(currentCulture, "{{Position:{0} Direction:{1}}}", new object[2]
53 {
56 });
57 }
58
59 public float? Intersects(BoundingBox box)
60 {
61 return box.Intersects(this);
62 }
63
64 public void Intersects(ref BoundingBox box, out float? result)
65 {
66 box.Intersects(ref this, out result);
67 }
68
69 public float? Intersects(BoundingFrustum frustum)
70 {
71 if (frustum == null)
72 {
73 throw new ArgumentNullException("frustum");
74 }
75 return frustum.Intersects(this);
76 }
77
78 public float? Intersects(Plane plane)
79 {
80 float num = plane.Normal.X * Direction.X + plane.Normal.Y * Direction.Y + plane.Normal.Z * Direction.Z;
81 if (Math.Abs(num) < 1E-05f)
82 {
83 return null;
84 }
85 float num2 = plane.Normal.X * Position.X + plane.Normal.Y * Position.Y + plane.Normal.Z * Position.Z;
86 float num3 = (0f - plane.D - num2) / num;
87 if (num3 < 0f)
88 {
89 if (num3 < -1E-05f)
90 {
91 return null;
92 }
93 num3 = 0f;
94 }
95 return num3;
96 }
97
98 public void Intersects(ref Plane plane, out float? result)
99 {
100 float num = plane.Normal.X * Direction.X + plane.Normal.Y * Direction.Y + plane.Normal.Z * Direction.Z;
101 if (Math.Abs(num) < 1E-05f)
102 {
103 result = null;
104 return;
105 }
106 float num2 = plane.Normal.X * Position.X + plane.Normal.Y * Position.Y + plane.Normal.Z * Position.Z;
107 float num3 = (0f - plane.D - num2) / num;
108 if (num3 < 0f)
109 {
110 if (num3 < -1E-05f)
111 {
112 result = null;
113 return;
114 }
115 result = 0f;
116 }
117 result = num3;
118 }
119
120 public float? Intersects(BoundingSphere sphere)
121 {
122 float num = sphere.Center.X - Position.X;
123 float num2 = sphere.Center.Y - Position.Y;
124 float num3 = sphere.Center.Z - Position.Z;
125 float num4 = num * num + num2 * num2 + num3 * num3;
126 float num5 = sphere.Radius * sphere.Radius;
127 if (num4 <= num5)
128 {
129 return 0f;
130 }
131 float num6 = num * Direction.X + num2 * Direction.Y + num3 * Direction.Z;
132 if (num6 < 0f)
133 {
134 return null;
135 }
136 float num7 = num4 - num6 * num6;
137 if (num7 > num5)
138 {
139 return null;
140 }
141 float num8 = (float)Math.Sqrt(num5 - num7);
142 return num6 - num8;
143 }
144
145 public void Intersects(ref BoundingSphere sphere, out float? result)
146 {
147 float num = sphere.Center.X - Position.X;
148 float num2 = sphere.Center.Y - Position.Y;
149 float num3 = sphere.Center.Z - Position.Z;
150 float num4 = num * num + num2 * num2 + num3 * num3;
151 float num5 = sphere.Radius * sphere.Radius;
152 if (num4 <= num5)
153 {
154 result = 0f;
155 return;
156 }
157 result = null;
158 float num6 = num * Direction.X + num2 * Direction.Y + num3 * Direction.Z;
159 if (!(num6 < 0f))
160 {
161 float num7 = num4 - num6 * num6;
162 if (!(num7 > num5))
163 {
164 float num8 = (float)Math.Sqrt(num5 - num7);
165 result = num6 - num8;
166 }
167 }
168 }
169
170 public static bool operator ==(Ray a, Ray b)
171 {
172 if (a.Position.X == b.Position.X && a.Position.Y == b.Position.Y && a.Position.Z == b.Position.Z && a.Direction.X == b.Direction.X && a.Direction.Y == b.Direction.Y)
173 {
174 return a.Direction.Z == b.Direction.Z;
175 }
176 return false;
177 }
178
179 public static bool operator !=(Ray a, Ray b)
180 {
181 if (a.Position.X == b.Position.X && a.Position.Y == b.Position.Y && a.Position.Z == b.Position.Z && a.Direction.X == b.Direction.X && a.Direction.Y == b.Direction.Y)
182 {
183 return a.Direction.Z != b.Direction.Z;
184 }
185 return true;
186 }
187}
static CultureInfo CurrentCulture
static double Sqrt(double d)
static double Abs(double value)
bool Intersects(BoundingBox box)
bool Equals(Ray other)
Definition Ray.cs:25
override string ToString()
Definition Ray.cs:49
override bool Equals(object obj)
Definition Ray.cs:34
static bool operator==(Ray a, Ray b)
Definition Ray.cs:170
void Intersects(ref BoundingSphere sphere, out float? result)
Definition Ray.cs:145
void Intersects(ref BoundingBox box, out float? result)
Definition Ray.cs:64
float? Intersects(BoundingFrustum frustum)
Definition Ray.cs:69
static bool operator!=(Ray a, Ray b)
Definition Ray.cs:179
void Intersects(ref Plane plane, out float? result)
Definition Ray.cs:98
override int GetHashCode()
Definition Ray.cs:44
float? Intersects(Plane plane)
Definition Ray.cs:78
float? Intersects(BoundingBox box)
Definition Ray.cs:59
Ray(Vector3 position, Vector3 direction)
Definition Ray.cs:19
float? Intersects(BoundingSphere sphere)
Definition Ray.cs:120
override string ToString()
Definition Vector3.cs:85