summaryrefslogtreecommitdiffstats
path: root/src/imports/particles/speedlimitaffector.cpp
blob: 16ea200ffb444ede0e9c394907f2435fe0d9e071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "speedlimitaffector.h"
#include <cmath>
#include <QDebug>
QT_BEGIN_NAMESPACE
SpeedLimitAffector::SpeedLimitAffector(QSGItem *parent) :
    ParticleAffector(parent), m_speedLimit(-1)
{
}

bool SpeedLimitAffector::affectParticle(ParticleData *d, qreal dt){
    Q_UNUSED(dt);
    if(m_speedLimit <= 0)
        return false;

    qreal x = d->curSX();
    qreal y = d->curSY();
    qreal s = sqrt(x*x + y*y);
    if(s <= m_speedLimit)
        return false;


    if(s >= m_speedLimit*1.01){
        qreal theta = atan2(y,x);
        d->setInstantaneousSX(m_speedLimit * cos(theta));
        d->setInstantaneousSY(m_speedLimit * sin(theta));
    }

    d->setInstantaneousAY(0);
    d->setInstantaneousAX(0);

    return true;
}
QT_END_NAMESPACE