[SOLVED]MeshCollider in Unity?

Chipmunk2D Bindings for the Unity3D engine
compass3
Posts: 37
Joined: Fri Aug 30, 2013 4:27 pm
Contact:

[SOLVED]MeshCollider in Unity?

Post by compass3 »

Hi,

I just bought the Chipmunk2D for Unity yesterday. The Chipmunk2D is very good, I find it works better than PhysX.

However I can not find a lot functions in Chipmunk2D manual. For example, the cpPolyValidate function. I was trying to use the function to make Chipmunk2D work well with concave polygon. But since I can not find the function, I probably will need to implement the calculation by myself.

Is the function exist in the library(May be I just did not find it)? Or is there any functions that provide similar usages?
Will the Chipmunk2D team made it available in future for Unity?

Thanks! :D
Last edited by compass3 on Wed Sep 04, 2013 3:16 pm, edited 1 time in total.
compass3
Posts: 37
Joined: Fri Aug 30, 2013 4:27 pm
Contact:

Re: cpPolyValidate in Unity?

Post by compass3 »

I think I can just compare the Length of vert and hull, if different, the original polygon is concave. Am I right?
:lol:
compass3
Posts: 37
Joined: Fri Aug 30, 2013 4:27 pm
Contact:

Re: cpPolyValidate in Unity?

Post by compass3 »

unfortunately, the method above is not correct. Even I compared all the vertices in vert and hull I still get incorrect result.
compass3
Posts: 37
Joined: Fri Aug 30, 2013 4:27 pm
Contact:

Re: cpPolyValidate in Unity?

Post by compass3 »

sorry, may be I made a mistake. Check every vertices should work.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpPolyValidate in Unity?

Post by slembcke »

Hey compass3.

So I didn't expose the cpPolyValidate() function to Unity because I didn't think people would actually want to use it. Chipmunk automatically creates a convex hull out of your input points like Unity does when using convex mesh colliders.

So there are two things that cpPolyValidate() does. It checks that the points are convex, that the points have a clockwise winding. When creating a polygon shape from a bunch of points, the hull property will contain be exactly that, the set of convex points with a clockwise winding. The point it picks for the first vertex is the hull is the leftmost one, ties are broken by picking the bottommost.

If you just want to check for concavity, then comparing the arrays' sizes is sufficient. Comparing the winding is harder since you need to figure out which is the first vertex in the hull array. Alternatively, if the array sizes are the same, you can just compare the first three vertexes in the array to check the winding. Check if the 2D cross product analog is positive or negative. (Do a 3D cross product and only look at the z-value) I forget which direction is which off the top of my head though.

All said and done... it might just be easiest to port cpPolyValidate() to Unity. It's a pretty short function, so it would only take a minute or two. https://github.com/slembcke/Chipmunk2D/ ... ape.c#L150
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
compass3
Posts: 37
Joined: Fri Aug 30, 2013 4:27 pm
Contact:

Re: cpPolyValidate in Unity?

Post by compass3 »

slembcke wrote:Hey compass3.

So I didn't expose the cpPolyValidate() function to Unity because I didn't think people would actually want to use it. Chipmunk automatically creates a convex hull out of your input points like Unity does when using convex mesh colliders.

So there are two things that cpPolyValidate() does. It checks that the points are convex, that the points have a clockwise winding. When creating a polygon shape from a bunch of points, the hull property will contain be exactly that, the set of convex points with a clockwise winding. The point it picks for the first vertex is the hull is the leftmost one, ties are broken by picking the bottommost.

If you just want to check for concavity, then comparing the arrays' sizes is sufficient. Comparing the winding is harder since you need to figure out which is the first vertex in the hull array. Alternatively, if the array sizes are the same, you can just compare the first three vertexes in the array to check the winding. Check if the 2D cross product analog is positive or negative. (Do a 3D cross product and only look at the z-value) I forget which direction is which off the top of my head though.

All said and done... it might just be easiest to port cpPolyValidate() to Unity. It's a pretty short function, so it would only take a minute or two. https://github.com/slembcke/Chipmunk2D/ ... ape.c#L150
:D Thank you very much, Slembcke! The reason I want to do this is because there are some static scene in our game. And these sence are not simple geometry. So I try to find out a way to use Chipmunk to do mesh collider. What I had done is use each triangle as a polygon, and all the triangle of the object as a same group of many polygon shapes. I tried this, the multiple polygon shapes can simulate as a mesh collider, but the FPS is very slow. I think it is because Chipmunk regard every polygon shape as separated.

Did I do the right way? Or is there any way to solve the problem. There is some static object with not only concave shape but also complex shapes. Again, thanks! :D
compass3
Posts: 37
Joined: Fri Aug 30, 2013 4:27 pm
Contact:

Re: cpPolyValidate in Unity?

Post by compass3 »

By the way, I detect if the polygon is convex by compare array length and each element of _vert[] and _hull. I think it is just the way similar as you mentioned. It works well. Thanks. :D
compass3
Posts: 37
Joined: Fri Aug 30, 2013 4:27 pm
Contact:

Re: cpPolyValidate in Unity?

Post by compass3 »

It is also possible because of each polyshape is a monobehaviour. And I attached too much monobehaviour as component to a game object, which slow down the game. Thanks.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpPolyValidate in Unity?

Post by slembcke »

So creating a lot of poly colliders from triangles isn't very efficient. Each individual shape has a pretty high cost, and polys are the most expensive shape type to use. A better solution for that is to outline your mesh using segment colliders. You need fewer of them and they are cheaper computationally. In either case, adding a radius to the polys or segments will increase the performance (10% or so is common) and smoothness of the collisions at corners and cracks.

The downside is that it does make your shapes hollow and it is possible to get stuff inside of them if they are moving fast enough or small enough to pass through in a single timestep.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
compass3
Posts: 37
Joined: Fri Aug 30, 2013 4:27 pm
Contact:

Re: cpPolyValidate in Unity?

Post by compass3 »

slembcke wrote:So creating a lot of poly colliders from triangles isn't very efficient. Each individual shape has a pretty high cost, and polys are the most expensive shape type to use. A better solution for that is to outline your mesh using segment colliders. You need fewer of them and they are cheaper computationally. In either case, adding a radius to the polys or segments will increase the performance (10% or so is common) and smoothness of the collisions at corners and cracks.

The downside is that it does make your shapes hollow and it is possible to get stuff inside of them if they are moving fast enough or small enough to pass through in a single timestep.

Thank you slembcke! I have changed the polygon shapes to segment shapes now. And the chipmunk works very well. It solves the problem physX did not solve.

What I am trying to do now is to turn off the PhysX, because running two physics engine at the same time slows the game I think. Do you know any way to do this? Or is it advisable to do this?
Thanks! :D
Last edited by compass3 on Wed Sep 04, 2013 2:55 pm, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests