Grouping and Hierarchy
Grouping is an important concept in YG. You can group objects together and then move the whole group around at once, which helps in arranging objects in the scene and with arranging the YG code itself. For instance, you could arrange objects within a room, then move the entire room around to position it within a house. You could also group people together inside a car, so that when the car moves, the people inside move along with it.
Another important aspect of grouping is control grouping. For example, you might want to group some objects together and make them all appear and disappear together; or, you could make a group of objects into an active trigger, so that pointing at any object in the group would trigger a response.
To see an example of grouping, consider the living room example. We'll put some objects on the coffee table, group them with the table, and move the whole group around the room.
// living room scene with objects on coffee table
light ()
object (file("coffeetable.pfb"))
object (file("coffeecup.pfb"),position(1 .5 2))
object (file("vase.pfb"),position(-.5 0 2))
object (file("knight.pfb"),position(.75 -.5 2))
object (file("sofa.pfb"),position (0 4 0))
To group the coffee table and other objects together, we use a Transform node:
// living room scene, with objects and coffee table grouped
light ()
transform (position(2 0 0))
{
object (file("coffeetable.pfb"))
object (file("coffeecup.pfb"),position(1 .5 2))
object (file("vase.pfb"),position(-.5 0 2))
object (file("knight.pfb"),position(.75 -.5 2))
}
object (file("sofa.pfb"),position (0 4 0))
a transform node is a node that does not display anything - it is only used for transforming (moving, rotating, and scaling) a group of other nodes. The group of nodes is contained in Curly Brackets after the transform node. The nodes in the brackets are called children, and the transform node is called the parent. You can put transforms inside other transforms, creating a hierarchy of nodes. Drawing a diagram of this type of structure looks a little like a family tree, hence the parent & child idea.
try different values for the position of the transform. note that while the coffee table and objects move, the sofa does not, because it is not part of the group - not one of the transform's children.
You can also use the size and orienation messages with the transform node, the same as with the object node.
Coordinate Systems
Change the position of an object on the coffeetable; say, put the knight at (0 0 2). Now try different positions for the transform node. The knight's X/Y position, 0,0 is at the origin, the center of the coordinate plane. But notice that it is NOT at the center of the whole world, but at the center of the table! When an object is a child of a transform, its own size, position, and orientation are RELATIVE to its parent transform. For comparison:
// 1. knight is not a child of the coffee table transform
transform (position(5 0 0))
{
object (file("coffeetable.pfb"))
}
object (file("knight.pfb"),position(0 0 2))
// 2. knight IS a child of the transform
transform (position(5 0 0))
{
object (file("coffeetable.pfb"))
object (file("knight.pfb"),position(0 0 2))
}
In the first example, the knight is positioned in global coordinates. In the second, it uses coordinates local to the coffeetable. Imagine a coordinate grid on the surface of the coffeetable, with 0,0 at the center. When you move an object on the table, it is positioned on that grid, not on the grid of the whole world. This is one of the most confusing concepts in 3D graphics, and incidentally, consistently baffles Flash programmers too.
Another way to think about local coordinate systems is as a way of providing context for the coordinates you're providing. Global coordinates are used when you want to think large; local coordinates are used when you're focusing on a smaller area in the world. Chicago is a great example of a local coordinate system - the city is built on a nice, even grid, with the origin in the center of the Loop. You can give someone directions in Chicago to, say, Halsted and Fullerton, with numbers like "800 West, 2400 North," which is like the coordinates (-800,2400) on an X/Y plane. This coordinate system assumes that 1) Chicago is a completely flat plane, and 2) that Carson Pririe Scott is the center of the universe. Assumption 1 is pretty close to true, but even aside from the curvature of the earth, you could give directions in terms of latitude and longitude, or in GPS coordinates, which would give you a location in relation to the whole surface of the planet, not just State Street. Thinking even larger, you could give coordinates in 3-dimensional space with the center of the earth as the origin; or with the sun as the origin; and so on.
(c) Ben Chang