1 Tutorials LightWave 3D Expression Physics: Projectile Trajectory Qua Jan 26, 2011 2:24 am
Admin
Admin
Download source files here. |
A virtual environment like LightWave 3D® is great for doing simulation work. It's even better for somebody who can do a little programming, because you can bring physical things to life. That's what we'll do in this tutorial. We'll convert some physics equations for projectile motion into LightWave Expressions. The branch of physics that deals with motion is known as mechanics. Projectile motion falls within this branch. The physics equations we'll need to simulate the phyiscal motion of a projectile are: <blockquote> x = v0 cosθ0 t y = v0 sinθ0 t - ½gt² </blockquote> Some of the elements of these formidable-looking equations are v0 (V-naught), which represents the initial velocity of the projectile, θ0 (Theta-naught) which indicates the initial angle of elevation from the horizontal, t for the time index, and g for the acceleration due to gravity (which, for the Earth, is approximately 9.8 m/s²). Let's talk a little first about converting these physics equations into something usable. Converting the Equations Converting these equations into LightWave Expressions is a simple matter of performing a step-by-step substitution of the the components previously described with the values we need. For instance, if we selected an initial velocity of 10 m/s for the projectile, this value will go into both equations as a direct replacement for v0: <blockquote> x = 10 * cosθ0 t y = 10 * sinθ0 t - ½gt² </blockquote> Next, for θ0, we will use the banking angle of an object in the scene called "Cannon". We will use the Expressions channel syntax to acquire the initial angle of the equation based upon the banking of the scene object: <blockquote> x = 10 * cos([Cannon.Rotation.B,0.0]) t y = 10 * sin([Cannon.Rotation.B,0.0]) t - ½gt² </blockquote> A minor problem with using this angle measurement directly is that the sin() and cos() functions expect angles to be in radian measurement, while the channel expressions used above return them in degrees. So, we must wrap each channel reference in a call to rad() to convert to the correct units: <blockquote> x = 10 * cos(rad([Cannon.Rotation.B,0.0])) t y = 10 * sin(rad([Cannon.Rotation.B,0.0])) t - ½gt² </blockquote> The t element in each equation can be substituted rather neatly using the Expressions Time variable: <blockquote> x = 10 * cos(rad([Cannon.Rotation.B,0.0])) * Time y = 10 * sin(rad([Cannon.Rotation.B,0.0])) * Time - ½g(Time)² </blockquote> And g can be replaced directly with the acceleration value for the Earth's gravity, 9.8 m/s²: <blockquote> x = 10 * cos(rad([Cannon.Rotation.B,0.0])) * Time y = 10 * sin(rad([Cannon.Rotation.B,0.0])) * Time - ½ * 9.8 * (Time)² </blockquote> Some final fix-ups on the equations for proper evaluation order will give us the initial expressions we can use in LightWave to simulate the ballistic path of a projectile in Earth's gravity: <blockquote> x = 10 * cos(rad([Cannon.Rotation.B,0.0])) * Time y = (10 * sin(rad([Cannon.Rotation.B,0.0])) * Time) - (0.5 * 9.8 * (Time * Time)) </blockquote> Applying the Expressions In a clean scene, we'll first add a Null object, and give it the name "Cannon". In addition, we'll also load an object that will server as our projectile: For the purposes of interactivity, be sure that your "Auto Key" setting is turned on. Now open the Graph Editor. Make sure you have the the X channel of the projectile selected, and add a new expression: Take the expression we developed previously for the X channel value, paste it into the expression "Value" area, and select "Apply": Follow the same steps for the Y channel expression: Back in the main LightWave window, select the "Cannon" object, and adjust its Bank angle value to 26.5 degrees: Pressing the play button will cause the projectile to launch away from the "Cannon" object at an initial elevation of 26.5 degrees, and an initial velocity setting of 10 m/s: By changing the Bank angle of the "Cannon" object, the projectile object will automatically change its trajectory without any further changes to the expressions controlling it: Adding More Control The existing system is useful as it is, however, we can make it a bit more interactive by allowing the user to adjust some of the operational values within the active expressions through the user interface. For instance, we can allow the user to adjust the initial velocity of the projectile by adding a new Null object to the scene (call it "Vo"), and mapping the X position of this Null object to the initial velocity setting in the expressions. First, we add a new Null and call it "Vo". We give it an initial X position of 1 meter: Next, we adjust the expressions we developed earlier by using the X position of the "Vo" Null object to control the initial velocity of the projectile. In order keep us from having to run the velocity object off the screen, we will scale its X position value by 10 (so that each meter of its position equals 10 m/s of initial velocity). <blockquote> x = (10 * [Vo.Position.X,0.0]) * cos(rad([Cannon.Rotation.B,0.0])) * Time y = ((10 * [Vo.Position.X,0.0]) * sin(rad([Cannon.Rotation.B,0.0])) * Time) - (0.5 * 9.8 * (Time * Time)) </blockquote> Another adjustment we can make is to keep the projectile from falling below the level of the "Cannon" object. In the current expressions, this can happen after the projectile falls back to the ground level defined by the "Cannon" object: We can apply the clamp() function to the Y expression to prevent the projectile from falling below the level of the "Cannon" object's Y position. In the clamp() function, we set the lower level (minimum) value to be the "Cannon" object's Y position, and the uppper level (maximum) value to be some arbitrarily high number: <blockquote> clamp(((10 * [Vo.Position.X,0.0]) * sin(rad([Cannon.Rotation.B,0.0])) * Time) - (0.5 * 9.8 * (Time * Time)),[Cannon.Position.Y,0.0],50000.0) </blockquote> Going Beyond... The expressions we just created represent pretty much the limit of what we can accomplish with them within LightWave®'s Expressions system. A great deal more can be done (for instance, adding an arbitrary floor for the projectile to land upon) by controlling the projectile through an LScript. Now that the equations have been converted for you, as a personal exercise you may want to try writing your own LScript (Item Motion) that reproduces the actions of the these expressions, and then add more features to it. |