File indexing completed on 2024-04-14 14:08:24

0001 /*
0002  * box2dmousejoint.cpp
0003  * Copyright (c) 2011 Joonas Erkinheimo <joonas.erkinheimo@nokia.com>
0004  *
0005  * This file is part of the Box2D QML plugin.
0006  *
0007  * This software is provided 'as-is', without any express or implied warranty.
0008  * In no event will the authors be held liable for any damages arising from
0009  * the use of this software.
0010  *
0011  * Permission is granted to anyone to use this software for any purpose,
0012  * including commercial applications, and to alter it and redistribute it
0013  * freely, subject to the following restrictions:
0014  *
0015  * 1. The origin of this software must not be misrepresented; you must not
0016  *    claim that you wrote the original software. If you use this software in
0017  *    a product, an acknowledgment in the product documentation would be
0018  *    appreciated but is not required.
0019  *
0020  * 2. Altered source versions must be plainly marked as such, and must not be
0021  *    misrepresented as being the original software.
0022  *
0023  * 3. This notice may not be removed or altered from any source distribution.
0024  */
0025 
0026 #include "box2dmousejoint.h"
0027 
0028 #include "box2dworld.h"
0029 #include "box2dbody.h"
0030 
0031 Box2DMouseJoint::Box2DMouseJoint(QObject *parent)
0032     : Box2DJoint(MouseJoint, parent)
0033     , m_maxForce(0.0f)
0034     , m_frequencyHz(5.0f)
0035     , m_dampingRatio(0.7f)
0036 {
0037 }
0038 
0039 void Box2DMouseJoint::setDampingRatio(float dampingRatio)
0040 {
0041     if (m_dampingRatio == dampingRatio)
0042         return;
0043 
0044     m_dampingRatio = dampingRatio;
0045     if (mouseJoint())
0046         mouseJoint()->SetDampingRatio(dampingRatio);
0047     emit dampingRatioChanged();
0048 }
0049 
0050 void Box2DMouseJoint::setFrequencyHz(float frequencyHz)
0051 {
0052     if (m_frequencyHz == frequencyHz)
0053         return;
0054 
0055     m_frequencyHz = frequencyHz;
0056     if (mouseJoint())
0057         mouseJoint()->SetFrequency(frequencyHz);
0058     emit frequencyHzChanged();
0059 }
0060 
0061 void Box2DMouseJoint::setMaxForce(float maxForce)
0062 {
0063     if (m_maxForce == maxForce)
0064         return;
0065 
0066     m_maxForce = maxForce;
0067     if (mouseJoint())
0068         mouseJoint()->SetMaxForce(maxForce);
0069     emit maxForceChanged();
0070 }
0071 
0072 void Box2DMouseJoint::setTarget(const QPointF &target)
0073 {
0074     if (m_target == target)
0075         return;
0076 
0077     m_target = target;
0078     if (mouseJoint())
0079         mouseJoint()->SetTarget(world()->toMeters(target));
0080     emit targetChanged();
0081 }
0082 
0083 b2Joint *Box2DMouseJoint::createJoint()
0084 {
0085     b2MouseJointDef jointDef;
0086     initializeJointDef(jointDef);
0087 
0088     jointDef.target = world()->toMeters(m_target);
0089     jointDef.maxForce = m_maxForce;
0090     jointDef.frequencyHz = m_frequencyHz;
0091     jointDef.dampingRatio = m_dampingRatio;
0092 
0093     return world()->world().CreateJoint(&jointDef);
0094 }
0095 
0096 QPointF Box2DMouseJoint::getReactionForce(float32 inv_dt) const
0097 {
0098     if (mouseJoint())
0099         return invertY(mouseJoint()->GetReactionForce(inv_dt));
0100     return QPointF();
0101 }
0102 
0103 float Box2DMouseJoint::getReactionTorque(float32 inv_dt) const
0104 {
0105     if (mouseJoint())
0106         return mouseJoint()->GetReactionTorque(inv_dt);
0107     return 0.0f;
0108 }