创建2D台球游戏,球速问题

时间:2019-08-16 12:54:22

标签: python game-physics physics

我正在尝试为台球游戏创建一个简单的程序,其中两个半径(R)的球(a)和(b)发生碰撞。我创建了一个python程序,它是这样的。

[   +1 ms] FAILURE: Build failed with an exception.
[   +1 ms] * Where:
[        ] Script '/Users/apple/flutter/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 379
[        ] * What went wrong:
[        ] A problem occurred configuring root project 'android_generated'.
[        ] > A problem occurred configuring project ':flutter'.
[        ]    > Could not find method execute() for arguments [] on task ':flutter:buildPluginReleaseDeviceInfo' of type
FlutterPluginTask.
[        ] * Try:
[        ] Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan
to get full insights.
[        ] * Get more help at https://help.gradle.org
[        ] BUILD FAILED in 587ms
[ +415 ms] Running Gradle task 'assembleAarRelease'... (completed in 1.1s)
[   +5 ms] "flutter aar" took 5,757ms.
Gradle task assembleAarRelease failed with exit code 1

当我为简单的碰撞绘制图片时,数据文件将包含该图片(输入数据)

pages[0].on('response', async response => { if (response.request() /*Your condition check*/) { var buffer = await response.buffer(); /*You can get the buffer*/ var content = await response.text(); /*You can get the content as text*/ } }); 值,

我得到类似

enter image description here

我用这个程序来画画

from math import sqrt, atan2, sin, cos, pi, inf
from numpy import array

W = 600  # width of the table
H = 300  # height of the table
R = 10  # the radius of the ball
A = 0  # deceleration constant
dt = 10 ** -3
ma = mb = 1  # masses of the particles a and b


def vec_magnitude(V1):
    return sqrt(V1[0]**2 + V1[1]**2)


def collision_test(V1, V2):
    if vec_magnitude(V1 - V2) <= 2 * R:
        return True


def dot_product(V1, V2):
    return sum(V1 * V2)


def after_collision_velocity(Va, Vb, Ra, Rb):
    ''' the equation that produces the velocity of the objects after the collision'''
    Va_new = Va - ((2 * mb * dot_product(Va - Vb, Ra - Rb)) /
                ((ma + mb) * (vec_magnitude(Ra - Rb))**2) * (Ra - Rb))
    Vb_new = Vb - ((2 * ma * dot_product(Vb - Va, Rb - Ra)) /
                ((ma + mb) * (vec_magnitude(Rb - Ra))**2) * (Rb - Ra))
    return Va_new, Vb_new


def motion(P, V_mag, angle, V):
    '''describes the motion of the ball'''
    if P[1] < R: #reflection from top 
        P += array([0, 2 * (R - P[1])])
        angle *= -1 #reflection from the angular perspective
        return P, V_mag, angle, V 
    if P[0] < R: # reflection from left
        P += array([2 * (R - P[0]), 0]) 
        angle = pi - angle
        return P, V_mag, angle, V
    if P[1] > H - R: #reflection from bottom
        P += array([0, 2 * (H - R - P[1])])
        angle *= -1
        return P, V_mag, angle, V
    if P[0] > W - R:  #reflection from right
        P += array([2 * (W - R - P[0]), 0])
        angle = pi - angle
        return P, V_mag, angle, V
    else:
        V_mag -= A * dt
        Vx = V_mag * cos(angle)
        Vy = V_mag * sin(angle)
        P += array([Vx * dt, Vy * dt])
        V = array([Vx, Vy])
        return P, V_mag, angle, V


file = open("test_drawing.txt", "w")
for line in open("tex.txt", "r"):
    t = 0 # starting time
    Xa, Ya, Xb, Yb, Vxa, Vya, Vxb, Vyb = [
        int(i) for i in (line.rstrip()).split(" ")]
    Pa = array([Xa, Ya], dtype=float) #position vector of the ball a
    Pb = array([Xb, Yb], dtype=float) #position vector of the ball b
    Va = array([Vxa, Vya], dtype=float) #velocity vvector of the ball a
    Vb = array([Vxb, Vyb], dtype=float) #velocity vector of the ball b
    Va_mag = vec_magnitude(Va)
    Vb_mag = vec_magnitude(Vb)
    if Vxa == 0: #these steps are necessarry to eliminate error on the angle process
        Vxa = inf
    angle_a = atan2(Vya, Vxa) # angle between velocity components of the ball a
    if Vxb == 0:
        Vxb = inf
    angle_b = atan2(Vyb, Vxb) # angle between velocity components of the ball b
    while t <= 10:
        Pa, Va_mag, angle_a, Va = motion(Pa, Va_mag, angle_a, Va) #moving the ball a
        Pb, Vb_mag, angle_b, Vb = motion(Pb, Vb_mag, angle_b, Vb) #moving the ball b
        if collision_test(Pa, Pb) == True:  #checking the collision validity
            Va, Vb = after_collision_velocity(Va, Vb, Pa, Pb)
            Va_mag = vec_magnitude(Va) #restating the velocities
            Vb_mag = vec_magnitude(Vb)
            if Va[0] == 0:
                Va[0] = inf
            angla_a = atan2(Va[1], Va[0]) #restating the angles
            if Vb[0] == 0:
                Vb[0] = inf
            angle_b = atan2(Vb[1], Vb[0])
        t += dt #incrementing time
        file.write(str(Pa[0]) + " " + str(Pa[1]) + " " + str(Pb[0]) + " " + str(Pb[1]) + "\n")
    print(Pa[0], Pa[1], Pb[0], Pb[1])
file.close()

如您所见,球(b)会弹跳,但球(a)不会弹跳。为了确定速度,我使用了弹性碰撞的维基百科页面中的方程式。

https://en.wikipedia.org/wiki/Elastic_collision

谁能理解为什么会这样?

1 个答案:

答案 0 :(得分:2)

您有错字。

angla_a = atan2(Va[1], Va[0]) #restating the angles

应该说angle_a。发生碰撞后,您实际上从未更新过angle_a