Before we start, it's recommended you have a sound understanding of the other subjects. Use of joints and bodies is essential, and you must know how to operate the toolset. If you have not looked over these subjects already or do not have an understanding already please go back and read through before continuing beyond this point.
- A good starting point before continuing with this tutorial would be to: make a box, place 4 cylinders in the corners to be the wheels, make some hinges, and group them with your starting box, use the Joint connector tool to connect the hinges and the wheels together, and then make a staticmesh floor shaped as a ramp. Click play. If your car rolls down the ramp, continue with this tutorial. If it doesn't roll, go back and fix it until it does roll down the hill.
Script is used in a number of models on the Google 3D Warehouse, to create interesting behaviors and to make control of the simulation easier. Scripts don't really do one particular thing. Scripting is a tool, that if you know how to use it, can do almost anything. I'll start off as basic as I can, with an explanation of SketchyPhysic's controllers.
These are inputs, from the user, that can change every frame of the simulation. The perfect beginner example is a slider, which will appear in the control panel shown during the simulation. It can be dragged around between 0 and 1, and 'outputs' a number every frame, telling the controller where it is. This can be used to control joints dynamically. Try this; select a joint, and in the Controller-field type:
Your first bit of code! It creates a slider, called 'one' in the control box. Play the simulation and see for yourself! Now that you've got that, we can do something a bit more interesting. Type:
Now play the simulation. Move around the sliders... interesting, huh? Other inputs, like joysticks, buttons and keys, can be used in the same way, and you aren't limited to addition. Oh no. You can basically write any sum you can think of in the formula boxes; see the main Ruby page for the list of operators. You're also able to use fixed numbers in any combination with dynamic inputs. So now, onto stuff that might be in some way useful:
That's the basic formula used by many modelers to control a thruster. When space isn't being pressed, it's 0, and 0 X 200 still equals 0. But when it's being pressed, it's 1, and 1 X 200 is 200, a value normally high enough to lift an object off the ground. Of course, 200 can be any number, and the key can be any key or button. Play around with these formulas, and make your own (just write it like a sum), and when you feel confident, you may want to move onto true scripting!
What you've just done is actually a formula; nothing that can truly be called a script, as it performs no logical functions. I'll start you off with a script that runs through a series of values, with a new value every few frames.
I don't expect this looks very friendly, but it can be quite useful; in the first , you will find the list of values that this micro-script cycles through. The second  tells it how often to change, where 'frame' is the current frame number of the simulation. Think about it... if the frame number is 100, then 100/ (divided) 50 is 2. This tells the script to use the second value in the list. The %5 resets the cycle when it reaches the value, as there is no sixth value, the script would stop working. The number here must be the same as the number of values in the list.
Now, how to make it work for you. You may have realized that you can change the numbers in the list (they don't have to be numbers, but we'll get to that). What you can also do is change the gap between cycles (the number of frames, in the example it's 50), and the number of values. So,
Works just as well.
The whole idea of scripts is that they're adaptable. There's no set list of them, you can change and combine them in an infinite number of ways. On a vehicle, this script can be adapted to make it drive around on its own, turning and throttling at set intervals. If you use it to control a robot arm's joints, it can be set to move on a loop, picking up objects, moving them, and dropping them.
Tidibits that may come in handy when creating and modifying your own formulas and scripts:
This function allows you to generate random numbers on the fly. Just type rand to get a random decimal number between 0 and 1 (It will never be 0 or 1), or rand(10) (where 10 can be any whole number) to get a whole number between 0 and 10.
- Tip: Using only rand as a controller is generally ill-advised, as a new value will be generated every frame, resulting in extreme movements. You may wish to create a variable on the first frame of the simulation as a random number, and retrieve it every frame thereafter; this, among other things, will be covered in the next section.
Joystick / keyboard controls
An outline of the joystick control input commands (every one of these should be placed within the brackets and quotes of:
- lefty (Left stick up/down or w/s)
- leftx (Left stick left/right or a/d)
- righty (Right stick up/down or up/down arrow keys)
- rightx (Right stick left/right or left/right arrow keys)
- Not to be used within the joy command. On an Xbox 360 style joypad, this is the analog triggers. On a PS2 style joypad, it works like rightx, but has no keyboard emulation, and finally, on some flight simulator joysticks, it is the throttle.
- Tip: It's a good idea to use these instead of sliders for control. Sliders are somewhat outdated, and, while they are still appropriate in some situations, the joysticks and keyboard emulation make control easier.
Key and button controls
Keys can also be used to control the simulation. Use key("q"), where q can be any key; key inputs work through joystick emulation, where a key in the up position will give 0 and a key being pressed will give 1. Button controls work in a similar way; joybutton("a"), where a could be a,x,y,b,lb, or rb. Special keys are as follows:
- all of the F1, F2, F3 keys
(Add more if you find them)