File indexing completed on 2024-04-21 03:51:11

0001 /*
0002     SPDX-FileCopyrightText: 2015 Andreas Cord-Landwehr <cordlandwehr@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "test_forces.h"
0008 
0009 #include "gravitation.h"
0010 #include "motor.h"
0011 #include "rigidbody.h"
0012 #include "particle.h"
0013 #include "vector.h"
0014 
0015 #include <QDebug>
0016 
0017 using namespace StepCore;
0018 
0019 void TestForces::testGravitationalForce()
0020 {
0021     // setup world
0022     World fakeWorld;
0023 
0024     RigidBody *body = new RigidBody;
0025     body->setMass(10);
0026     fakeWorld.addItem(body);
0027 
0028     Particle *particleA = new Particle;
0029     Particle *particleB = new Particle;
0030     particleA->setMass(10);
0031     particleB->setMass(10);
0032     particleA->setForce(Vector2d::Zero());
0033     particleB->setForce(Vector2d(0,1));
0034     particleA->setPosition(Vector2d(0,0));
0035     particleB->setPosition(Vector2d(0,1));
0036     fakeWorld.addItem(particleA);
0037     fakeWorld.addItem(particleB);
0038 
0039     // test gravitational force
0040     GravitationForce force(9.8); // use this constant to make verification easier
0041     force.setWorld(&fakeWorld);
0042     force.calcForce(true); // test variance errors in same run
0043 
0044     // only affects particles
0045     QCOMPARE(double(body->force()[0]), 0.0);
0046     QCOMPARE(double(body->force()[1]), 0.0);
0047 
0048     QCOMPARE(double(particleA->force()[0]), 0.0);
0049     QCOMPARE(double(particleA->force()[1]), 980.0);
0050     QCOMPARE(double(particleB->force()[0]), 0.0);
0051     QCOMPARE(double(particleB->force()[1]), -979.0); // note the force
0052 
0053     QCOMPARE(double(particleA->particleErrors()->positionVariance()[0]), 0.0);
0054     QCOMPARE(double(particleA->particleErrors()->positionVariance()[1]), 0.0);
0055 
0056     QVERIFY(force.world());
0057 }
0058 
0059 void TestForces::testWeightForce()
0060 {
0061     // setup world
0062     World fakeWorld;
0063 
0064     Particle *particle = new Particle;
0065     particle->setMass(10);
0066     fakeWorld.addItem(particle);
0067 
0068     RigidBody *body = new RigidBody;
0069     body->setMass(10);
0070     body->setPosition(Vector2d::Zero());
0071     fakeWorld.addItem(body);
0072 
0073     WeightForce force(9.8); // use this constant to make verification easier
0074     force.setWorld(&fakeWorld);
0075     force.calcForce(true); // test variance errors in same run
0076 
0077     QCOMPARE(double(particle->force()[0]), 0.0);
0078     QCOMPARE(double(particle->force()[1]), -98.0);
0079     QCOMPARE(double(particle->particleErrors()->accelerationVariance()[0]), 0.0);
0080     QVERIFY(double(particle->particleErrors()->accelerationVariance()[1]) < 1e-9);
0081 
0082     QCOMPARE(double(body->force()[0]), 0.0);
0083     QCOMPARE(double(body->force()[1]), -98.0);
0084     QCOMPARE(double(body->position()[0]), 0.0);
0085     QCOMPARE(double(body->position()[1]), 0.0);
0086     QCOMPARE(double(body->torque()), 0.0);
0087     QCOMPARE(double(body->rigidBodyErrors()->accelerationVariance()[0]), 0.0);
0088     QVERIFY(double(body->rigidBodyErrors()->accelerationVariance()[1]) < 1e-9);
0089 }
0090 
0091 void TestForces::testRigidlyFixedLinearMotor_data()
0092 {
0093     QTest::addColumn<bool>("rigidlyFixed");
0094     QTest::addColumn<Vector2d>("initialForce");
0095     QTest::addColumn<double>("angle");
0096     QTest::addColumn<Vector2d>("expectedForce");
0097 
0098     QTest::addRow("not-fixed") << false << Vector2d(1, 0) << M_PI_2 << Vector2d(1, 0);
0099     QTest::addRow("fixed")     << true  << Vector2d(1, 0) << M_PI_2 << Vector2d(0, 1);
0100 }
0101 
0102 void TestForces::testRigidlyFixedLinearMotor()
0103 {
0104     QFETCH(bool, rigidlyFixed);
0105     QFETCH(Vector2d, initialForce);
0106     QFETCH(double, angle);
0107     QFETCH(Vector2d, expectedForce);
0108 
0109     // setup world
0110     World fakeWorld;
0111 
0112     RigidBody *body = new RigidBody;
0113     body->setPosition(Vector2d::Zero());
0114     fakeWorld.addItem(body);
0115 
0116     LinearMotor motor;
0117     motor.setBody(body);
0118     motor.setForceValue(initialForce);
0119     motor.setRigidlyFixed(rigidlyFixed);
0120 
0121     body->setAngle(angle);
0122     motor.calcForce(/*calcVariances:*/ false);
0123 
0124     QCOMPARE(motor.forceValue()[0], expectedForce[0]);
0125     QCOMPARE(motor.forceValue()[1], expectedForce[1]);
0126 }
0127 
0128 QTEST_MAIN(TestForces)
0129 
0130 #include "moc_test_forces.cpp"