Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / raypicking-1.pyOpen in CoCalc
Author: William A. Stein
1# Soya 3D tutorial
2# Copyright (C) 2001-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# raypicking-1: Laser
20
21# This lesson includes a red laser ray and 3 rotating cubes.
22# Move the mouse to move the laser.
23# In particular, lasers have been used while testing raypicking function.
24
25# BTW, the laser code (in soya/laser.py) is fully in Python and uses less that 60 lines
26# of code, so you might want to take a look at it.
27
28
29import sys, os, os.path, soya, soya.cube, soya.laser, soya.sdlconst
30
31soya.init()
32soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
33
34# Creates the scene.
35
36scene = soya.World()
37
38# Adds 3 rotating cubes in it (see basic-2.py about rotating volume).
39
40cube_world = soya.cube.Cube()
41cube_shape = cube_world.shapify()
42
43class RotatingCube(soya.Volume):
44  def __init__(self, parent, angle_speed):
45    soya.Volume.__init__(self, parent, cube_shape)
46    self.angle_speed = angle_speed
47
49    self.rotate_incline(0.2 * proportion * self.angle_speed)
50
51cube_1 = RotatingCube(scene, 5.0)
52cube_1.set_xyz(-1.1, 0.5, 0.0)
53
54cube_2 = RotatingCube(scene, -5.0)
55cube_2.set_xyz(-1.5, -1.5, 0.0)
56
57cube_3 = RotatingCube(scene, 5.0)
58cube_3.set_xyz(1.1, -0.5, 0.0)
59cube_3.scale(0.5, 0.5, 0.5)
60
62
63light = soya.Light(scene)
64light.set_xyz(0.0, 0.2, 1.0)
65
66# Creates a camera.
67
68camera = soya.Camera(scene)
69camera.set_xyz(0.0, -1.0, 3.0)
70
71soya.set_root_widget(camera)
72
73# Hide the mouse cursor
74
75soya.cursor_set_visible(0)
76
77
78# MouseLaser is a subclass of laser that is controlled by the mouse.
79
80class MouseLaser(soya.laser.Laser):
81  def begin_round(self):
82    soya.laser.Laser.begin_round(self)
83
84    # Processes the events
85
86    for event in soya.process_event():
87      if event[0] == soya.sdlconst.MOUSEMOTION:
88
89        # For mouse motion event, rotate the laser (quite) toward the mouse.
90        # The formulas are empirical; see soya.cursor for a better algorithm
91        # if you want to translate mouse positions into 3D coordinates.
92
93        mouse = soya.Point(
94          scene,
95          (float(event[1]) / camera.get_screen_width () - 0.5) *  4.0,
96          (float(event[2]) / camera.get_screen_height() - 0.5) * -4.0,
97          0.0,
98          )
99        self.look_at(mouse)
100
101
102# Creates a red mouse-controlled laser, which reflect on walls.
103# You can change the laser color with laser.color = (r, g, b, a).
104
105laser = MouseLaser(scene, reflect = 1)
106laser.y = -2.5
107
108scene.x = 1.0
109
110# Main loop
111
112soya.Idler(scene).idle()
113
114
115# TODO (left as an exercice):
116# Turn this tutorial lesson into a full game, where the player must shoot
117# with the laser a specific target (or monsters).
118# The levels includes different moving and rotating obstacles.
119# A multiplayer version uses two laser, one for each player, and two targets.
120
121# Hint :
122# the laser.points list contains all the points that define the laser trajectory.
123# use laser.color to change the color of the laser.
124
125# Good luck!
126