- Home /
How do i check if two objects are on the same 2D plane?
I dont know if the title makes sense, but i hope the pictures will.
I already know how to check on 1 axis. Now it triggers when the player is on the fictional dotted line.
Now I need to be able to check if the player is on the same "plane" as the object (the dotted line) even when the object is rotated.
.
I basicly want to check this:
If you draw a line from the red object and its local up (dotted line). Is the player center on that line?
radius doesn't matter and line of sight doesn't matter.
The solution is probably very trivial, but I cant get my head around it atm.
Any help is much appreciated :)
Edit:
I found out that subtracting the two positions is better. Object.position.x - Player.position.x This way I also know which side the player is on.
Is there a radius that you need to check? Do you need to have line of sight?
If you don't need line of sight, a Physics2D.OverlapCircl:
Physics2D.OverlapCircle(player.transform.position, float circleRadius, objectLayerReference)
I know you said you need to tell if it is rotated, maybe you could do something like this
if(player.transform.position.x != object.transform.position.x)
{
objectIsRotated = true;
}
By the way, in that example you show it is the x values that are same there, not the y.
I did not test any of that, I don't have access to Unity or a compiler to make sure I gave you 100% accurate info, so if there isn't something working for you, let me know and I'll see what I can do.
Hey, thanks for the response.
I think you misunderstood my problem (probably because of my bad description)^^.
I've updated the post, so hopefully it makes more sense now. I also fixed the first picture so that the if-statement is correct... ':D
Answer by sacredgeometry · Aug 16, 2019 at 08:55 PM
What you are talking about are coplanar/colinear vectors.
So you need to define a plane/line before you can find out if another vector sits on it.
Then you can calculate the triple product of the vectors. If you get 0, they are coplanar.
https://en.wikipedia.org/wiki/Triple_product
Example
public static class Vector3Extensions
{
public static bool IsOnPlane(this Vector3 c, Vector3 a, Vector3 b)
{
return Vector3.Dot(a, Vector3.Cross(b, c)) == 0.0f;
}
public static bool IsInLine(this Vector3 c, Vector3 a, Vector3 b)
{
return Vector3.Cross((b-c), a-b) == Vector3.zero;
}
}
Your answer
Follow this Question
Related Questions
2D collisions on a Texture2D with transparent areas 2 Answers
Having problems with mouse drag mesh vertex deformation 0 Answers
How to compare several distances? 1 Answer
2D game look at mouse rotate 1 Answer
2 fingers sprite rotation 1 Answer