1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
public struct Segment {
public Vector2 Start;
public Vector2 End;
}
public Vector2? Intersects (Segment AB, Segment CD) {
float deltaACy = AB.Start.y - CD.Start.y;
float deltaDCx = CD.End.x - CD.Start.x;
float deltaACx = AB.Start.x - CD.Start.x;
float deltaDCy = CD.End.y - CD.Start.y;
float deltaBAx = AB.End.x - AB.Start.x;
float deltaBAy = AB.End.y - AB.Start.y;
float denominator = deltaBAx * deltaDCy - deltaBAy * deltaDCx;
float numerator = deltaACy * deltaDCx - deltaACx * deltaDCy;
if (denominator == 0) {
if (numerator == 0) {
// collinear. Potentially infinite intersection points.
// Check and return one of them.
if (AB.Start.x >= CD.Start.x && AB.Start.x <= CD.End.x) {
return AB.Start;
} else if (CD.Start.x >= AB.Start.x && CD.Start.x <= AB.End.x) {
return CD.Start;
} else {
return null;
}
} else { // parallel
return null;
}
}
float r = numerator / denominator;
if (r < 0 || r > 1) {
return null;
}
float s = (deltaACy * deltaBAx - deltaACx * deltaBAy) / denominator;
if (s < 0 || s > 1) {
return null;
}
return new Vector2 ((float) (AB.Start.x + r * deltaBAx), (float) (AB.Start.y + r * deltaBAy));
}
void FixedUpdate () {
//Camera.main.transform.rotation = Quaternion.identity;
Proxies = GameObject.FindObjectsOfType(typeof(HeroProxy)) as HeroProxy[];
Planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
foreach (HeroProxy p in Proxies)
{
if (p == null)
continue;
if(!GeometryUtility.TestPlanesAABB(Planes, p.collider.bounds))
{
Vector3 otherpos = p.transform.position;
otherpos.y = 0;
Vector3 dir = (p.transform.position - this.transform.position).normalized;
dir.y = dir.z;
dir.z = 0;
Vector3 ooo = Camera.main.WorldToScreenPoint(otherpos); //Camera.main.WorldToScreenPoint(p.transform.position);
if (ooo.z < 0)
ooo = Quaternion.Euler(0, 0, 180) * ooo;
float rrx = 0;
float rry = 0;
float lim = 100000;
Segment ss = new Segment() { Start = new Vector2(0, 0), End = new Vector2(0, 720) };
Segment ss2 = new Segment() { Start = new Vector2(360, 640), End = new Vector2(ooo.x, ooo.y) };
Vector2? interleft = Intersects(ss, ss2);
ss = new Segment() { Start = new Vector2(0, 720), End = new Vector2(1280, 720) };
ss2 = new Segment() { Start = new Vector2(360, 640), End = new Vector2(ooo.x, ooo.y) };
Vector2? inteup = Intersects(ss, ss2);
ss = new Segment() { Start = new Vector2(1280, 720), End = new Vector2(1280, 0) };
ss2 = new Segment() { Start = new Vector2(360, 640), End = new Vector2(ooo.x, ooo.y) };
Vector2? intepright = Intersects(ss, ss2);
ss = new Segment() { Start = new Vector2(1280, 0), End = new Vector2(0, 0) };
ss2 = new Segment() { Start = new Vector2(360, 640), End = new Vector2(ooo.x, ooo.y) };
Vector2? intepdown = Intersects(ss, ss2);
float manl = 0;
float manr = 0;
float manu = 0;
float mand = 0;
if (interleft.HasValue)
{
rrx = interleft.Value.x;
rry = interleft.Value.y;
manl = (interleft.Value - new Vector2(ooo.x, ooo.y)).magnitude;
}
else if (inteup.HasValue)
{
rrx = inteup.Value.x;
rry = inteup.Value.y;
manu = (inteup.Value - new Vector2(ooo.x, ooo.y)).magnitude;
}
else if (intepright.HasValue)
{
rrx = intepright.Value.x;
rry = intepright.Value.y;
manr = (intepright.Value - new Vector2(ooo.x, ooo.y)).magnitude;
}
else if (intepdown.HasValue)
{
rrx = intepdown.Value.x;
rry = intepdown.Value.y;
mand = (intepdown.Value - new Vector2(ooo.x, ooo.y)).magnitude;
}
rry = 720 - rry;
rrx = Mathf.Clamp(rrx, 0, 1280f - 32f);
rry = Mathf.Clamp(rry, 0, 720f - 32f);
}
}
} |
Partager