如何解决我在 CodeSculptor3 上制作的这个 3d 相机的旋转有什么问题,CodeSculptor3 是一个基于浏览器的 Python 解释器,带有 GUI?
这是代码的链接,您可以使用该程序: https://py3.codeskulptor.org/#user306_KJotPenLg75Fx0e_6.py
import simplegui
import math
import codeskulptor
width,height=800,550
center=(width/2,height/2)
keys=0
pi=3.1415926535897932384626433832795
#Camera
cam_pos=[0,0]
cam_speed=5
rotate_speed=pi/90#radians
yaw,pitch=0,0
W,R,A,S,D,F,DOWN,UP,LEFT,RIGHT=False,False,False
#The distance between the camera and the canvas.
canvas_distance=400
#input a 3d point,return the projected position on the canvas
def projection(pos):
delta_x=-pos[0]
delta_y=-pos[1]
delta_z=-pos[2]
if delta_z==canvas_distance:return(center[0]-delta_x,center[1]+delta_y)
if 0<delta_z<canvas_distance:return (center[0]-(delta_x/delta_z)*canvas_distance,center[1]+(delta_y/delta_z)*canvas_distance)
if delta_z<=0:return (center[0]-delta_x*10000,center[1]+delta_y*10000)
ratio=canvas_distance/delta_z
return [center[0]-delta_x*ratio,center[1]+delta_y*ratio]
#rotate around the camera
def rotation(pos):
#yaw changes x and z
if LEFT==True or RIGHT==True:
cosine=math.cos(yaw)
sine=math.sin(yaw)
pos[2]=cosine*pos[2]+sine*pos[0]
pos[0]=cosine*pos[0]-sine*pos[2]
#pitch changes y and z
elif UP==True or DOWN==True:
cosine=math.cos(pitch)
sine=math.sin(pitch)
pos[1]=cosine*pos[1]+sine*pos[2]
pos[2]=cosine*pos[2]-sine*pos[1]
return pos
def cam_update():
global yaw,pitch
if W==True:translate(2,cam_speed)
elif S==True:translate(2,-cam_speed)
if A==True:translate(0,cam_speed)
elif D==True:translate(0,-cam_speed)
if R==True:translate(1,-cam_speed)
elif F==True:translate(1,+cam_speed)
if LEFT==True or RIGHT==True or UP==True or DOWN==True:
if LEFT==True:yaw=rotate_speed
elif RIGHT==True:yaw=-rotate_speed
if UP==True:pitch=rotate_speed
elif DOWN==True:pitch=-rotate_speed
rotate()
def keyup(key):
global W,RIGHT,keys
keys-=1
if key==simplegui.KEY_MAP['W']:W=False
elif key==simplegui.KEY_MAP['S']:S=False
elif key==simplegui.KEY_MAP['A']:A=False
elif key==simplegui.KEY_MAP['D']:D=False
elif key==simplegui.KEY_MAP['R']:R=False
elif key==simplegui.KEY_MAP['F']:F=False
elif key==simplegui.KEY_MAP['up']:UP=False
elif key==simplegui.KEY_MAP['down']:DOWN=False
elif key==simplegui.KEY_MAP['left']:LEFT=False
elif key==simplegui.KEY_MAP['right']:RIGHT=False
def keydown(key):
global W,keys
keys+=1
#move forward & backward
if key==simplegui.KEY_MAP['W']:W,S=True,False
elif key==simplegui.KEY_MAP['S']:S,W=True,False
#move left & right
elif key==simplegui.KEY_MAP['A']:A,D=True,False
elif key==simplegui.KEY_MAP['D']:D,A=True,False
#levitate & fall
elif key==simplegui.KEY_MAP['R']:R,F=True,False
elif key==simplegui.KEY_MAP['F']:F,R=True,False
#pitch
elif key==simplegui.KEY_MAP['up']:UP,DOWN=True,False
elif key==simplegui.KEY_MAP['down']:DOWN,UP=True,False
#yaw
elif key==simplegui.KEY_MAP['left']:LEFT,Right=True,False
elif key==simplegui.KEY_MAP['right']:RIGHT,LEFT=True,False
def find_distance(a,b):
if len(a)==2:return math.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)
return math.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2+(a[2]-b[2])**2)
class Point:
def __init__(self,name,color,loc):
self.name=name
self.loc=loc#(x,y,z)
self.color=color
self.pos=projection(self.loc)#canvas position
#Declaring Point objects
x=Point('X','red',[800,-180,-1300])
y=Point('Y','blue',[-200,820,-1300])
z=Point('Z','green',-300])
origin=Point('O','white',-1300])
points=[x,z,origin]
#translate all points
def translate(axis,units):
for point in points:point.loc[axis]+=units
#rotate all points
def rotate():
for point in points:point.loc=rotation(point.loc)
#this function executes 60 times a second
def draw(canvas):
for point in points:
canvas.draw_line(origin.pos,point.pos,3,point.color)
canvas.draw_text(point.name+str([int(x) for x in point.loc]),15,'white')
#if any keys are pressed,update points' locations
if keys>0:
cam_update()
for point in points:
point.pos=projection(point.loc)
canvas.draw_text('distance between Z and O: '+str(find_distance(z.loc,origin.loc)),[15,20],20,'yellow')
frame = simplegui.create_frame("SSS 3D",width,height)
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
label=frame.add_label('Click the canvas first')
label=frame.add_label('To move: WASD RF')
label=frame.add_label('To yaw or pitch: arrow keys')
frame.start()
当我尝试旋转时,两点之间的距离会减小。您可以看到我的示例中的轴不断缩小。看起来轴线之间的角度也越来越尖锐。
解决方法
解决了。精度问题。我只需要减少中间变量并在一次长计算中返回值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。