1 /*
2  *  Copyright 2015  Andreas Cord-Landwehr <cordlandwehr@kde.org>
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Lesser General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2.1 of the License, or (at your option) version 3, or any
8  *  later version accepted by the membership of KDE e.V. (or its
9  *  successor approved by the membership of KDE e.V.), which shall
10  *  act as a proxy defined in Section 6 of version 3 of the license.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "test_forces.h"
22 #include "gravitation.h"
23 #include "rigidbody.h"
24 #include "particle.h"
25 #include "vector.h"
26 #include <QDebug>
27 
28 using namespace StepCore;
29 
testGravitationalForce()30 void TestForces::testGravitationalForce()
31 {
32     // setup world
33     World fakeWorld;
34 
35     RigidBody *body = new RigidBody;
36     body->setMass(10);
37     fakeWorld.addItem(body);
38 
39     Particle *particleA = new Particle;
40     Particle *particleB = new Particle;
41     particleA->setMass(10);
42     particleB->setMass(10);
43     particleA->setForce(Vector2d::Zero());
44     particleB->setForce(Vector2d(0,1));
45     particleA->setPosition(Vector2d(0,0));
46     particleB->setPosition(Vector2d(0,1));
47     fakeWorld.addItem(particleA);
48     fakeWorld.addItem(particleB);
49 
50     // test gravitational force
51     GravitationForce force(9.8); // use this constant to make verification easier
52     force.setWorld(&fakeWorld);
53     force.calcForce(true); // test variance errors in same run
54 
55     // only affects particles
56     QCOMPARE(double(body->force()[0]), 0.0);
57     QCOMPARE(double(body->force()[1]), 0.0);
58 
59     QCOMPARE(double(particleA->force()[0]), 0.0);
60     QCOMPARE(double(particleA->force()[1]), 980.0);
61     QCOMPARE(double(particleB->force()[0]), 0.0);
62     QCOMPARE(double(particleB->force()[1]), -979.0); // note the force
63 
64     QCOMPARE(double(particleA->particleErrors()->positionVariance()[0]), 0.0);
65     QCOMPARE(double(particleA->particleErrors()->positionVariance()[1]), 0.0);
66 
67     QVERIFY(force.world());
68 }
69 
testWeightForce()70 void TestForces::testWeightForce()
71 {
72     // setup world
73     World fakeWorld;
74 
75     Particle *particle = new Particle;
76     particle->setMass(10);
77     fakeWorld.addItem(particle);
78 
79     RigidBody *body = new RigidBody;
80     body->setMass(10);
81     body->setPosition(Vector2d::Zero());
82     fakeWorld.addItem(body);
83 
84     WeightForce force(9.8); // use this constant to make verification easier
85     force.setWorld(&fakeWorld);
86     force.calcForce(true); // test variance errors in same run
87 
88     QCOMPARE(double(particle->force()[0]), 0.0);
89     QCOMPARE(double(particle->force()[1]), -98.0);
90     QCOMPARE(double(particle->particleErrors()->accelerationVariance()[0]), 0.0);
91     QVERIFY(double(particle->particleErrors()->accelerationVariance()[1]) < 1e-9);
92 
93     QCOMPARE(double(body->force()[0]), 0.0);
94     QCOMPARE(double(body->force()[1]), -98.0);
95     QCOMPARE(double(body->position()[0]), 0.0);
96     QCOMPARE(double(body->position()[1]), 0.0);
97     QCOMPARE(double(body->torque()), 0.0);
98     QCOMPARE(double(body->rigidBodyErrors()->accelerationVariance()[0]), 0.0);
99     QVERIFY(double(body->rigidBodyErrors()->accelerationVariance()[1]) < 1e-9);
100 }
101 
102 QTEST_MAIN(TestForces)
103