- Home /
Normal of the edge between two triangles
I have a ball rolling through the tube, and after ever roll I do a raycast down, and then get the normal of that point so I can get the difference between it and the initial raycast and then rotate the ball can be parallel to the ground. The problem is, the tube is made up of a series of triangles, as most things are. If I got the normal of the present triangle it would point strait up, but because of there the ball is the ray is casting down on an edge between two triangles and the normal is pointing at an odd angle. In the picture the green is the raycast, and the red is the normal. Is there a way that I can tell the difference between triangle proper and an edge between two triangles. I've included my code so you might better understand what I am trying to do.
//Move ball down onto the surface.
Physics.Raycast(down.position, -down.up, hit);
transform.position.y -= (hit.distance - transform.GetComponent(SphereCollider).radius*scale);
//Rotate the ball around a point very close to where the ball is touching the tube, it will roll but will be elevated slightly which is why it has to be brought back down in the above code
Physics.Raycast(child.position, -child.up, hit);
transform.RotateAround(hit.point, child.right, 5);
//rotate the children to realign
child.Rotate(child.right * -5);
down.Rotate(down.right * -5);
Debug.DrawRay(down.position, -down.up, Color.green, 5);
//Rotate children so we can find what the normal of the surface is.
Physics.Raycast(down.position, -down.up, hit);
//if surface is an edge rotate by a little
Debug.DrawRay(hit.point, hit.normal, Color.red, 5);
rot = -Vector3.AngleBetween(down.position, hit.normal);
child.Rotate(child.right * rot);
down.Rotate(down.right * rot);
Ah you would normally be right, but unfortuantly this is a far from normal case, you see the tube(imported from maya) and the ball do not interact properly when left to physics. The ball wont sit on the tube properly, and will tend to freak out after a few seconds. I posed this question to UA all about it a couple of weeks ago, but there has been no answer yet. http://answers.unity3d.com/questions/308191/mesh-collider-maya-import.html
I tried that but heres the think, ins$$anonymous$$d of sitting proper, Y axis pointing strait up, it tilts. I don't get it.
Answer by Eric5h5 · Sep 07, 2012 at 04:21 AM
Maybe you want this: http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-barycentricCoordinate.html
Now did come across this before, but even despite the example given I have no real idea how to use it, any chance you know how, or have any better examples?
I used it once, but it was kind of a long time ago, so I don't remember any specifics.
Your answer
Follow this Question
Related Questions
Automatically rotating objects to fit on to others 0 Answers
How do I obtain the surface normal for a point on a collider (can't use RaycastHit.normal)? 3 Answers
How can i get point reletive to hit.point 0 Answers
Angle between Ray and Normal 1 Answer
Why does the raycast hit only objects/faces of a certain rotation? 1 Answer