- Home /
Question about finding out edges of a box collider
Okay this is really annoying for me since i want to do a pathfinding script designed for my own game and don't want to use somebody else's cuz i want some practice. I have figure out my algorithm and kinda want to put it into action. THe problem now is that i cant find a way to find out the edges of box colliders for the algorithm to work all i need is the world space size of the box. I know you can do it by raycasting your entire map blablabla, but its kinda unnecessary for my algorithm. I just want to know the size of the box in world space or somehow get the coordinates of the 4 vertical edges of a box collider;
I have learned how to find out all the other data already. Or is there another way of doing it? Im using mathmatical approach by looking at boxcollider top down so it becomes a rectangle. turn the 4 sides into 4 function and whenever they intersect they provide me a x,z position of the box collider. They great thing about it is that it tells me which side of the cube did the raycast contact as well. I have examine my formula and algorithm in 3js for another game and it works. Here is some of it.
so data[3] and data[4] were suppose to the world space size of the box collider; abcd are the 4 points and xz in this program is instead xy. poi is point of impact data[1] data[2] is the center of the collider and 5 is the eulerangle in radius
float[] FindPoints( float[] data , Vector3 poi){ Debug.Log (data[3]+" "+data[4]+" "+cs[3]+" "+cs[4]); float[] edges= new float[4]; float x=0; float y=0; float x1,y1=0; x1= poi.x-data[1]; y1= poi.y-data[2]; Debug.Log (y1+" "+x1); // find which side its on if(y1==x1*(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5])){ Debug.Log ("1"); //ax edges[0]=(data[4]/Mathf.Cos(data[5])-data[3]/Mathf.Sin(data[5]))*Mathf.Tan(Mathf.PI/2-data[5]); //dx edges[1]=(-data[4]/Mathf.Cos(data[5])-data[3]/Mathf.Sin(data[5]))*Mathf.Tan(Mathf.PI/2-data[5]); //ay edges[2]=edges[0]/(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5]); //dy edges[3]=edges[1]/(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5]);
}
else if(y1==x1*(Mathf.Tan(Mathf.PI/2-data[5]))-data[3]/Mathf.Sin (data[5])){
Debug.Log ("2");
//ax
edges[0]=(data[4]/Mathf.Cos(data[5])-data[3]/Mathf.Sin(data[5]))/Mathf.Tan(Mathf.PI/2-data[5]);
//bx
edges[1]=(data[4]/Mathf.Cos(data[5]+data[3])/Mathf.Sin(data[5]))/Mathf.Tan(Mathf.PI/2-data[5]);
//ay
edges[2]=edges[0]/(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5]);
//dy
edges[3]=edges[1]/(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5]);
}
else if(y1==x1*(Mathf.Tan(data[5]))+data[3]/Mathf.Cos(data[5])){
Debug.Log ("3");
//bx
edges[0]=(data[4]/Mathf.Cos(data[5]+data[3])/Mathf.Sin(data[5]))/Mathf.Tan(Mathf.PI/2-data[5]);
//cx
edges[1]=(-data[4]/Mathf.Cos(data[5])+data[3]/Mathf.Sin(data[5]))/Mathf.Tan(Mathf.PI/2-data[5]);
//ay
edges[2]=edges[0]/(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5]);
//dy
edges[3]=edges[1]/(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5]);
}
else if(y1==x1*(Mathf.Tan(data[5]))-data[3]/Mathf.Cos(data[5])){
Debug.Log ("4");
//cx
edges[0]=(-data[4]/Mathf.Cos(data[5])+data[3]/Mathf.Sin(data[5]))/Mathf.Tan(Mathf.PI/2-data[5]);
//dx
edges[1]=(-data[4]/Mathf.Cos(data[5])-data[3]/Mathf.Sin(data[5]))/Mathf.Tan(Mathf.PI/2-data[5]);
//ay
edges[2]=edges[0]/(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5]);
//dy
edges[3]=edges[1]/(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5]);
}
Debug.Log (x1*(Mathf.Tan(Mathf.PI/2-data[5]))+data[3]/Mathf.Sin (data[5]));
Debug.Log (x1*(Mathf.Tan(Mathf.PI/2-data[5]))-data[3]/Mathf.Sin (data[5]));
Debug.Log (x1*(Mathf.Tan(data[5]))+data[3]/Mathf.Cos(data[5]));
Debug.Log (x1*(Mathf.Tan(data[5]))-data[3]/Mathf.Cos(data[5]));
edges[0]+=data[1];
edges[1]+=data[1];
edges[2]+=data[2];
edges[3]+=data[2];
return edges;
}
You should be able to read off the size using this
http://docs.unity3d.com/ScriptReference/BoxCollider-size.html
Indeed; for a box, bounds should match the collider itself :) Note this method is not so repeatable for Spheres etc, but still perfectly valid.
the problem is if i rotate the box it will give a different result since it is making a new box trying to contain it? at least thats how it seems to me. I tried to use the box collider but it doesn't allow me since im using raycasthit and there doesnt seems to be a option of using boxcollider