Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 31f9a3f

Browse filesBrowse files
committed
implement intersect function between a line and a plane
1 parent 98145d0 commit 31f9a3f
Copy full SHA for 31f9a3f

File tree

Expand file treeCollapse file tree

5 files changed

+75
-8
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+75
-8
lines changed
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class CreatePlaneHit : MonoBehaviour
6+
{
7+
public Transform A;
8+
public Transform B;
9+
public Transform C;
10+
public Transform D;
11+
public Transform E;
12+
13+
Plane plane;
14+
Line L1;
15+
16+
void Start()
17+
{
18+
plane = new Plane(new Coords(A.position), new Coords(B.position), new Coords(C.position));
19+
20+
L1 = new Line(new Coords(D.position), new Coords(E.position), Line.LINETYPE.RAY);
21+
L1.Draw(1, Color.green);
22+
23+
for (float s = 0; s <= 1; s += 0.1f)
24+
{
25+
for (float t = 0; t <= 1; t += 0.1f)
26+
{
27+
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
28+
sphere.transform.position = plane.Lerp(s, t).ToVector();
29+
}
30+
}
31+
32+
float z = L1.IntersectsAt(plane);
33+
Debug.Log(z);
34+
35+
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
36+
cube.transform.position = L1.Lerp(z).ToVector();
37+
}
38+
39+
void Update()
40+
{
41+
42+
}
43+
}

‎Assets/GlobalScripts/CreatePlaneHit.cs.meta

Copy file name to clipboardExpand all lines: Assets/GlobalScripts/CreatePlaneHit.cs.meta
+11Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/GlobalScripts/HolisticMath.cs

Copy file name to clipboardExpand all lines: Assets/GlobalScripts/HolisticMath.cs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ static public float Angle(Coords vector1, Coords vector2)
5353
{
5454
float dotDivide = Dot(vector1, vector2) / (Distance(new Coords(0, 0, 0), vector1) * Distance(new Coords(0, 0, 0), vector2));
5555
return Mathf.Acos(dotDivide); //radians. For degrees * 180/Mathf.PI;
56-
}
5756

57+
}
5858
static public Coords Cross(Coords vector1, Coords vector2)
5959
{
6060
float iMult = vector1.y * vector2.z - vector1.z * vector2.y;

‎Assets/GlobalScripts/Line.cs

Copy file name to clipboardExpand all lines: Assets/GlobalScripts/Line.cs
+15-2Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System.Text;
34
using UnityEngine;
45

56
public class Line
@@ -31,18 +32,30 @@ public Line(Coords _A, Coords _V)
3132
type = LINETYPE.SEGMENT;
3233
}
3334

35+
public float IntersectsAt(Plane p)
36+
{
37+
Coords n = HolisticMath.Cross(p.v, p.u);
38+
if(HolisticMath.Dot(n, v) == 0)
39+
{
40+
return float.NaN;
41+
}
42+
43+
float t = HolisticMath.Dot(new Coords(-n.x, -n.y, -n.z), A - p.A) / HolisticMath.Dot(n, B - A);
44+
45+
return t;
46+
}
3447

3548
public float IntersectsAt(Line l)
3649
{
37-
if(HolisticMath.Dot(Coords.Perp(l.v), v) == 0)
50+
if (HolisticMath.Dot(Coords.Perp(l.v), v) == 0)
3851
{
3952
return float.NaN;
4053
}
4154

4255
Coords c = l.A - this.A;
4356
float t = HolisticMath.Dot(Coords.Perp(l.v), c) / HolisticMath.Dot(Coords.Perp(l.v), v);
4457

45-
if(t < 0 || t > 1 && type == LINETYPE.SEGMENT)
58+
if (t < 0 || t > 1 && type == LINETYPE.SEGMENT)
4659
{
4760
return float.NaN;
4861
}

‎Assets/GlobalScripts/Plane.cs

Copy file name to clipboardExpand all lines: Assets/GlobalScripts/Plane.cs
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
public class Plane
66
{
7-
Coords A;
8-
Coords B;
9-
Coords C;
10-
Coords v;
11-
Coords u;
7+
public Coords A;
8+
public Coords B;
9+
public Coords C;
10+
public Coords v;
11+
public Coords u;
1212

1313
public Plane(Coords _A, Coords _B, Coords _C)
1414
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.