未知编程新手

2024-10-01 07:18:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我基本上完全了解编程,做了大约3天,我变得相当感兴趣相当快-作为一个练习,以帮助我学习,我已经选择了火的考验,只是直接进去,并使用vPython尝试创建一个斯诺克模拟。你知道吗

到目前为止一切正常,我已经有了所有的模型,为表,靠垫,口袋下巴等,我已经设法使球反弹的靠垫和下巴在一个现实的方式。我也有球以与速度成比例的速度减速-我知道这些都很简单,但我是新来的,所以这一切都非常令人兴奋。你知道吗

不管怎样-问题来了。我正在创建一个函数,其中两个球是输入,它首先检测它们是否接触,然后使它们改变方向(相互撞击)。到目前为止,我有:

def ballhit(ball1,ball2):

    v1 = ball1.velocity
    v2 = ball2.velocity
    btb = ball1.pos - ball2.pos # Ball To Ball
    nbtb = btb/abs(btb) # Normalised btb
    if abs(ball1.velocity) == 0:
        theta1 = 0
    else:
        theta1 = acos(abs(ball1.velocity.x)/abs(ball1.velocity))
    if abs(ball2.velocity) == 0:
        theta2 = 0
    else:
        theta2 = acos(abs(ball2.velocity.x)/abs(ball2.velocity))
    phi = acos(abs(vector(1,0,0))/abs(nbtb))

    ball1.velocity.x = ((2*bm*ball2.velocity*cos(theta2-phi)*cos(phi))/2*bm) + ball1.velocity*(sin(theta1-phi))*(cos(phi+(pi/2)))

    ball1.velocity.y = ((2*bm*ball2.velocity*cos(theta2-phi)*sin(phi))/2*bm) + ball1.velocity*(sin(theta1-phi))*(sin(phi+(pi/2)))

    ball2.velocity.x = ((2*bm*ball1.velocity*cos(theta1-phi)*cos(phi))/2*bm) + ball2.velocity*(sin(theta2-phi))*(cos(phi+(pi/2)))

    ball2.velocity.y = ((2*bm*ball1.velocity*cos(theta1-phi)*sin(phi))/2*bm) + ball2.velocity*(sin(theta2-phi))*(sin(phi+(pi/2)))

    return ball1.velocity
    return ball2.velocity

两个θ值是球与x轴的夹角,phi值是接触点的法线与x轴的夹角,然后我计算每个球的每个分量。据我所知,计算是正确的,但我写的方式肯定是绝对可怕的(如我所料)。你知道吗

这是我得到的错误:

line 320, in <module>
ballhit(red1,blue)
line 302, in ballhit
ball1.velocity.x = ((2*bm*ball2.velocity*cos(theta2-phi)*cos(phi))/2*bm) +
ball1.velocity*(sin(theta1-phi))*(cos(phi+(pi/2))) ArgumentError:
Python argument types in
None.None(vector, vector) did not match C++ signature:
None(class cvisual::vector {lvalue}, double)

我已经定义了两个球的速度,从我所看到的来看,没有其他未赋值的变量。你知道吗

如果有人能向我解释一下这个错误的真正含义,我会很感激的,如果有人能告诉我它是如何修正的,那将是双点。你知道吗

谢谢:)


Tags: piabssincos速度bmvectorphi