Trying to color squares of mesh as ball rolls over it

This is Unity3D for iPhone: I have a meshcollider (with a texture) and a rigidbody sphere collider and I can detect collisions fine in OnCollisionStay. My goal is to eventually be able to change the color of a square/rectangle as the ball rolls over it on the mesh. The ball rolls fine, all the physics works great, but how can I get the triangle vertices of the mesh based on where the collision takes place? I know how to get ALL the vertices, but does anyone know of any posts that show how to get the closest vertices (on the mesh) to the collision? I know the mesh is really triangles and not quads or rectangles, so I can always color or texture two of them to color in a rectangle/square, but how can I go from contact info on the mesh to getting the vertices of the triangle(s) as the ball rolls over them? Also, are the vertices of the collision global, or local to the mesh? (assuming there is such a thing) (I’m a bit of a noob at all this 3D stuff so forgive my noobosity.)

this would be good to know if its possible.
bump :wink:

You could take the contact points from the Collision and then do a Physics.RayCast() to find out which triangle(s) is hit (it’s in the RaycastHit structure).

However, it might be faster to make each of your squares a separate object. Then you can just change the material on the ones you’ve hit.

Your OnCollisionStay event gives you a ‘Collision’ object. One of its properties is ‘contacts’, which is an array of contact points. This is usually just one point, but can be more than one.

Each item in the contacts list is a ‘ContactPoint’ object, and if you look those up in the scripting reference, you’ll find they have a ‘point’ (being a world-space position), and ‘normal’. In an ideal world, it would also have the face number in here, but it doesn’t.

However, with the point normal values, you now have enough info to cast a ray against the object’s collider, at the exact spot of the collision point.

(See Collider.Raycast).

Simply create a ray that starts from: (collisionPoint + normal), and casts along the direction: -normal

This means the ray starts a little distance away from the face (in the direction of the face’s normal), and casts in the opposite direction (towards the face).

The raycast result (in the RaycastHit object) contains your desired ‘triangleIndex’ value! (and additionally contains the exact UV coords hit on the objects texture too).

hope this helps,

  • Ben

Thanks a lot duck for the detailed explanation. Someone mentioned making all my squares of the mesh separate objects, however I tried this and it slows the whole thing down to the point where it’s unusable, so I will either use the raycast method or not do it at all. It’s nice to know how to do it though. Even using the raycast method, I still don’t think that would allow me to paint just part of the mesh. I could create a new object, but eventually that will once again slow it down too much. What I’m doing right now is I have several targets that I instantiate one at a time that have to be hit, and they disappear once hit, so the number of active objects is kept low.

It will, actually. Probably simplest to use vertex colors (see this topic), though you can use other methods too. If this mesh has regular squares (like a grid of graph paper), then I wouldn’t bother with raycasting, since you will be able to derive what square the ball is over using some simple math.

–Eric

Thanks very much for the link. As far as my mesh goes, it’s definitely not flat (more like a vortex shape). But maybe I can use the coloring technique you mentioned. Thanks.