Guide:Physics tricks

From ActiveWiki
Jump to navigation Jump to search

Physics can be used for two techniques to enhance interactivity; a random generator and to simulate wind dynamics.

Random generator

For this tutorial, I will be making use of a recent creation: Zarco, the coin-operated boy wonder. His function is to provide fortunes to visitors. As the browser does not contain a random command, I use physics (and juju) to randomly decide the fortune. This can be used to pick fortunes, numbers, names or any values and actions you desire (e.g. randomly decided traps)

Basic operation

Zarco in his cabinet, giving out wise directions. Watch him operate on YouTube

Zarco is triggered by a button, which sets of a chain of animate commands. These commands cause a physical ball to spin around in an enclosure of panels with the collide panel. The ball is then given a "local axis" (ltm) force, pushing it toward a panel. The collided panel then gives its fortune. There are 24 possible fortunes that Zarco may give at any time.

Setting up the "picker"

A random fortune is given by a spinning nograv ball enclosed in a chamber. First, create the "picker" ball using a small object such as "p1sph0005" (Note: Despite the fact that this object has a bottom-center axis, the physics engine correctly sets its axis to the mesh's center). Give it the following code:

create
  name rand,
  colltag 10,
  collider nograv;
collide
  reset physics,
  addtorque 250 20 59

The create trigger sets properties for the ball:

  • Its name is "rand", which will allow other objects to modify it
  • Its "colltag" is 10; this will be used to allow collision events between it and the fortune panels
  • "collider" makes it a physics object. The "nograv" attribute is important as it makes it float instead of falling.

The collider trigger causes the ball to:

  • "reset physics"; when the ball collides with a fortune panel, it will return to its original position
  • "addtorque"; this causes the ball to spin wildly after resetting. This gives it some "randomness" to the next choice.

Setting up the "fortune panels"

Zarco's random chamber; this is where the "picker" ball is kept. Watch the picker in action on YouTube

Surrounding the ball is the chamber, which is made up of small panels. In Zarco's case, this chamber is a cube consisting of four panels on each side (using the "a" object to reduce cell space). All sides must be enclosed to prevent the ball from flying off into the unknown. Each panel should contain the following code:

create
  collider static,
  colltag 10;
collide
  say "Zarco:  <MESSAGE>"

The create trigger sets properties for the panel:

  • "collider" makes it a physics object. The "static" attribute is important as it makes the panel unmovable.
  • Its "colltag" is 10; this will be used to allow collision events between it and the picker ball

At this point, the code after the collider trigger is up to you; you can make an object nearby move, play a sound, start a media, anything. In Zarco's case, all panels use the same command:

  • "say" causes the object to say something in chat. In this case, all messages start with "Zarco: " with an invisible tab character between his name and the message; this makes him look like a nearby person. Example fortunes that Zarco say are:
    • collide say "Zarco: Probably."
    • collide say "Zarco: Reception is fuzzy; ask again later"
    • collide say "Zarco: Keep taking left turns and you will find out"

Setting up this button

To invoke the randomizer and spit out a random fortune, a button (or bump panel) is required. Zarco's button uses a complex astart mechanism, but I will explain a more simple method. With this code, a second or two after the button is clicked, a random action should be performed. Create a button with the following code:

activate
  addtorque 9 -7 8 name=rand,
  addforce 5 3 1 ltm name=rand;

The activate trigger causes the button to do the following when clicked:

  • "addtorque" causes the picker ball to spin. The numbers are not significant; you should change these to random values. This is the fundamental "random" functionality.
  • "addforce" is what causes the ball to move into a fortune panel, eventually colliding with it.

Entropy and why Zarco is not actually random

This trick will never be truly random; this and other random generation techniques used by computers are always "psuedo-random". Especially with the technique in this tutorial, it can be predictable. For example, Zarco's first fortune would always be "Oh... I've been meaning to ask you about that...". This is because (using the above code example) Zarco will always start with a spin of "9 -7 8".

To make Zarco more random, something called "entropy" needs to be added. Entropy is what makes real-world random number generators more "random"; this entropy can be readings from the atmosphere, monitoring static on radio waves or even monitoring cosmic background radiation. While Active Worlds does not yet have the ability to read from cosmic background radiation, it is still possible to add "entropy" to the randomizer.

A floorboard that provides entropy to the randomizer for those who approach Zarco.

To do this, I have added "bump" triggers on floor objects around Zarco that addtorque to the picker ball. For example, this code is on the wooden floorboards in front of Zarco:

bump addtorque 10 2 18 name=rand global

The bump trigger causes the random picker ball to spin around when the floor is walked on. Again, these numbers are insignificant; you should change them to different numbers for each bump trigger. An important attribute of this command is "global"; this causes the ball to spin for others nearby, giving them additional entropy.

This way, when a visitor approaches Zarco, he ideally should get a different first answer than others. Other triggers can be combined too, such as enter zone or VRT timers using the at trigger to provide entropy every minute.

Error handling

One problem with this trick is that it requires physics. This can present a problem when visitors have physics disabled; triggering Zarco would make nothing happen (or in the AWReunion version's case, it simply says "Zarco whirrs to life in his machine..."). To prevent confusion, error handling should be implemented. Change the button code to the following:

Note: Although I use a legacy animate timer here, you can also use the timer command and at. Personal experience has shown me that timer is an unpredictable command, however. YMMV.
create
  name zerror,
  animate me . 1 1 5000;
activate
  addtorque 9 -7 8 name=rand,
  addforce 5 3 1 ltm name=rand,
  astart;
adone
  say "Zarco is unable to answer as he requires physics to be enabled"

The create trigger sets properties for the button:

  • Its name is "zerror", which will allow the random picker to stop the "error timer"
  • The animate command gives it a time out of 5000 milliseconds (5 seconds); this should be more than the time it takes for the picker ball to pick a fortune.

The activate trigger causes a new thing to happen when clicked: It starts the five second "animate" timer. Then, the adone trigger causes Zarco to say a helpful error message when the five seconds are up.

Now to make the picker ball report its success upon picking a fortune, change its code to the following:

create
  name rand,
  colltag 10,
  collider nograv;
collide
  reset physics,
  addtorque 250 20 59,
  astop zerror

The collide trigger now contains the command "astop zerror". What this does is it stops the button's five second timer upon colliding with a fortune panel, preventing the error showing up as clearly, the physics engine is enabled for that particular user.

Notes

  • Be careful introducing zones with unusual gravity settings such as 0 or negative values; this can cause the randomizer to spam fortunes in chat or break it.
  • Although virtually you can surround the ball with as many panels as possible, it is not recommended. The physics engine has a low resolution; the smaller the panels, the more likely the picker will trigger more than 1 fortune at a time.
  • Do not add the "global" attribute to the button; as physics is not global, people near Zarco will see different fortunes to others.

Simulating wind dynamics

Although cloth supports wind paramters, this is not controllable by commands. Using this trick, it is possible to make an environment feel more alive and reactive to the atmosphere. I use this in the AWReunion (world) welcome cabin to make the foyer's banner get blown away by the harsh outdoor winter winds.

Opening the front doors causes the harsh winter winds to blow inside, affecting the cloth banner. Watch on YouTube

Basic operation

This trick makes use of an invisible physical ball, a looping animate timer and another time-out timer. The invisible ball is parked in front of the cloth (front toward the source of the wind). When triggered, the repeating looping timer repeatedly resets and throws the ball into the cloth banner, making it look like buffers of incoming wind. Eventually, this loop is stopped when the door is closed by using another timer.

Setting up the "wind ball"

The "wind" ball exposed; cloth is invisible whilst in build mode.

Create a ball object (such as p1sph0100) in front of the cloth or physical object you wish to apply wind to. Apply it relatively high to the cloth's center point. Rotate it so that its X axis is facing the cloth. Add the following code to the ball:

create
  name wind,
  animate me . 1 1 1000,
  visible no, solid no;
adone
  reset physics name=wind,
  addforce 25 ltm,
  astart

The create trigger sets properties for the ball:

  • Its name is "wind", which will allow other objects to trigger its timer
  • It is given a timer of 1000 milliseconds (one second) using the legacy animate command
  • It is made invisible and non-collidable by users (this will not affect physics)

The adone trigger causes the following to happen when the ball's one second timer is triggered:

  • The physics are reset for the ball, causing it to move back into original position
  • It is pushed into the cloth (assuming its X axis is toward the cloth) using the "addforce" command
  • "astart" triggers this timer again after one second; this causes a loop

Setting up the door and "time out"

For this tutorial, I will explain how to trigger the "wind" using a door. However this can be applied to any event, such as falling rocks exposing an opening. Create a door that is ideally facing the cloth with the ball in between. Add the following code:

create
  animate me . 1 1 10000;
activate
  rotate -10 time=2 smooth wait=8,
  collider name=wind,
  astart wind,
  astart;
adone
  astop wind,
  collider static name=wind

The create trigger gives the door a timer of 10000 milliseconds (ten seconds) that will be used to end the wind's timer loop. This time is chosen to coincide with the door's eight second rotate command.

The activate trigger causes the following to happen when the door is clicked:

  • The door opens and holds its position for eight seconds
  • The wind ball is made a physical object
  • The wind ball's one second looping timer is started, making it throw itself into the cloth every second
  • The door's own ten second timer is started

The adone trigger causes the following to happen when the door's ten second timer is triggered:

  • The wind ball's repeating one second timer is stopped
  • The ball is made a static object, to prevent lag