Procedural mesh generation and plotting trajectories

Part 1:

What I would like to do is plot a trajectory between two points in 3D space...the origin point would be an empty GO and the endpoint would be the vector returned from a raycast hit. Is this possible?

Part 2:

I would like to use the trajectory obtained in part 1 as a path to extrude a tri along to generate a 3D mesh (in this case, a laser trail). To keep the polycount low, I would need to be able to adjust the segmentation of the trajectory (so, as opposed to a perfectly smooth curve, I could have a curve with n segments). Is this possible?

Part 3:

Am I going about this the wrong way? Is there an easier or less resource intensive way to do this?

Thanks.

[EDIT]

The vector for the trajectory will be calculated based on a starting GO (called FirePoint). I plan to use Raycast.Hit to obtain the ending vector (so when you say press a button, a ray will be cast from Firepoint, and when it intersects with an enemy, that will give the endpoint). I am not sure how to calculate the trajectory or curve between these two points, this is one of the things I need help with. Ideally, I would be able to set the resolution of the curve in such a way as to be able to define how many segments the curve has (so, I could say calculate the trajectory, and have it draw a curve with n segments that would approximate a perfectly smooth trajectory, see image).

As for generating the mesh, once I had this trajectory, I would like to extrude a tri along the curve approximation to form a 3D mesh. This mesh would only need to be generated once, it's position would not update every frame. I do not know how to do this either.

I hope this helps to further explain exactly what I am trying to do.

trajectory image

[EDIT 2] To be clear, I really just need an arc between the two points, it does not have to be an accurate trajectory that is physics based.

If your goal is to draw something like a laser trail or tracer bullets, that is, a visualization of the path taken by an object, you might want to look into Trail Renderers, an easy and cheap way to do things like this. All you would need to do is move the game object (that the trail renderer is attached to) along the path in question. If the physics engine is moving the object for you, then that's even easier.

Note that the game object doesn't itself have to be visible to leave whatever trail you wish.

Yes, With scripting you can make a mesh that is a tri extruded along some line or curve. You can get the endpoints from wherever you like.

Yes, you can write the script such that you can control the segmentation.

It seems you have two different problems to solve:

  1. You need to calculate a trajectory and then sample a number of points along that trajectory. (Output is an array of points.)

  2. You need to visualize a path (extrude a tri, or similar) defined by a number of points. (Input is an array of points.)

I would advise solving the problems one at a time, since they're quite separate. You can start coding the visualization/extrusion and test it on just some hardcoded points, or you can begin coding the path calculation and just visualize it with a loop calling Debug.DrawLine() for each line segment.

For calculating the trajectory, you need to decide what kind of constraints you want. You could have the trajectory follow gravity, but even then there's infinitely many solution for getting from A to B. Say you have an angle and a force (speed when throwing). You can have a fixed angle and calculate the needed force, or you can have a fixed force and calculate the needed angle. In both cases, there is not always a solution - the angle can be too low, or the force too small. In any case, you should end up with a 2nd order polynomial. You can then sample that polynomial with fixed increments of X to get your array of points.

For getting started with generating meshes procedurally, you can see the procedural example project and go from there.

Do you want to generate a "realistic" trajectory, i.e one that could be replicated with the physics engine, or do you just want to create an arch?

In the procedural example project there is a script which can handle mesh extrusion maybe you would want to take a look at that :)

http://unity3d.com/support/resources/example-projects/procedural-examples

Working in terms of angle and force to create the proper curve is one way. Another way is to implement a Bezier curve algorithm to construct the curve you want. It only needs at least two endpoints (which you already have: the FirePoint GameObject and the Raycast.Hit) and one other point in the middle to control the steepness of the curve.

anomalous_underdog's suggestions of Bezier curves is particularly well-suited if you want complete control over the curve and the movement along it.

If you want a basic parabolic trajectory, particularly if the ground is flat, I'd consider using parabolic trajectory formulas. To solve for the trail/arc:

  • Calculate the amount of time before object hits ground at the end. (Equation 4)
  • In a loop, use altitude equation to get the height of the object after N amount of time. (This could be an adjustable value in the editor so you could try different N amounts. The smaller N is, the smoother the parabola.)
  • Now you have a series of heights (Z) along your arc. By adding the X/Y vector of the direction you are firing along you get 3D points for your arc. You can use them to define a mesh, draw Gizmo lines, or render a trail.

You could also use this technique over non-flat ground. Instead of ending the loop after a certain amount of time, you'd end when the coordinates you calculate have collided with something.