Python Bungee Cord Simulation

时间:2015-10-27 23:38:34

标签: python

我正在尝试使用视觉来模拟蹦极跳线(我试图在这里寻找一个非常相似的答案,但它没有提供答案)。我的代码是

def animateBungeeJump(mass, deltaT, simulationTime, surfaceArea, unstretchedBungeeLength):
g = 9.8
k = 21.7
vel = 0.0
accel = 0.0
distance = unstretchedBungeeLength
time_start = 0.0
d = 0.0

while(time_start <= simulationTime):
    Fg = mass * g
    Ff = (-0.65) * surfaceArea * vel * abs(vel)
    Fs = (-1) * k * d
    Ft = Fg + Ff + Fs

    accel = Ft / mass
    vel = accel * time_start
    d += vel * time_start

    print "Accel", accel
    print "D:", d
    print "Vel:", vel
    print ""
    time_start += deltaT
animateBungeeJump(60, 0.1, 5.0, 0.2, 30)

我想在调试时看到的结果是看到加速度反弹和一般距离(d)应该如何反弹和反弹,但在运行代码时,如果我使用10s的simulationTime,它会转到-inf和inf或者更多。我不知道我是否正确地完成了所有的数学运算。对不起,如果我的代码看起来很愚蠢,我就是在学习。

结束时的结果输出大约5秒:

Accel -4.29956602269e+105 D: -1.07489150567e+107 Vel: -2.14978301134e+106 我非常确定速度不应该低于

1 个答案:

答案 0 :(得分:1)

您的增量更改未正确完成。速度是自上次以来的变化;速度和距离都应根据时间跨度而不是整个运行时间进行更新。试试这些:

public void associateDissociateLodgerAndContact(Long lodgerId, List<Long> contactIdToDissociates, List<Long> contactIdToAssociates) {
    Lodger lodger = lodgerRepository.findOne(lodgerId);

    List<Contact> contactList;

    if (lodger.getContactList() == null) {
        lodger.setContactList(new ArrayList<>());
        contactList = lodger.getContactList();
    } else {
        contactList = lodger.getContactList();
    }

    if (contactIdToDissociates != null) {

        for (Iterator<Contact> iterator = contactList.iterator(); iterator.hasNext();) {
            Contact contact = iterator.next();

            for (Long contactId : contactIdToDissociates) {
                if (contact.getContactId().longValue() == contactId.longValue()) {
                    iterator.remove();
                    break;
                }
            }

        }
    }

    List<Contact> contactToAssign = contactRepository.findAll(contactIdToAssociates);

    lodger.getContactList().addAll(contactToAssign);
    lodgerRepository.save(lodger);
}