File indexing completed on 2024-06-09 09:27:47

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef MECHANICSSIMULATION_H
0012 #define MECHANICSSIMULATION_H
0013 
0014 #include <QList>
0015 #include <QObject>
0016 #include <QPointer>
0017 
0018 class QTimer;
0019 class MechanicsItem;
0020 class MechanicsDocument;
0021 typedef QList<MechanicsItem *> MechanicsItemList;
0022 
0023 /**
0024 @short 2 dimensional vector with associated length functions et al
0025 @author David Saxton
0026 */
0027 class Vector2D
0028 {
0029 public:
0030     Vector2D();
0031 
0032     double length() const;
0033     double lengthSquared() const
0034     {
0035         return x * x + y * y;
0036     }
0037 
0038     double x;
0039     double y;
0040 };
0041 
0042 /**
0043 @short State of a rigid body, in an inertial MechanicsDocument frame
0044 @author David Saxton
0045 */
0046 class RigidBodyState
0047 {
0048 public:
0049     RigidBodyState();
0050 
0051     Vector2D linearMomentum;
0052     double angularMomentum;
0053     Vector2D position; // Position of center of mass
0054 };
0055 
0056 /**
0057 @author David Saxton
0058 */
0059 class MechanicsSimulation : public QObject
0060 {
0061     Q_OBJECT
0062 public:
0063     MechanicsSimulation(MechanicsDocument *mechanicsDocument);
0064     ~MechanicsSimulation() override;
0065 
0066     MechanicsDocument *mechanicsDocument() const
0067     {
0068         return p_mechanicsDocument;
0069     }
0070 
0071 protected slots:
0072     void slotAdvance();
0073 
0074 protected:
0075     QPointer<MechanicsDocument> p_mechanicsDocument;
0076     QTimer *m_advanceTmr;
0077 };
0078 
0079 /**
0080 Rigid body with mass, inertia, etc. Collection of mechanics items with
0081 functionality for moving them about, rotating, etc. Only one mother-parent
0082 (has no parent itself, all other items are descendents) allowed.
0083 @short Rigid body, handles MechanicsItems
0084 @author David Saxton
0085 */
0086 class RigidBody
0087 {
0088 public:
0089     RigidBody(MechanicsDocument *mechanicsDocument);
0090     ~RigidBody();
0091 
0092     /**
0093      *
0094      */
0095     void advance(int phase, double delta);
0096     /**
0097      * Add the MechanicsItem to the entity.
0098      * @returns true iff successful in adding
0099      */
0100     bool addMechanicsItem(MechanicsItem *item);
0101     /**
0102      * Pointer to the mother MechanicsItem.
0103      */
0104     MechanicsItem *overallParent() const
0105     {
0106         return p_overallParent;
0107     }
0108     /**
0109      * Updates the mass and the moment of inertia info
0110      */
0111     void updateRigidBodyInfo();
0112 
0113 protected:
0114     /**
0115      * Attempt to find the overall parent.
0116      * @returns false iff unsucessful (including if there are no MechanicsItems present)
0117      */
0118     bool findOverallParent();
0119     /**
0120      * Move the set of MechanicsItems by the given amount
0121      */
0122     void moveBy(double dx, double dy);
0123     /**
0124      * Rotate the set of MechanicsItems by the given amount about the center of
0125      * mass.
0126      * @param dtheta Rotate amount in radians
0127      */
0128     void rotateBy(double dtheta);
0129 
0130     MechanicsItemList m_mechanicsItemList;
0131     MechanicsItem *p_overallParent;
0132     MechanicsDocument *p_mechanicsDocument;
0133 
0134     RigidBodyState m_rigidBodyState;
0135     double m_mass;
0136     double m_momentOfInertia;
0137 };
0138 
0139 #endif