Donnerstag, 24. Dezember 2009

Python 042 Ball springt durchs Gelände



from visual import *
from math import *
import numpy as np
import time

scene.center=(10,0,5)

l = 2.5
#coordinate system
curve(pos=[(-l/2,0,0),(2*l,0,0)],radius=0.05,color=color.green)
curve(pos=[(0,-l/2,0),(0,2*l,0)],radius=0.05,color=color.blue)
curve(pos=[(0,0,-l/2),(0,0,2*l)],radius=0.05,color=color.red)

xwidth=40
zwidth=40
ebene = []
normal = []

#ground surface
for x in range(xwidth):
ebene.append([])
normal.append([])
for z in range(zwidth):
ebene[x].append(sin(x/4.0)+sin(z/4.0)) #FUNCTION
normal[x].append(0)

#draw ground surface
for x in range(xwidth-1):
for z in range(zwidth-1):
curve(pos=[(x,ebene[x][z],z),(x+1,ebene[x+1][z],z)],radius=.1,color=color.green)
curve(pos=[(x,ebene[x][z],z),(x,ebene[x][z+1],z+1)],radius=.1,color=color.green)

#ball
ball = sphere(pos=(8,4,11),radius=.3)
vektor = [-0,-0.02,-0]
ball.trail = curve(color=ball.color, radius=.1)

#calculate surface normal
def surfacenormal(pointx,pointz):
pointy = sin(pointx/4.0)+sin(pointz/4.0) #FUNCTION
start = (pointx,pointy,pointz)
#derivative and cross product of fw and fu
fu = np.array([1,0.25*cos(pointx/4.0),0]) #FUNCTION
fw = np.array([0,0.25*cos(pointz/4.0),1]) #FUNCTION
fv = np.cross(fw,fu)
end = (fv[0]+pointx,fv[1]+pointy,fv[2]+pointz)
normal = curve(pos=[start, end])
return normal.pos

t=0
veca = [0,0,0]
vecc = [0,0,0]
#movement
while 1:
t += 1
ball.pos = ball.pos + vektor
ball.trail.append(pos=ball.pos)
if (ball.pos.y <= (sin(int(ball.pos.x/4.0))+sin(int(ball.pos.z/4.0)))): #FUNCTION
veca[0] = -vektor[0] #incoming vector
veca[1] = -vektor[1]
veca[2] = -vektor[2]
vecb = surfacenormal(int(ball.pos.x),int(ball.pos.z)) #surface normal
vecc[0] = vecb[1][0]-vecb[0][0]
vecc[1] = vecb[1][1]-vecb[0][1]
vecc[2] = vecb[1][2]-vecb[0][2]
vecref = 2*norm(vecc)-norm(veca)
vektor = vecref * mag(veca)

vektor[1]=(vektor[1]-0.0001)*0.999 #Daempfung (vereinfacht)
time.sleep(.001)

Keine Kommentare:

 
eXTReMe Tracker