File indexing completed on 2024-04-21 14:43:25

0001 /*
0002  * box2dprismaticjoint.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 "box2dfrictionjoint.h"
0027 
0028 #include "box2dworld.h"
0029 #include "box2dbody.h"
0030 
0031 Box2DFrictionJoint::Box2DFrictionJoint(QObject *parent)
0032     : Box2DJoint(FrictionJoint, parent)
0033     , m_maxForce(0.0f)
0034     , m_maxTorque(0.0f)
0035     , m_defaultLocalAnchorA(true)
0036     , m_defaultLocalAnchorB(true)
0037 {
0038 }
0039 
0040 void Box2DFrictionJoint::setLocalAnchorA(const QPointF &localAnchorA)
0041 {
0042     m_defaultLocalAnchorA = false;
0043 
0044     if (m_localAnchorA == localAnchorA)
0045         return;
0046 
0047     m_localAnchorA = localAnchorA;
0048     emit localAnchorAChanged();
0049 }
0050 
0051 void Box2DFrictionJoint::setLocalAnchorB(const QPointF &localAnchorB)
0052 {
0053     m_defaultLocalAnchorB = false;
0054 
0055     if (m_localAnchorB == localAnchorB)
0056         return;
0057 
0058     m_localAnchorB = localAnchorB;
0059     emit localAnchorBChanged();
0060 }
0061 
0062 void Box2DFrictionJoint::setMaxForce(float maxForce)
0063 {
0064     if (!(b2IsValid(maxForce) && maxForce >= 0.0f)) {
0065         qWarning() << "FrictionJoint: Invalid maxForce:" << maxForce;
0066         return;
0067     }
0068     if (m_maxForce == maxForce)
0069         return;
0070 
0071     m_maxForce = maxForce;
0072     if (frictionJoint())
0073         frictionJoint()->SetMaxForce(maxForce);
0074     emit maxForceChanged();
0075 }
0076 
0077 void Box2DFrictionJoint::setMaxTorque(float maxTorque)
0078 {
0079     if (!(b2IsValid(maxTorque) && maxTorque >= 0.0f)) {
0080         qWarning() << "FrictionJoint: Invalid maxTorque:" << maxTorque;
0081         return;
0082     }
0083     if (m_maxTorque == maxTorque)
0084         return;
0085 
0086     m_maxTorque = maxTorque;
0087     if (frictionJoint())
0088         frictionJoint()->SetMaxTorque(maxTorque);
0089     emit maxTorqueChanged();
0090 }
0091 
0092 b2Joint *Box2DFrictionJoint::createJoint()
0093 {
0094     b2FrictionJointDef jointDef;
0095     initializeJointDef(jointDef);
0096 
0097     // Default localAnchorA to bodyA center
0098     if (m_defaultLocalAnchorA)
0099         jointDef.localAnchorA = jointDef.bodyA->GetLocalCenter();
0100     else
0101         jointDef.localAnchorA = world()->toMeters(m_localAnchorA);
0102 
0103     // Default localAnchorB to the same world position as localAnchorA
0104     if (m_defaultLocalAnchorB) {
0105         b2Vec2 anchorA = jointDef.bodyA->GetWorldPoint(jointDef.localAnchorA);
0106         jointDef.localAnchorB = jointDef.bodyB->GetLocalPoint(anchorA);
0107     } else {
0108         jointDef.localAnchorB = world()->toMeters(m_localAnchorB);
0109     }
0110 
0111     jointDef.maxForce = m_maxForce;
0112     jointDef.maxTorque = m_maxTorque;
0113 
0114     return world()->world().CreateJoint(&jointDef);
0115 }
0116 
0117 QPointF Box2DFrictionJoint::getReactionForce(float32 inv_dt) const
0118 {
0119     if (frictionJoint())
0120         return invertY(frictionJoint()->GetReactionForce(inv_dt));
0121     return QPointF();
0122 }
0123 
0124 float Box2DFrictionJoint::getReactionTorque(float32 inv_dt) const
0125 {
0126     if (frictionJoint())
0127         return frictionJoint()->GetReactionTorque(inv_dt);
0128     return 0.0f;
0129 }