- Home /
Modify this script for PolygonCollider2D
Hi guys, I am implementing a Tile Map, from Tiled Map generator using XuniTMX2D from here https://bitbucket.org/Chaoseiro/x-unitmx/overview, i managed to modify the scripts to implement boxCollider2D instead of boxCollider but i have tried to change the method of generating Polygon collider mesh into PolygonCollider2D and It has not work, this is the original code, could someone help me transforming it into PolygonCollider2D? thanks in advance.
/// <summary>
/// Generate a Polygon collider mesh
/// </summary>
/// <param name="obj">Object which properties will be used to generate this collider.</param>
/// <param name="zDepth">Z Depth of the collider.</param>
/// <param name="colliderWidth">Width of the collider.</param>
/// <param name="innerCollision">If true, calculate normals facing the center of the collider (inside collisions), else, outside collisions.</param>
/// <returns>Generated Game Object containing the Collider.</returns>
public GameObject GeneratePolygonCollider(MapObject obj, float zDepth = 0, float colliderWidth = 1.0f, bool innerCollision = false)
{
GameObject polygonCollider = new GameObject(obj.Name);
polygonCollider.transform.parent = this.Parent.transform;
Mesh colliderMesh = new Mesh();
colliderMesh.name = "Collider_" + obj.Name;
MeshCollider mc = polygonCollider.AddComponent<MeshCollider>();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
Vector3 firstPoint = (Vector3)obj.Points[0];
Vector3 secondPoint, firstFront, firstBack, secondFront, secondBack;
for (int i = 1; i < obj.Points.Count; i++)
{
secondPoint = (Vector3)obj.Points[i];
firstFront = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth - colliderWidth);
firstBack = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth + colliderWidth);
secondFront = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth - colliderWidth);
secondBack = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth + colliderWidth);
if (innerCollision)
{
vertices.Add(firstBack); // 3
vertices.Add(firstFront); // 2
vertices.Add(secondBack); // 1
vertices.Add(secondFront); // 0
}
else
{
vertices.Add(firstFront); // 3
vertices.Add(firstBack); // 2
vertices.Add(secondFront); // 1
vertices.Add(secondBack); // 2
}
triangles.Add((i - 1) * 4 + 3);
triangles.Add((i - 1) * 4 + 2);
triangles.Add((i - 1) * 4 + 0);
triangles.Add((i - 1) * 4 + 0);
triangles.Add((i - 1) * 4 + 1);
triangles.Add((i - 1) * 4 + 3);
firstPoint = secondPoint;
}
// Connect last point with first point
secondPoint = (Vector3)obj.Points[0];
firstFront = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth - colliderWidth);
firstBack = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth + colliderWidth);
secondFront = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth - colliderWidth);
secondBack = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth + colliderWidth);
if (innerCollision)
{
vertices.Add(firstBack); // 3
vertices.Add(firstFront); // 2
vertices.Add(secondBack); // 1
vertices.Add(secondFront); // 0
}
else
{
vertices.Add(firstFront); // 3
vertices.Add(firstBack); // 2
vertices.Add(secondFront); // 1
vertices.Add(secondBack); // 2
}
triangles.Add((obj.Points.Count - 1) * 4 + 3);
triangles.Add((obj.Points.Count - 1) * 4 + 2);
triangles.Add((obj.Points.Count - 1) * 4 + 0);
triangles.Add((obj.Points.Count - 1) * 4 + 0);
triangles.Add((obj.Points.Count - 1) * 4 + 1);
triangles.Add((obj.Points.Count - 1) * 4 + 3);
colliderMesh.vertices = vertices.ToArray();
colliderMesh.triangles = triangles.ToArray();
colliderMesh.RecalculateNormals();
mc.sharedMesh = colliderMesh;
polygonCollider.isStatic = true;
return polygonCollider;
}
No one here is going to do your work for you, we are simply here to help point you in the right direction.
Depends @Sildaekar , people do get bored or feel like helping a bit more sometimes.
He's just asking for help, not to have it done completely for him.
Last change but still not working, updated vectors to 2D, removed triangles and added vertices to Polygon path with index 0.
public GameObject GeneratePolygonCollider ($$anonymous$$apObject obj, float zDepth = 0, float colliderWidth = 1.0f, bool innerCollision = false)
{
GameObject polygonCollider = new GameObject (obj.Name);
polygonCollider.transform.parent = this.Parent.transform;
PolygonCollider2D pC = polygonCollider.AddComponent<PolygonCollider2D> ();
List<Vector2> vertices = new List<Vector3> ();
List<int> triangles = new List<int> ();
Vector2 firstPoint = (Vector2)obj.Points [0];
Vector2 secondPoint, firstFront, firstBack, secondFront, secondBack;
for (int i = 1; i < obj.Points.Count; i++) {
secondPoint = (Vector2)obj.Points [i];
firstFront = new Vector2 (obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y);
firstBack = new Vector2 (obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y);
secondFront = new Vector2 (obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y);
secondBack = new Vector2 (obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y);
if (innerCollision) {
vertices.Add (firstBack); // 3
vertices.Add (firstFront); // 2
vertices.Add (secondBack); // 1
vertices.Add (secondFront); // 0
} else {
vertices.Add (firstFront); // 3
vertices.Add (firstBack); // 2
vertices.Add (secondFront); // 1
vertices.Add (secondBack); // 2
}
firstPoint = secondPoint;
}
// Connect last point with first point
secondPoint = (Vector2)obj.Points [0];
firstFront = new Vector2 (obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y);
firstBack = new Vector2 (obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y);
secondFront = new Vector2 (obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y);
secondBack = new Vector2 (obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y);
if (innerCollision) {
vertices.Add (firstBack); // 3
vertices.Add (firstFront); // 2
vertices.Add (secondBack); // 1
vertices.Add (secondFront); // 0
} else {
vertices.Add (firstFront); // 3
vertices.Add (firstBack); // 2
vertices.Add (secondFront); // 1
vertices.Add (secondBack); // 2
}
pC.setPath (0, vertices.ToArray ());
polygonCollider.isStatic = true;
return polygonCollider;
}
I don't want you to do it for me, I just need a bit of help, thanks in advance.
Answer by Fornoreason1000 · Mar 26, 2014 at 09:56 AM
the best way to do these things is first understand the operation of the script and the goal of the script.
in this case your going from 3d collisions to 2d collisions. which means what? No Z axis, so you would rewrite the code so that it no longer takes the z axis into account. but meshes are always in 3d so be wary.
try looking at the differences of what the script is currently doing and what you want it to be doing. for example character turns blue, i want it red, so i find whatever is making him blue and change it so it makes him red.
some key differcnes you should make note of
3d physics uses mesh collides, 2d uses Polygon
polygon collision do not accept meshes, they accept an array of verticies
2d vertices shouldn't really have a proper Z axis value
http://docs.unity3d.com/Documentation/Components/class-PolygonCollider2D.html
this looks useful too http://docs.unity3d.com/Documentation/ScriptReference/PolygonCollider2D.SetPath.html
Hope it helps and good luck let me know if you need anymore help
Hi thanks a lot for your help, I changed all the Vector3 to Vector2, I also removed Z axis from all the vertices, and finally I added all the vertices to the path of the polygon like this: pC.setPath (0, vertices.ToArray ());
But I haven't used the triangles, I think that I don't need them but I am not really sure about it.
thanks a lot for your time and help!
You can see the new updated solution above, I edited it with the new code, so its easier for you to help me :)
triangles are specific to meshes, you don't need them. also at line 18, you assign a List to a List , that will cause us problems.
one way you could do this is just set vertices on your object to that collider. since its 2d and has no "pits" and weird shapes.
Thanks a lot, I just changed it but it doesn't change much as I was not using the triangles, I am still missing something, It must be something related with vertices index when setting path...
What happening? do you get errors or the collide isnt there or is really messed up?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Combine common surfaces 1 Answer
Box Collider Vs Simple Plane Mesh Collider 1 Answer
Change polygon collider depending on the animation 0 Answers