Erain 3D
advert here

Level: Beginner Version : 1.0 Author : Thomas PFEIFFER Date : May 25th 2006
Reference : Skin your first cube


Transform your Box

Explanation

Transformations allow you to realize several effects on your objects, and they are of three types : rotation, translation, and scale.

Rotation allows you to rotate the Object's vertices around its center, or around a specific center point, on different axes (X, Y, Z, or your own)

Translation is used to place your object in the 3D space. You can of course move the object in the 3 directions.

Scale is use to change the object's dimensions. For example you can distort the object vertically by changing its Y scaling ( greater than 1 to stretch it, less than 1 to shrink it).

Combining transformations

After this short explanation, you have to know that you can mix several transformations. But to do that, you should first understand how to link them.

BUT BE CAREFUL, THE ORDER OF TRANSFORMATIONS IS VERY IMPORTANT

To illustrate this warning, here's a picture of what you get after a rotation followed by a translation :tutorials:sandy_illustration_1.jpg and a translation followed by a rotation :tutorials:sandy_illustration_2.jpg

As you see, you can very easily get a different result from what you expect.

Now that this is explained, let's go to a little example.

We still use our little cube, but with a different skin (to help you to understand how it works), and with a different mode.

QUAD mode:

Now a note about the two primitive generation modes available in Sandy : 'tri' (default value) and 'quad'.

Tri mode means that the primitive generation always makes three vertices per face, rather than 'quad' mode which allows four or more vertices per face (e.g Cylinder).

Quad mode makes your object render faster (for fewer faces will be created), but you can't texture it with correct perspective distortion. The real texture algorithm needs to have an object with 3 points per face – the tri mode.

Please check the documentation for further details.

The code:

This piece of code realizes two transformations : a rotation followed by a translation. For the first time you can see the tree representation of the 3D scene. This tree representation asks a bit more effort from the programmer, some steps might not seem useful, but when you get more advanced with the library and with more complex scene construction, you'll see the point of this method.

import sandy.core.data.*;
import sandy.core.group.*;
import sandy.primitive.*;
import sandy.view.*;
import sandy.core.*;
import sandy.skin.*;
import sandy.util.*;
import sandy.core.transform.*;
import sandy.events.*;
function init (Void):Void
{
	// screen creation, the object where objects will be displayed.
	var ecran:ClipScreen = new ClipScreen (this.createEmptyMovieClip ('ecran', 1), 600, 600);
	// we create our camera
	var cam:Camera3D = new Camera3D (700, ecran);
	// we add the camera to the world
	World3D.getInstance ().addCamera (cam);
	// we create the root node.
	var bg:Group = new Group ();
	// and set it as the root node of the world.
	World3D.getInstance ().setRootGroup (bg);
	// and we lauch the scene creation
	createScene (bg);
	// and now that everything is created, we can launch the world rendering.
	World3D.getInstance ().render ();
}
function createScene (bg:Group):Void
{
	// We create our object. It is a cube of 50 pixels, and with the quad mode, so here four points per face.
	var o:Object3D = new Box (50, 50, 50, 'quad');
	// we create a skin to "dress up" the object a bit, and make it visually better.
	//  SimpleColor skin is used here, it needs the fill color, alpha, and thickness
	var skin:Skin = new MixedSkin (0x00FF00, 100, 0, 100, 1);
	// We apply the skin to the object
	o.setSkin (skin);
	// We create two TransformGroup instances. Each one corresponds to a node of the tree we are going to create.transformations
	var tg1:TransformGroup = new TransformGroup ();
	var tg2:TransformGroup = new TransformGroup ();
	// We also create two instances of Translation objects.
	// Give them an appropriate name, this becomes important very rapidly.
	var translation:Transform3D = new Transform3D ();
	var rotation:Transform3D = new Transform3D ();
	// we translate the object by 500 pixels on the Z axis.
	translation.translate (0, 0, 500);
	// We rotate the object by 30 degrees on an axis oriented from the origin to the point (1, 1, 1).
	rotation.rotAxis (new Vector (1, 1, 1), 30);
	// We apply those transformations to the nodes.
	tg1.setTransform (translation);
	tg2.setTransform (rotation);
	// We add the rotation transformation to the node which represents the rotation by setting it as a child.
	// The rotation will be applied first, translation later (higher in the tree)
	tg2.addChild (o);
	// Now we link the two nodes
	// The order here is very important.
	tg1.addChild (tg2);
	// Now we simply link the translation node to the root node, thus completing the tree creation
	bg.addChild (tg1);
}
// We launch the animation creation.
init ();

Source and demo

Source :
.fla file

Demo :