Sunday, February 1, 2009

Second Life mathematics

About a year or so ago I played around with Second Life but found it was limited as an environment due to lack of integration with IM or Skype (I was looking to use it as an intuitive conferencing tool). It still lacks these features but I've had a little fun recently playing around with the scripting language to play with polygons using Second Life as my geometry lab.

The maximum object size (normally) you can create is a 10x10x10m cube. You can reshape this up in different ways (like a cylinder) but with the premise of making a simple house/room I created a couple of scripts to make giant cubes and domes using a 10x10x0.025m panel as a building block.

My recent experiment was to create a script to use these flat panels to make an N-sided polygon shaped wall in a ring around my avatar. The only tricky bit of math is the distance between the centre point (my avatar) and the 10m wide panel.

This formula does the trick:
distance = panelwidth (2×tan[π/N])
You can work this out by thinking about the right-angled triangle between the avatar, middle of the panel and the edge where the next panel joins:
  1. The base of the triangle shown must be ½ the panel width
  2. the angle "a" cuts up the circle into 2xN triangles (a full circle is 2π in radians) and so "a" = 2π/2N = π/N.
  3. Basic trigonometry gives us the tangent of "a" equal to the opposite side (which we know) over the adjacent side (which is the distance we want).
Programming bit: Note that SL limits the distance you can create (rez) an object to 10m, so my script creates the panel in a small size near the avatar (llRezObject) with the right final rotation, orders the panel (the panel has its own script listening for this command) to the target position (llSetPos) and then the panel resizes itself (llSetScale) when it realizes it has reached the target position (llGetPos). Phew.

No comments: