- Home /
Get ClosestPointOnBounds when inside bounds?
Hello, I've been banging my head on this for hours now. I'll explain the context first. I'm making a railroad tycoon prototype. When placing roads, they will follow the mouse using a raycast hits. I placed a hidden quad around a "building" box to detect if I'm close to a building.
I want to snap the road position to the exterior of the collider.bounds. The problem is when using ClosestPointOnBounds, it will return the current position if I'm inside the bounds (and I always will be).
Possible solutions:
1) Add some values to my hit.point so I'm always outside the bounds, but closest to the hit point… What could I use to make this accurate?
2) An unknown function???
3) Create my own ClosestPointOnBounds, which seems really complicated from what I read…
4) Profit?
Some of my code to give a better idea of what I'm doing:
void noClick()
{
if (newRoadStack.Count != 0)
{
RaycastHit hit;
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit);
// GameObject thisRoad = newRoadStack.Peek().GetComponentInChildren<Collider>().gameObject;
if (hit.collider.tag == "Terrain")
{
//hit = newCastIgnoreRoads();
doFollowMouse(hit);
}
else if (hit.transform.gameObject.name == "SnapZone")
{
//doFollowMouse(hit);
doSnapToBuilding(hit);
}
}
}
void doSnapToBuilding(RaycastHit hit)
{
hit.point = hit.collider.ClosestPointOnBounds(hit.point); // <--- PROBLEM IS HERE, returns the original hit point :(
doFollowMouse(hit);
}
void doFollowMouse(RaycastHit hit)
{
newRoadStack.Peek().transform.LookAt(hit.point); // Look at mouse
// Resize road et FUCK C# CEST CAVE
Vector3 stupidCSharp = newRoadStack.Peek().transform.localScale;
float tempDistance = Vector3.Distance(newRoadStack.Peek().transform.position, hit.point);
// If the mouse is too close, clamp the distance to one and dont show cause its ugly
if (tempDistance < invisibleDistance)
{
tempDistance = 1;
newRoadStack.Peek().GetComponentInChildren<Renderer>().enabled = false; // Show object
}
else
newRoadStack.Peek().GetComponentInChildren<Renderer>().enabled = true; // Show object
stupidCSharp.z = tempDistance;
newRoadStack.Peek().transform.localScale = stupidCSharp;
}
Thanks for any help. If you have a better way of going around with this problem, please feel free to explain :)
O$$anonymous$$, as I was writing this, inspiration kinda struck. Since my "building" cube is always inside the SnapZone collider and my SnapZone is it's child, I can use the building GetPointsOnBound. This effectively snaps to the building edges and moves with my mouse, hurray.
Now, all I would need to do is add to my point the distance along the cube normal to the SnapZone bound… Any ideas? Is this clear at all? :)
Answer by Socapex · Dec 07, 2013 at 06:44 PM
Got it! :D
So, after realizing I could get the point on the bounds if my inner cube (the building), the solution was quite simple. Since the closest point to my mouse on the building will always be orthogonal, I can find the direction by substracting the inner point to my mouse hit point. I normalize that to get the direction towards the outer snap bound.
I then find the difference in size of my 2 bounds (exterior - interior) and scale my direction accordingly. I can now add this to the inner point I found originally and TADA! Works :)
Here is the relevant part of the code:
void doSnapToBuilding(RaycastHit hit)
{
Vector3 insidePoint = hit.transform.parent.collider.ClosestPointOnBounds(hit.point);
Vector3 directionToMouse = hit.point - insidePoint;
directionToMouse.Normalize();
Vector3 distanceToSnapBound = hit.collider.bounds.max - hit.transform.parent.collider.bounds.max;
distanceToSnapBound.Scale(directionToMouse);
insidePoint += distanceToSnapBound;
hit.point = insidePoint;
doFollowMouse(hit);
}
Answer by vladibo · Aug 06, 2017 at 08:50 PM
// extension method
internal static Vector3 ClosestToSurfacePoint(this Collider collider, Vector3 point, bool throwIfFallback = true)
{
var sc = collider as SphereCollider;
if(sc != null)
{
var closest = sc.ClosestPoint(point);
if(fun.distanceSquared.Between(ref closest, ref point) < 0.001)
{
float len;
var toPointDir = (point - sc.transform.position).ToUnit(Vector3.forward);
closest = sc.ClosestPoint(sc.transform.position + toPointDir*(sc.radius+0.001f));
}
return closest;
}
var cc = collider as CapsuleCollider;
if(cc != null)
{
var closest = cc.ClosestPoint(point);
if(fun.distanceSquared.Between(ref closest, ref point) < 0.001)
{
var dir = cc.direction == 0 ? new Vector3(1,0,0) : cc.direction == 1 ? new Vector3(0,1,0) : new Vector3(0,0,1);
float len;
var vec = (cc.transform.rotation * dir) * (cc.height/2f - cc.radius);
var c1 = cc.transform.position + vec;
var c2 = cc.transform.position - vec;
Vector3 drop;
fun.point.ClosestOnLineSegment(ref point, ref c1, ref c2, out drop);
var toPointDir = (point - drop).ToUnit(out len);
closest = cc.ClosestPoint(drop + toPointDir*(cc.radius+0.001f));
}
return closest;
}
if(throwIfFallback) throw new ArgumentException("Unsupported collider type "+collider);
return collider.ClosestPoint(point);
}
//fun.distanceSquared.Between
public static float Between(ref Vector3 a, ref Vector3 b)
{
var vectorX = (double)(a.x - b.x);
var vectorY = (double)(a.y - b.y);
var vectorZ = (double)(a.z - b.z);
return (float)(((vectorX * vectorX) + (vectorY * vectorY)) + (vectorZ * vectorZ));
}
public static bool ClosestOnLineSegment(ref Vector3 p, ref Vector3 line1, ref Vector3 line2, out Vector3 closest)
{
point.ProjectOnLine(ref p, ref line1, ref line2, out closest);
if (!IsOnSegment(ref line1, ref closest, ref line2))
{
var d1 = distanceSquared.Between(ref line1, ref closest);
var d2 = distanceSquared.Between(ref line2, ref closest);
closest = d1 < d2 ? line1 : line2;
return false;
}
return true;
}
public static void ProjectOnLine(ref Vector3 point, ref Vector3 line1, ref Vector3 line2, out Vector3 projection)
{
var pointToLine = point - line1;
var lineVector = line2 - line1;
Vector3 onNormal;
vector.ProjectOnNormal(ref pointToLine, ref lineVector, out onNormal);
projection = onNormal + line1;
}
public static bool IsOnSegment(ref Vector3 segStart, ref Vector3 point, ref Vector3 segEnd)
{
if (point.x <= max(segStart.x, segEnd.x)+epsilon &&
point.x >= min(segStart.x, segEnd.x)-epsilon &&
point.y <= max(segStart.y, segEnd.y)+epsilon &&
point.y >= min(segStart.y, segEnd.y)-epsilon &&
point.z <= max(segStart.z, segEnd.z)+epsilon &&
point.z >= min(segStart.z, segEnd.z)-epsilon)
return true;
return false;
}
This code uses methods that don't exist in Unity (toUnit) and needs a lot of rewriting to get close to working (removing variable references to internally referenced methods, like Between and deleting the "distanceSquared" variable)
Your answer
Follow this Question
Related Questions
Vertex Snapping Produces Gaps 0 Answers
Get position of the centre of each grid tile 1 Answer
How can I consistently get the rectangle bounds around a mesh? 2 Answers
Getting Screen Bounds 1 Answer
Get right or left Vector2/3 out of renderer.bounds 0 Answers