- Home /
Answer by AlwaysSunny · May 16, 2015 at 06:07 PM
If you can, just check whether four points (the four corners of your car) lie within the parking space's bounding volume. This is a much easier approach.
If you need a little more data, check the car's position and orientation against the parking spot's center and orientation. This allows testing a parking job's accuracy with a high degree of fidelity, and is computationally trivial. A distance of one quarter of a car width might be the worst possible "successful" park job, and the Dot Product of the car's forward and the parking spot's forward will give you a scalar value representing alignment. Definitely sounds like the way to go.
I mention the alternative below because it's useful in other situations, and can give you an intersection mesh, accurate overlap percentages, and other relevant data...
If you can simplify a problem to 2D, do so. Definitely looks like you can. This means discarding the Y axis information on your two boundary objects and conducting the overlap check in 2D.
Numerous discussions on this topic exist; it becomes a "simple" math problem. You might be able to find a geometry library that does such calculations for you with relative ease. Otherwise, you'll need to hunt down a helpful paper or article which discusses how to calculate the area of intersection between two rectangles. It should make for a long, boring, educational afternoon. ;)
Thanks for answer, Can you give some codes for simplest solution? I don't get english very well.
This is untested, but it should get you pretty close to a valid "accuracy checking" solution. Code goes on the "parking space" object. Code implies the "Z" axis of the parking space is the "long" axis.
// Adjust these values as needed
float $$anonymous$$Distance = 0.5f;
float $$anonymous$$Dot = 0.85f;
void OnTriggerStay(Collider other){
if (other.tag != "Car") return; // must be a car
float difference = car.position - transform.position;
float distance = difference.magnitude;
if (distance > $$anonymous$$Distance) return; // too far from center
float dot = Vector3.Dot( car.forward, transform.forward );
dot = $$anonymous$$athf.Abs(dot);
if (dot < $$anonymous$$Dot) return; // too extreme angle
float distanceAccuracy = $$anonymous$$athf.InverseLerp( $$anonymous$$Distance, 0, distance );
float angleAccuracy = $$anonymous$$athf.InverseLerp( $$anonymous$$Dot, 1, dot );
// these two variables between 0..1 represent overall parking accuracy
}
That doesn't actually give the area of overlap that he asked for.
No, but his comment asked for the "simplest solution" for measuring the accuracy of a parked car. This solution is valid for many placement-and-orientation checks. Boolean mesh operations are a much more detailed and complex topic than UA welcomes anyhow.
AlwaysSunny, Thanks for code. I think this is enough. Thank you. I will update my answer after testing it.
Your answer
Follow this Question
Related Questions
Variable doesn't change when box hit trigger area 3 Answers
Manipulate gravity in an area? 1 Answer
Overlapping Triggers not working 0 Answers
How to use Physics2D.OverlapArea 2 Answers
Is OnTriggerEnter always fired when an object is already inside? 0 Answers