Scripting collision events

Tutorial:Virtual Worlds Made Easy Scripting Collision Events
Time to complete: 5 minutes approximately
Summary: This tutorial will show you how to turn on a light with a collision event.
Aim: Learn to insert primitives, use scripting and trigger events.
Required application: VastPark Creator

Hey all, I'm going to show you how to make a park were a light gets turned on using a script.

To start off we're going to add the objects required to make the park. Let's start with a box, make its position 0, 0, 0 and its size something like 1, 0.1, 1. The size isn't really important so you can make it as big as you want. It's also important to turn physics on the box, and can be done be checking physics enabled to be on. This can be found in the Behaviour menu of the boxes properties.

Now let's put some lights into the scene, we're going to need a directional and a point light for this tutorial. Position the directional light at 0, -5, 0 and adjust its diffuse value to #494949
The point lights properties can be set to whatever values that you like, just make sure that light can be seen on the box. I positioned the point light at 0, 2, 0, and set its Diffuse value to#0022FF. It's Range is set to 60 and its Constant Attenuation is 1.00. Once you're happy with your lights values disable the point light in its appearance menu .

Now make a sphere, enable its physics and make it movable remembering to give it a weight so gravity can affect it. Change its size to 0.5, 0.5, 0.5 and place its directly above the box you created earlier.

The scene is now set up, we just need to add some interactivity through scripting. To create a script go to the insert menu and click on Inline Script, give it a name like 'OnCollision'. The script will then be added to the project list and by double clicking it will open up a scripting window. Now add this script in:

1.function main(obj, args)
2.scene:getelement('light').enabled = true
3.end

Remember to change the name of the element in the script to correspond with the name of your light.

The only thing left now is to add a trigger to fire the script. Select the sphere and in its properties there is a menu called triggers. The first dropdown corresponds to the event you want it to occur on, we want "collided". The second dropdown is for your script, select it and click the + to add the trigger to the sphere.

Remember to add a camera to the scene, and make sure its pointing to the box. Under the scene properties set Camera to whatever you named the camera

That's it, now click Interactive Preview to watch your work in action. On collision events can be used for all sorts of situations, have a play with them and see what you can come up with.

VWME2_final_image.jpg (6.1 kB) Michael Louca, 03/02/2010 01:55 pm

VWME2_scene.jpg (10.2 kB) Michael Louca, 03/02/2010 01:55 pm