- Home /
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.
[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.
Answer by runevision · Nov 23, 2009 at 04:52 PM
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:
You need to calculate a trajectory and then sample a number of points along that trajectory. (Output is an array of points.)
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.
Answer by Bampf · Nov 24, 2009 at 04:23 AM
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.
Also take a look at the LineRenderer component if you want more procedural control over precisely where the trajectory is rendered.
Answer by TowerOfBricks · Nov 23, 2009 at 07:45 PM
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
Answer by anomalous_underdog · Nov 25, 2009 at 01:57 AM
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.
I don't have code on hand for bezier curves but maybe this could help: http://www.unifycommunity.com/wiki/index.php?title=Spline_Controller
Answer by Bampf · Nov 25, 2009 at 01:06 PM
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.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
question about invisible walls 2 Answers
Camera Meshrenderer in a multiplayer game 2 Answers
Cutting a Mesh Into Smaller Pieces 5 Answers
Mesh deformation simulating a fire 0 Answers