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

0001 /*
0002  * box2drevolutejoint.cpp
0003  * Copyright (c) 2011 Joonas Erkinheimo <joonas.erkinheimo@nokia.com>
0004  * Copyright (c) 2011 Markus Kivioja <markus.kivioja@digia.com>
0005  *
0006  * This file is part of the Box2D QML plugin.
0007  *
0008  * This software is provided 'as-is', without any express or implied warranty.
0009  * In no event will the authors be held liable for any damages arising from
0010  * the use of this software.
0011  *
0012  * Permission is granted to anyone to use this software for any purpose,
0013  * including commercial applications, and to alter it and redistribute it
0014  * freely, subject to the following restrictions:
0015  *
0016  * 1. The origin of this software must not be misrepresented; you must not
0017  *    claim that you wrote the original software. If you use this software in
0018  *    a product, an acknowledgment in the product documentation would be
0019  *    appreciated but is not required.
0020  *
0021  * 2. Altered source versions must be plainly marked as such, and must not be
0022  *    misrepresented as being the original software.
0023  *
0024  * 3. This notice may not be removed or altered from any source distribution.
0025  */
0026 
0027 #include "box2dropejoint.h"
0028 
0029 #include "box2dworld.h"
0030 
0031 Box2DRopeJoint::Box2DRopeJoint(QObject *parent)
0032     : Box2DJoint(RopeJoint, parent)
0033     , m_maxLength(0.0f)
0034     , m_defaultLocalAnchorA(true)
0035     , m_defaultLocalAnchorB(true)
0036 {
0037 }
0038 
0039 void Box2DRopeJoint::setLocalAnchorA(const QPointF &localAnchorA)
0040 {
0041     m_defaultLocalAnchorA = false;
0042 
0043     if (m_localAnchorA == localAnchorA)
0044         return;
0045 
0046     m_localAnchorA = localAnchorA;
0047     emit localAnchorAChanged();
0048 }
0049 
0050 void Box2DRopeJoint::setLocalAnchorB(const QPointF &localAnchorB)
0051 {
0052     m_defaultLocalAnchorB = false;
0053 
0054     if (m_localAnchorB == localAnchorB)
0055         return;
0056 
0057     m_localAnchorB = localAnchorB;
0058     emit localAnchorBChanged();
0059 }
0060 
0061 void Box2DRopeJoint::setMaxLength(float maxLength)
0062 {
0063     if (m_maxLength == maxLength)
0064         return;
0065 
0066     m_maxLength = maxLength;
0067     if (ropeJoint()) {
0068         const float maxLengthMeters = world()->toMeters(maxLength);
0069         if (maxLengthMeters < b2_linearSlop)
0070             qWarning() << "RopeJoint: maxLength too small";
0071 
0072         ropeJoint()->SetMaxLength(maxLengthMeters);
0073     }
0074     emit maxLengthChanged();
0075 }
0076 
0077 b2Joint *Box2DRopeJoint::createJoint()
0078 {
0079     b2RopeJointDef jointDef;
0080     initializeJointDef(jointDef);
0081 
0082     // Default localAnchorA to bodyA center
0083     if (m_defaultLocalAnchorA)
0084         jointDef.localAnchorA = jointDef.bodyA->GetLocalCenter();
0085     else
0086         jointDef.localAnchorA = world()->toMeters(m_localAnchorA);
0087 
0088     // Default localAnchorB to bodyB center
0089     if (m_defaultLocalAnchorB)
0090         jointDef.localAnchorB = jointDef.bodyB->GetLocalCenter();
0091     else
0092         jointDef.localAnchorB = world()->toMeters(m_localAnchorB);
0093 
0094     jointDef.maxLength = world()->toMeters(m_maxLength);
0095 
0096     if (jointDef.maxLength < b2_linearSlop)
0097         qWarning() << "RopeJoint: maxLength too small";
0098 
0099     return world()->world().CreateJoint(&jointDef);
0100 }
0101 
0102 QPointF Box2DRopeJoint::getReactionForce(float32 inv_dt) const
0103 {
0104     if (ropeJoint())
0105         return invertY(ropeJoint()->GetReactionForce(inv_dt));
0106     return QPointF();
0107 }
0108 
0109 float Box2DRopeJoint::getReactionTorque(float32 inv_dt) const
0110 {
0111     if (ropeJoint())
0112         return ropeJoint()->GetReactionTorque(inv_dt);
0113     return 0.0f;
0114 }