Lesson 3
The hierarchy of objects
Level:
Beginner Version of the Xtreme3D: 3.0.x
Author: Gecko
The concept of hierarchy we have already met, but until now we have not viewed it in practice. Many who are not familiar with the approach of the objects, and do not know what a huge savings of time and effort here. The hierarchy allows without any work to do that too difficult or impossible without its use. It comes to the specifics of the movements of objects in some special cases.
Imagine, for example, a situation: it is necessary to simulate a simple star system - the sun and rotating around the planet. Around the world, in turn, rotates the satellite. For simplicity, we will still think in two-dimensional space. How can I do?
Let the Sun - the sun, the Planet - the planet, Moon - the satellite. Each object has two coordinates are X and Y, as well as the angle of rotation around its axis - A. Then (in the ����������)
Sun.X = 0
Sun.Y = 0
Planet.X = Sun.X + cos(Sun.A) * 10
Planet.Y = Sun.Y + sin(Sun.A) * 10
Taking into account that the distance between the sun and the planet is 10 conditional units. When turning the sun around its axis, the planet will rotate around it, moving the coordinates calculated from the rotation angle of the sun and the distances to be searched. Now it is easy to likewise calculate and coordinates of the Satellite:
Moon.X = Planet.X + cos(Planet.A) *2
Moon.Y = Planet.Y + sin(Planet.A) *2
Manually But this is not always convenient. Especially, if the system is not three object as well, for example, all ten. Or the location of objects varies periodically (for example, the satellite is lifting from one planet and goes to the other). Wise will automate the process by entering for each object in the property of the parent (Parent):
Planet.Parent = Sun
Moon.Parent = Planet
And update the coordinates of the objects the same for all formula:
Object.X = Object.Parent.X + cos(object.Parent.A) *2
Object.Y = Object.Parent.Y + sin(object.Parent.A) *2
And is the simplest hierarchy.
With 2D graphics all is relatively simple. But what about the 3d? In the 3D graphics in addition to the sinuses and ��������� used vectors and matrices. Operations with them quite ����������� and extremely difficult for understanding a newcomer. In addition, all too often, to carry out such operations at the level of the GML irrational: for the storage arrays under the matrix would require more memory and mathematical operations with them will reduce the FPS. But not all so terribly. Xtreme3D assumes all demanding computing, executing them at the level of the machine code, so its hierarchy will work much faster and more accurately than the written manually on the GML.
When using the built-in hierarchy of the Xtreme3D all work is directed by the parents to objects. The trick is that the descendant inherits the coordinate system of the parent. For example, the coordinates of the parent (X, Y, Z) are the coordinates of the Center, on which the count their own coordinates its descendant (X+x, y+Y, Z+z). The descendant, in turn, sends its own coordinates to their descendants, and so on. Own object coordinates are called local.
The coordinate system may be transformed displacement, rotation, or scale. Turning the Local coordinate system of the parent causes change of direction of the axes in the inherited coordinate system, the descendant, which automatically leads to its rotation in space. If, at the time of the rotation of a descendant was at some distance from the center of the inherited them coordinate system, it will look like the rotation of the descendant of around its parent. Just as in our example!
To establish a system of sun and planets in our case it is sufficient to write something like this:
Sun=SphereCreate(4, 24, 24, global.scene);
ObjectSetPosition(sun, 0, 0, 0).
Planet=SphereCreate(1, 24, 24, sun);
ObjectSetPosition(planet, 0, 0, 10);
Moon=SphereCreate(0.5, 24, 24, planet);
ObjectSetPosition(moon, 0, 0, 2);
SphereCreate function creates a sphere. You must specify its radius, as well as the number of meridian and parallels. Our Sun radius is equal to 4, the planet - 1, the satellites - 0.5. The meridians and parallels (slices, stacks) divide the sphere into squares, the number of which determines the quality of the appearance of the sphere. Usually it is sufficient to point 24 of the meridian and 24 parallel.
Now you can in the event Step turn, the sun and the planet:
ObjectTurn(sun, 2);
ObjectTurn(planet ,6);
...And observe the manifestation of one of the most important properties of the object hierarchy. A proper use of these properties is the main task of working with the Xtreme3D. This kind of manifestations can be observed not only in space, but at every step, so it is important to have an effective means of modeling.