- Home /
Problem with getting colliding objects
Hello, I have a mine which damages in a radius. When I click a button the mine explodes and I have to check what is colliding with the mine radius.
I tried making a Primitive.Sphere with the radius of the blast. But i can't make the check if that sphere collides with another objects.
I tried looping through the other primitive objects and try sphere_mesh.bounds.Intersects(rectangle_mesh.bounds) and my_sphere.renderer.bounds.Intersects(my_rectangle.renderer.bounds) but these methods don't work very accurately because the bounding box is not rotating (it just expands and mesh.bounds is in local space and doesn't rotate too)
I dont want to use OnCollisionEnter and OnCollisionExit or triggers. I want to make it in utilities function.
I just want to check if there is an object touching the blast radius.
In my opinion there are two options: 1. mathematical way to get the lines/points of the objects and see if they go through the circle 2. make some kind of intersections of the primitive objects
I preffer the mathematical way because i can make the checks even on the server (where i don't have GameObjects only values) and then the server can make the check and return the damaged units. But the problem with the mathematical way is that the rectangles rotate...
The idea I have is to take the sides of the rectangle and rotate them the right way and make intersection between circle and lines (and line with line)
But I am not good at math and I can't do it. And i really think that the mathematical way is the way to go.
Has anyone done such mathematical intersection scripts ?
Any help would be apreciated.
So i think that the question is a two part question
Can someone tell me how to take the corners of a rotated rectangle (i know its center position and the localScale)
Can someone tell me how to make the mathematical intersection of the lines between the points and another line/circle.
Answer by nikroth · Dec 30, 2013 at 05:00 PM
I solved it! So i get the coordinates ot he cornes of the rectangle (as told above) And then i make Rays from one point to the other And the rays have distance equal to the distance between the points
And after that i check if those rays intersect another object.
I will apreciate it a lot if someone gives me a mathematical way of checking intersection between: line and another line and line and circle
in C# preferably :)
Answer by robertbu · Dec 28, 2013 at 04:32 PM
You talk about rectangle and circle, so I cannot tell if this is 3D or 2D. The typical 3D solution is to use Physics.OverlapSphere().
The function will return a list of colliders that are within a specified radius of a specified point.
For 2D, there is Physics.OverlapCircleAll().
I am talking about 3D and OverlapSphere checks the bounds but the bounds are not accurate because they dont rotate.
I know the manual says it only check the bounds, but the manual appears to be mistaken. I just ran a quick test to be sure, it was testing against the mesh collider or box collider, not against the bounds. Do your own test.
Okay it is a solution for circle against rectangle check but there is no OverlapBox method for rectangle agains rectangle.
Is there a way to get the corner points of a rotated rectangle ?
Given the local (i.e. un-rotated) coordinates of the corners, you can use Transform.TransformPoint() to convert them to world coordinates. You can also use Transform.InverseTransformPoint() to convert them back to local coordinates of another game object.
Answer by nikroth · Dec 28, 2013 at 06:54 PM
An idea that i have is: Get the points calculated from the center
poin1 = posision.z + height/2 , position.x - width/2 and so on....
(every Y coordinate is 0 to check only 2D coordinates)
and then rotate the points around the center with the function from this post: The Post
But now comes the question how to check if lines between the points intersect another line or circle