1# Soya 3D tutorial
2# Copyright (C) 2004 Jean-Baptiste LAMY
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
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.
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
19# basic-3: Time management : a randomly moving sphere
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
26# Import the Soya module.
28import sys, os, os.path, random, soya
33# Creates a scene.
35scene = soya.World()
38# Creates the randomly moving sphere's class. We call it head, because we'll use
39# a head-like shape.
40# This class inherits from soya.Volume, so it can have a shape (the head).
44 # Redefine the constructor.
46 def __init__(self, parent):
48 # Calls the soya.Volume constructor (remember, calling the super implementation is
49 # always a good idea), and use the shape called 'caterpillar_head'.
51 soya.Volume.__init__(self, parent, soya.Shape.get("caterpillar_head"))
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.
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.
64 self.speed = soya.Vector(self, 0.0, 0.0, -0.2)
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.
71 def begin_round(self):
73 # Calls the super implementation.
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.
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.
83 self.rotate_lateral((random.random() - 0.5) * 50.0)
85 # In advance_time, we make the head advance.
87 def advance_time(self, proportion):
88 soya.Volume.advance_time(self, proportion)
90 # Moves the head according to the speed vector.
91 # add_mul_vector is identical to: self.add_vector(proportion * self.speed), but faster.
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.
96 self.add_mul_vector(proportion, self.speed)
99# Creates a Head in the scene.
101head = Head(scene)
103# Creates a light.
105light = soya.Light(scene)
106light.set_xyz(2.0, 5.0, 0.0)
108# Creates a camera.
110camera = soya.Camera(scene)
112camera.set_xyz(0.0, 15.0, 15.0)
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).