- Home /
Arbitrary mesh traversal
I am trying to create a vehicle that can move of the surface of an arbitrary mesh.
The mesh is for example spherical, and I want movement on both the inner and outer surfaces.
This script does not produce good results, as extreme jerkiness occurs. What is the best approach?
function Start()
{
var thisTransform = GetComponent( Transform );
var surfaceCollider = surface.GetComponent( Collider );
}
function Update()
{
var hit : RaycastHit;
var closest = surfaceCollider.ClosestPointOnBounds(thisTransform.position);
if (surfaceCollider.Raycast(new Ray(thisTransform.position, closest - thisTransform.position), hit, 10)) {
//Setting new position and rotation.
thisTransform.position = hit.point + hit.normal * height;
thisTransform.LookAt(thisTransform.position + thisTransform.forward, hit.normal);
}
//Rigidbody is moved.
}
Answer by Fattie · Oct 14, 2012 at 05:59 AM
It is actually incredibly difficult, mathematically, to "move something on a truly arbitrary mesh"
you must tell us more clearly what the situation is. what sort of mesh, generally, is it ?
is it roughly flat (like a field). does it have concave "dips", is it closed (like a lumpy ball in space). or what?
I'm pretty sure ClosestPointOnBounds is not relevant to what you are doing.
Basically in any racing game you've ever played:
you move the car a little
you use a raycast to check your current exact height above the "ground"
set your vehicle height correctly
this is an absolute basic of making vid games. I urge you to perfect that, you will have to get that working first in any rate.
your next problem will relate to knowing the tangent of the surface.
But it is impossible to give the "usual approach" unless we understand exactly what you are trying to do.
In response to your comments:
spherical ?
if it is a sphere,
simply calculate the exact distance from the center, and the tangent, with one line of arithmetic
(Or you can just use, I guess, a raycast to "find" the surface. if you are INSIDE the sphere, obviously raycast from the outside in. Are you aware colliders have one surface only?)
can you more clearly explain - in great detail - perhaps with a picture - EXACTLY what sort of object you mean?
if it is spherical, you would just get the answer using math.
regarding ClosestPointOnBounds.
I have told you exactly how you keep an object on a surface. see points one, two, three above for full explanation. I am so old I have worked on vast numbers of keep-ground-on-surface rigs on many generations of Stuff, and that's conceptually the only way you can do it.
I urge ou to canvas the opinion of more experienced developers on this issue
I think you may have the wrong idea about ClosestPointOnBounds.
you are familiar with the "bounding box" idea, it is aligned in global coords. it is almost useless for precision work.
the typical use of and classic example of ClosestPointOnBounds is when there is (say) an explosion and you basically need to know WHICH SIDE of your spaceship, the explosion happened on. it can roughly tell you the side where the action is happening.
So I encourage you to more clearly describeb (diagram) what sort of surface you mean.
If it's spherical, you have the exact answer here.
Even more ...
The answer you point to is wrong.
I wrote a correct there.
Regarding how ClosestPointOnBounds works.
I can only retype what I typed above. Are you familiar with AABB type bounding boxes in the 3D universe?
If you are not, you must familiarize yourself with them.
once again I explain, they are oriented in global space, so ONLY VERY ROUGHLY match the shape of the object.
If you don't believe me, or think I am just typing for the enjoyment of finger exercise, I urge you to ask others about this, or, read about it online. :)
I will copy and paste what I wrote above. I will CAPS and BOLD the important parts.
the typical use of and classic example of ClosestPointOnBounds is when there is (say) an explosion and you basically need to know WHICH SIDE of your spaceship, the explosion happened on. it can ROUGHLY tell you the side where the action is happening.
regarding "simply" using raycast,
knowing the direction to cast -- if this solution is relevant -- is entirely dependent on the exact situation involved. often you cast towards some minimal center point, or gravitationally downwards, or some other solution I could tell you, if you get a chance to explain the shape of the surface you are dealing with.
you have said it is spherical. that has a specific meaning, if it is spherical I have given the solution.
if it is not spherical (maybe "bumpy ball" ? orwatever - I don't know and can't guess) I am very very very very happy to give you the typical solution used in games development, for your situation, if I have already worked on that sort of rig.
At the moment I can't help any more as we need to know what the surface is like.
(the shape of the VEHICLE can also make a huge difference. if THE VEHICLE is literally spherical (the vehicle) that is staggeringly different from a car, aircraft, humanoid, etc. abd would use massively different sytems.
My guess - you believe there is some "universal" solution that will work for all surfaces and all vehicles...
This is actually wrong because heuristically often it is a purely aesthetic decision what is meant by down, on some strange surfaces. But putting aside that issue we could easily tell you the purely mathematical way to absolutely find the set of closest verts on the closest surface segment acing the vehicle, and figuring a tangent surface from there ... but that would be of zero help to you in building a game.
1 - again if the surface is in fact "spherical" you have the answer (use arithmetic).
2 - the question and answer you point to here http://answers.unity3d.com/questions/325099/point-mesh-shortest-vector.html is simply wrong, as I have explained at great length a number of times. I love repeating myself so thanks for the opportunity! :)
3 - I urge you to read up on "bounding boxes" as it may be causing considerable confusion.
4 - I am more than happy to provide, if I know of one, a typical game engineering solution to the (extremely common, central) problem you describe .. but it is impossible unless the nature of the surface (and indeed the vehicle) is known. we don't even know if the surface is Closed yet
5 - you seem to seek a "universal" solution. the "universal" mathematical solution would be irrelevant here.
The mesh is for example spherical, and I want movement on both the inner and outer surfaces. Is it not the case that the two main methods for finding the next position on a mesh are using ClosestPointOnBounds (which is not accurate for large velocities), and solving the mesh's equations.
The vehicle has any shape, for example a sphere. Based on http://answers.unity3d.com/questions/325099/point-mesh-shortest-vector.html I thought that ClosestPointOnBounds returns the point on a mesh that has the shortest distance to some other point, in this case the vehicle. You indeed gave the answer for a spherical surface, but if the surface is for example a 3D sinusoidal, I want that point because it is probably closest to the real solution that can be found without solving equations. Simply using RayCast is not possible because the direction of the cast is not known.
Just a thought - does the vehicle not have a logical down? You could ray cast from above the vehicle's centre using that logical down after a move, then adjust the angle of the vehicle to sit on the surface of the triangle that is hit by that cast. Basically setting the transform.up of the vehicle to the normal of the hit collider or the triangle in the mesh. Obviously this rotates the down for the next move.
Ugh, well your comment on 099 is right, oh hell :S I'm thinking!!!
Your answer
Follow this Question
Related Questions
Manipulating arrays of Vector3s with pointers 1 Answer
Obtaining a perpendicular point? 1 Answer
Point mesh shortest vector 1 Answer
Defining the forward for an arbitrary mesh 2 Answers
Sample the height of the mesh? 1 Answer