Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / raypicking-3.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
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
19
# raypicking-3: Laser on Cal3D animated object
20
21
# This lesson shows that raypicking (e.g. laser) also works for Cal3D animated models.
22
23
24
import sys, os, os.path, soya, soya.cube, soya.laser, soya.sdlconst
25
26
soya.init()
27
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
28
29
# Creates the scene.
30
31
scene = soya.World()
32
33
# Adds an animated sorcerer (see character-animation-1.py for animated characters).
34
35
sorcerer_shape = soya.Cal3dShape.get("balazar")
36
37
sorcerer = soya.Cal3dVolume(scene, sorcerer_shape)
38
sorcerer.rotate_lateral(-120.0)
39
sorcerer.animate_blend_cycle("marche")
40
sorcerer.set_xyz(-1.0, -1.0, 0.0)
41
sorcerer.solid = 1
42
43
#sorcerer.scale(0.5, 0.5, 0.5)
44
#sorcerer.scale(2.0, 2.0, 2.0)
45
46
# Adds a light.
47
48
light = soya.Light(scene)
49
light.set_xyz(0.0, 0.2, 1.0)
50
51
# Creates a camera.
52
53
camera = soya.Camera(scene)
54
camera.set_xyz(0.0, 0.0, 3.0)
55
56
soya.set_root_widget(camera)
57
58
# Hide the mouse cursor
59
60
soya.cursor_set_visible(0)
61
62
63
# MouseLaser is a subclass of laser that is controlled by the mouse.
64
65
class MouseLaser(soya.laser.Laser):
66
def begin_round(self):
67
soya.laser.Laser.begin_round(self)
68
69
# Processes the events
70
71
for event in soya.process_event():
72
if event[0] == soya.sdlconst.MOUSEMOTION:
73
74
# For mouse motion event, rotate the laser (quite) toward the mouse.
75
# The formulas are empirical; see soya.cursor for a better algorithm
76
# if you want to translate mouse positions into 3D coordinates.
77
78
mouse = soya.Point(
79
scene,
80
(float(event[1]) / camera.get_screen_width () - 0.5) * 4.0,
81
(float(event[2]) / camera.get_screen_height() - 0.5) * -4.0,
82
0.0,
83
)
84
self.look_at(mouse)
85
86
87
# Creates a red mouse-controlled laser, which reflect on walls.
88
# You can change the laser color with laser.color = (r, g, b, a).
89
90
laser = MouseLaser(scene, reflect = 1)
91
laser.x = 2.0
92
93
94
# Main loop
95
96
soya.Idler(scene).idle()
97