1# Soya 3D tutorial
2# Copyright (C) 2004 Jean-Baptiste LAMY
3#
4# This program is free software; you can redistribute it and/or modify
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19# basic-3: Time management : a randomly moving sphere
20
21# In this lesson, we'll create a spherish head that moves around randomly.
22# You'll learn about time management (second part), vectors and coordinate-system
23# conversion.
24
25
26# Import the Soya module.
27
28import sys, os, os.path, random, soya
29
30soya.init()
31soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
32
33# Creates a scene.
34
35scene = soya.World()
36
37
38# Creates the randomly moving sphere's class. We call it head, because we'll use
40# This class inherits from soya.Volume, so it can have a shape (the head).
41
43
44  # Redefine the constructor.
45
46  def __init__(self, parent):
47
48    # Calls the soya.Volume constructor (remember, calling the super implementation is
49    # always a good idea), and use the shape called 'caterpillar_head'.
50
52
53    # Adds a speed attribute to our new object.
54    # The speed is a Vector object. A Vector is a mathematical object, used for
55    # computation ; contrary to other object (Light, Camera, Volume, World,...) it does not
56    # modify the rendering in any way.
57
58    # A vector is defined by a coordinate system and 3 coordinates (X, Y, Z) ; here the
59    # speed is defined in 'self', i.e. the Head, and with coordinates 0.0, 0.0, -0.2.
60    # Remember that in Soya, the -Z direction is the front. So the speed
61    # This means that the speed vector is parallel to the direction the head is looking
62    # at, and has a length of 0.2.
63
64    self.speed = soya.Vector(self, 0.0, 0.0, -0.2)
65
66  # Like advance_time, begin_round is called by the idler.
67  # But contrary to advance_time, begin_round is called regularly, at the beginning of each
68  # round ; thus it receive no 'proportion' argument.
69  # Decision process should occurs in begin_round.
70
71  def begin_round(self):
72
73    # Calls the super implementation.
74
75    soya.Volume.begin_round(self)
76
77    # Changes the direction of the head, by rotating it around the Y axis, of a random
78    # angle between -25.0 and 25.0 degrees.
79
80    # Notice that after the rotation, the speed vector is still parallel to the direction
81    # the head is looking at, since the vector is defined 'inside' the head.
82
83    self.rotate_lateral((random.random() - 0.5) * 50.0)
84
86
89
90    # Moves the head according to the speed vector.
92
93    # Notice that the head is defined is the head.parent coordinate system (e.g. the scene)
94    # though the speed vector is defined in the head coordinate system.
95
97
98
99# Creates a Head in the scene.
100
102
103# Creates a light.
104
105light = soya.Light(scene)
106light.set_xyz(2.0, 5.0, 0.0)
107
108# Creates a camera.
109
110camera = soya.Camera(scene)
111soya.set_root_widget(camera)
112camera.set_xyz(0.0, 15.0, 15.0)
113
114# Makes the camera looking at the head's initial position.
115# The look_at method is another rotation method ; it makes any 3D object looking toward
116# the given position (a 3D object or a Point), or in the given direction (if the argument
117# is a Vector).
118