~jtaylor/ubuntu/oneiric/soya/fix-780305

« back to all changes in this revision

Viewing changes to tutorial/lesson-105.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Dequènes (Duck)
  • Date: 2005-01-30 09:55:06 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050130095506-f21p6v6cgaobhn5j
Tags: 0.9.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Soya 3D tutorial
2
 
# Copyright (C) 2001-2002 Jean-Baptiste LAMY
3
 
#
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.
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
 
# Lesson 105: Laser
19
 
 
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.
23
 
 
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
26
 
 
27
 
# This is the same as lesson 105 (3 rotating cubes)
28
 
 
29
 
soya.init()
30
 
 
31
 
scene = soya3d.World()
32
 
 
33
 
import os, os.path, sys
34
 
data_dir = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "data")
35
 
 
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")
40
 
 
41
 
material = model.Material()
42
 
material.tex_filename  = "block2.tga"
43
 
 
44
 
cube = cube.Cube(None, material)
45
 
 
46
 
shape = cube.shapify()
47
 
 
48
 
volume_1 = soya3d.Volume(scene, shape)
49
 
volume_1.set_xyz(-1.1, 0.5, 0.0)
50
 
 
51
 
volume_2 = soya3d.Volume(scene, shape)
52
 
volume_2.set_xyz(-1.5, -1.5, 0.0)
53
 
 
54
 
volume_3 = soya3d.Volume(scene, shape)
55
 
volume_3.set_xyz(1.1, -0.5, 0.0)
56
 
 
57
 
light = soya3d.Light(scene)
58
 
light.set_xyz(0.0, 0.2, 1.0)
59
 
 
60
 
camera = soya3d.Camera(scene)
61
 
camera.set_xyz(0.0, -1.0, 3.0)
62
 
 
63
 
soya.set_root_widget(widget.Group())
64
 
soya.root_widget.add(camera)
65
 
soya.root_widget.add(widget.FPSLabel())
66
 
 
67
 
def rotate_advance_time(obj, angle):
68
 
  return lambda proportion: obj.rotate_incline(0.2 * proportion * angle)
69
 
 
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)
73
 
 
74
 
 
75
 
# Creates a red laser, which reflect on walls.
76
 
 
77
 
import soya.laser
78
 
laser = soya.laser.Laser(scene, reflect = 1)
79
 
laser.y = -2.5
80
 
 
81
 
 
82
 
# Hide the mouse cursor
83
 
 
84
 
soya.cursor_set_visible(0)
85
 
 
86
 
 
87
 
def begin_round():
88
 
  # Processes the events
89
 
  
90
 
  for event in soya.process_event():
91
 
    if event[0] == soya.MOUSEMOTION:
92
 
      
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.
96
 
      
97
 
      mouse = Point(
98
 
        scene,
99
 
        (float(event[1]) / camera.get_screen_width () - 0.5) *  4.0,
100
 
        (float(event[2]) / camera.get_screen_height() - 0.5) * -4.0,
101
 
        0.0,
102
 
        )
103
 
      laser.look_at(mouse)
104
 
  
105
 
laser.begin_round = begin_round
106
 
 
107
 
# Main loop
108
 
 
109
 
idler.Idler(scene).idle()
110
 
 
111
 
 
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.
117
 
 
118
 
# Hint :
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.
121
 
 
122
 
# Good luck!