Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

pseudostadt.py

Kamera folgt Objekt: Weitwinkel-Spaziergang durch "künstliche Stadt": mit der Maus (+ Tastatur) kann man sich bewegen und im zweiten Fenster gleichzeitig von oben betrachten...

pseudostadt.py — text/python-source, 3 KB (4067 bytes)

Dateiinhalt

# -*- coding: utf-8 -*-
# Teststadt für Rundgang
# 2010-03-08

from visual import *
from visual.text import *
from random import *


scene.title = "ARTIFICIAL CITY" # Fenstertitel
scene.x = 0	                    # Fensterpos v. links
scene.y = 0	                    # Fensterpos v. oben
scene.height = 320              # Fensterhöhe
scene.width = 700               # Fensterbreite
scene.range = (1,1,1)           # Abstand
scene.center = (0,5,50)         # Festlegung Blickziel
scene.userspin = False          # Blickwechsel abgeschaltet
scene.userzoom = False          # Benutzerzoom abgeschaltet
scene.fov = 2                   # Kamerawinkel
scene.background = (0.8,0.85,1) # Farbe Hintergrund
scene.stereodepth = 2           # Stereotiefe
scene.show_rendertime = True    # Anzeige der Berechnungszeiten
scene.fullscreen = False         # Vollbildmodus
haus_anzahl = 30                # Konstante: Anzahl der Häuser

# graue Bodenplatte
floor = box(pos=(0,0,0), width=120, height=1, length=120, color=(0.4,0.4,0.4))
for i in range(haus_anzahl): # Häuser durch Zufall generieren
    zufall = random()
    box(pos=(random()*100-50, (10+zufall*5)/2,random()*100-50),\
        width=5+random()*5, height=10+zufall*5, length=7+random()*3, \
        color=color.hsv_to_rgb(((float(i)/haus_anzahl),1,1)))
text(pos=(0, 20, -50), string="ARTIFICIAL CITY", color=color.orange, depth=1, height=5, justify="center") #Schriftzug

rhairs = 0.025 # half-length of crosshairs
dhairs = 2 # how far away the crosshairs are
maxcosine = dhairs/sqrt(rhairs**2+dhairs**2) # if ray inside crosshairs, don't move
haircolor = color.black
roam=False

#-Zweitfenster für topview---
zweitfenster = display(width=300,height=320, title="TOP-VIEW",\
                       center=(0,5,0), forward=(0,-5,0), x=700, y=0,\
                       background=(0.8,0.85,1))
zweitfenster.range = 75
zweitfenster.select()
for obj in scene.objects:
    try:
        obj.__copy__(display=zweitfenster).opacity=0.5
    except:
        pass
walker = sphere(radius=2, color=color.white, pos=scene.center, material=materials.emissive)
walker.trail = curve(color=color.yellow)    

while True:
    rate(50) 

    # Toggle roam option
    if scene.mouse.events:
        m = scene.mouse.getevent()
        if m.press or m.drag:
            roam = True
        elif m.release or m.drop:
            roam = False

    # If in roaming mode, change center and forward according to mouse position
    if roam:
        ray = scene.mouse.ray
        if abs(dot(ray,scene.forward)) < maxcosine: # do something only if outside crosshairs
            newray = norm(vector(ray.x, 0, ray.z))
            angle = arcsin(dot(cross(scene.forward,newray),scene.up))
            newforward = rotate(scene.forward, axis=scene.up, angle=angle/50)
            scene.center = scene.mouse.camera+newforward*mag(scene.center-scene.mouse.camera)
            scene.forward = newforward
            scene.center = scene.center+scene.forward*ray.y/2.
            zweitfenster.select()
            walker.pos = scene.center
            walker.trail.append(pos=walker.pos, retain=5000)

    # Falls Tastatur gedrückt...
    if scene.kb.keys:
        s = scene.kb.getkey()
        print s
        if   s == 'up':     # vorwärts
            scene.center = scene.center+scene.forward*mag(scene.center-scene.mouse.camera)
        elif s == 'down':   # rückwärts
            scene.center = scene.center-scene.forward*mag(scene.center-scene.mouse.camera)
        elif s == 'ctrl+up':   # links drehen
            newforward = rotate(scene.forward, axis=scene.up, angle=radians(2))
            scene.center = scene.mouse.camera+newforward*mag(scene.center-scene.mouse.camera)
            scene.forward = newforward
            #scene.center = scene.center+scene.forward*ray.y/2.
        elif s == 'alt+up':  # rechts drehen
            newforward = rotate(scene.forward, axis=scene.up, angle=radians(-2))
            scene.center = scene.mouse.camera+newforward*mag(scene.center-scene.mouse.camera)
            scene.forward = newforward


Funktionsleiste