2
# Copyright (C) 2001-2002 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
20
# This lesson includes a red laser ray and 3 rotating cubes.
21
# Move the mouse to move the laser.
22
# In particular, lasers have been used while testing raypicking function.
24
import soya, soya.soya3d as soya3d, soya.model as model, soya.cube as cube, soya.widget as widget, soya.idler as idler
25
from soya.math3d import Point
27
# This is the same as lesson 105 (3 rotating cubes)
31
scene = soya3d.World()
33
import os, os.path, sys
34
data_dir = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "data")
36
model.Image .PATH = os.path.join(data_dir, "images")
37
model.Material.PATH = os.path.join(data_dir, "materials")
38
soya3d.World .PATH = os.path.join(data_dir, "worlds")
39
model.Shape .PATH = os.path.join(data_dir, "shapes")
41
material = model.Material()
42
material.tex_filename = "block2.tga"
44
cube = cube.Cube(None, material)
46
shape = cube.shapify()
48
volume_1 = soya3d.Volume(scene, shape)
49
volume_1.set_xyz(-1.1, 0.5, 0.0)
51
volume_2 = soya3d.Volume(scene, shape)
52
volume_2.set_xyz(-1.5, -1.5, 0.0)
54
volume_3 = soya3d.Volume(scene, shape)
55
volume_3.set_xyz(1.1, -0.5, 0.0)
57
light = soya3d.Light(scene)
58
light.set_xyz(0.0, 0.2, 1.0)
60
camera = soya3d.Camera(scene)
61
camera.set_xyz(0.0, -1.0, 3.0)
63
soya.set_root_widget(widget.Group())
64
soya.root_widget.add(camera)
65
soya.root_widget.add(widget.FPSLabel())
67
def rotate_advance_time(obj, angle):
68
return lambda proportion: obj.rotate_incline(0.2 * proportion * angle)
70
volume_1.advance_time = rotate_advance_time(volume_1, 5.0)
71
volume_2.advance_time = rotate_advance_time(volume_2, -5.0)
72
volume_3.advance_time = rotate_advance_time(volume_3, 5.0)
75
# Creates a red laser, which reflect on walls.
78
laser = soya.laser.Laser(scene, reflect = 1)
82
# Hide the mouse cursor
84
soya.cursor_set_visible(0)
88
# Processes the events
90
for event in soya.process_event():
91
if event[0] == soya.MOUSEMOTION:
93
# For mouse motion event, rotate the laser (quite) toward the mouse.
94
# The formulas are empirical; see soya.cursor for a better algorithm
95
# if you want to translate mouse positions into 3D coordinates.
99
(float(event[1]) / camera.get_screen_width () - 0.5) * 4.0,
100
(float(event[2]) / camera.get_screen_height() - 0.5) * -4.0,
105
laser.begin_round = begin_round
109
idler.Idler(scene).idle()
112
# TODO (left as an exercice):
113
# Turn this tutorial lesson into a full game, where the player must shoot
114
# with the laser a specific target (or monsters).
115
# The levels includes different moving and rotating obstacles.
116
# A multiplayer version uses two laser, one for each player, and two targets.
119
# the laser.points list contains all the points that define the laser trajectory.
120
# use laser.color to change the color of the laser.