Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / traveling-camera-1.pyOpen in CoCalc
Author: William A. Stein
1
# Soya 3D tutorial
2
# Copyright (C) 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
# basic-5: Event management : a keyboard-controlled caterpillar
20
21
# In this lesson, our caterpillar will obey you !
22
# You'll learn how to use SDL events with Soya.
23
# Use the cursor arrows to control the caterpillar.
24
25
26
# Import the Soya module.
27
# The soya.sdlconst module contains all the SDL constants.
28
29
import sys, os, os.path, soya, soya.sdlconst
30
31
soya.init()
32
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
33
34
# Creates a scene.
35
36
scene = soya.World()
37
38
39
# The CaterpillarHead class is very similar to the CaterpillarHead class of the previous
40
# lesson.
41
42
class CaterpillarHead(soya.Volume):
43
def __init__(self, parent):
44
soya.Volume.__init__(self, parent, soya.Shape.get("caterpillar_head"))
45
self.speed = soya.Vector(self, 0.0, 0.0, 0.0)
46
self.rotation_lateral_speed = 0.0
47
self.solid = 0
48
49
def begin_round(self):
50
soya.Volume.begin_round(self)
51
52
for event in soya.process_event():
53
if event[0] == soya.sdlconst.KEYDOWN:
54
if event[1] == soya.sdlconst.K_UP: self.speed.z = -0.2
55
elif event[1] == soya.sdlconst.K_DOWN: self.speed.z = 0.1
56
elif event[1] == soya.sdlconst.K_LEFT: self.rotation_lateral_speed = 10.0
57
elif event[1] == soya.sdlconst.K_RIGHT: self.rotation_lateral_speed = -10.0
58
elif event[1] == soya.sdlconst.K_q: soya.IDLER.stop()
59
elif event[1] == soya.sdlconst.K_ESCAPE: soya.IDLER.stop()
60
61
if event[0] == soya.sdlconst.KEYUP:
62
if event[1] == soya.sdlconst.K_UP: self.speed.z = 0.0
63
elif event[1] == soya.sdlconst.K_DOWN: self.speed.z = 0.0
64
elif event[1] == soya.sdlconst.K_LEFT: self.rotation_lateral_speed = 0.0
65
elif event[1] == soya.sdlconst.K_RIGHT: self.rotation_lateral_speed = 0.0
66
67
self.rotate_lateral(self.rotation_lateral_speed)
68
69
def advance_time(self, proportion):
70
soya.Volume.advance_time(self, proportion)
71
self.add_mul_vector(proportion, self.speed)
72
73
74
class CaterpillarPiece(soya.Volume):
75
def __init__(self, parent, previous):
76
soya.Volume.__init__(self, parent, soya.Shape.get("caterpillar"))
77
self.previous = previous
78
self.speed = soya.Vector(self, 0.0, 0.0, -0.2)
79
self.solid = 0
80
81
def begin_round(self):
82
soya.Volume.begin_round(self)
83
self.look_at(self.previous)
84
if self.distance_to(self.previous) < 1.5: self.speed.z = 0.0
85
else: self.speed.z = -0.2
86
87
def advance_time(self, proportion):
88
soya.Volume.advance_time(self, proportion)
89
self.add_mul_vector(proportion, self.speed)
90
91
92
caterpillar_head = CaterpillarHead(scene)
93
caterpillar_head.rotate_lateral(90.0)
94
95
previous_caterpillar_piece = caterpillar_head
96
for i in range(10):
97
previous_caterpillar_piece = CaterpillarPiece(scene, previous_caterpillar_piece)
98
previous_caterpillar_piece.x = i + 1
99
100
# Creates a light.
101
102
light = soya.Light(scene)
103
light.set_xyz(2.0, 5.0, 0.0)
104
105
# Creates a camera.
106
107
camera = soya.TravelingCamera(scene)
108
109
traveling = soya.ThirdPersonTraveling(caterpillar_head)
110
#traveling = soya.ThirdPersonTraveling(soya.Point(caterpillar_head, 0.0, 1.0, 0.0))
111
traveling.distance = 15.0
112
#traveling.smooth_move = 1
113
traveling.smooth_rotation = 0
114
#traveling.direction = soya.Vector(camera, 0.0, 1.0, 2.0)
115
#traveling.incline_as = None
116
117
camera.add_traveling(traveling)
118
camera.speed = 0.3
119
camera.set_xyz(0.0, 15.0, 15.0)
120
camera.look_at(caterpillar_head)
121
soya.set_root_widget(camera)
122
123
soya.Idler(scene).idle()
124