PSI_bounce_3.py
PSI_bounce_3.py — text/python-source, 2 KB (2324 bytes)
Dateiinhalt
# -*- coding: utf-8 -*-
# veränderte Version von bounce.py (/examples)
# Fallbeschleunigung
from visual import *
from visual.controls import *
# KONSTANTEN - diese WERTE zum Test ändern!
ERDBESCHLEUNIGUNG = 9.81
REBOUND = 0.9
# grüne Bodenplatte
floor = box(pos=(0,-5,0),length=4, height=0.5, width=4, color=color.green)
# Ball
ball = sphere(pos=(0,5,0))
# Geschwindigkeitsvektor (testweise x-/z-Anteil variieren...!)
ball.velocity = vector(0,-1,0)
# Beschriftungen für ERDBESCHLEUNIGUNG und REBOUND
label(pos=ball.pos, text="g=%s"%ERDBESCHLEUNIGUNG, xoffset=50,yoffset=0, height=10, border=6, font='sans')
label(pos=floor.pos, text="r=%s"%REBOUND , xoffset=50,yoffset=0, height=10, border=6, font='sans')
scene.autoscale = 1 # Skalieren ausgeschaltet
pick=None # ein Element mit der Maus ge"pick"ed?
dt = 0.015 # Delta t - Zeitinkrement
while True:
rate(150) # max. 150 Berechnungen pro Sekunde
if scene.mouse.events:
m1 = scene.mouse.getevent() # Mausevent einfangen
if m1.pick == ball and m1.drag: # ist Maus auf Ball und wird gezogen?
drag_pos = m1.pickpos # Zugposition übernommen
pick = m1.pick # pick aktiviert
elif m1.drop: # falls Maus wieder losgelassen...
ball.color=color.white # alte Ballfarbe reaktiviert
pick = None # pick deaktiviert
if pick: # falls pick aktiviert...
# Projektion auf bestehende xy-Ebene, auch wenn rotiert
new_pos = scene.mouse.project(normal=(0,0,1))
ball.color=color.yellow # färbe Ball gelb
if new_pos != drag_pos: # falls Position verändert...
# offset for where the ball was clicked:
pick.pos += new_pos - drag_pos # übernehme neue Position
drag_pos = new_pos # aktualisiere
else:
# Ball fällt...
ball.pos = ball.pos + ball.velocity*dt
if ball.y < -3.80 and ball.velocity.y < 0.1: # falls
ball.velocity.y = -REBOUND*ball.velocity.y
else:
ball.velocity.y = ball.velocity.y - ERDBESCHLEUNIGUNG*dt #