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

0001 /*
0002  * box2dmotorjoint.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 "box2dmotorjoint.h"
0028 
0029 #include "box2dworld.h"
0030 #include "box2dbody.h"
0031 
0032 Box2DMotorJoint::Box2DMotorJoint(QObject *parent)
0033     : Box2DJoint(MotorJoint, parent)
0034     , m_angularOffset(0.0f)
0035     , m_maxForce(1.0f)
0036     , m_maxTorque(1.0f)
0037     , m_correctionFactor(0.3f)
0038     , m_defaultLinearOffset(true)
0039     , m_defaultAngularOffset(true)
0040 {
0041 }
0042 
0043 void Box2DMotorJoint::setLinearOffset(const QPointF &linearOffset)
0044 {
0045     m_defaultLinearOffset = false;
0046 
0047     if (m_linearOffset == linearOffset)
0048         return;
0049 
0050     m_linearOffset = linearOffset;
0051     if (motorJoint())
0052         motorJoint()->SetLinearOffset(world()->toMeters(linearOffset));
0053     emit linearOffsetChanged();
0054 }
0055 
0056 void Box2DMotorJoint::setAngularOffset(float angularOffset)
0057 {
0058     m_defaultAngularOffset = false;
0059 
0060     if (m_angularOffset == angularOffset)
0061         return;
0062 
0063     m_angularOffset = angularOffset;
0064     if (motorJoint())
0065         motorJoint()->SetAngularOffset(toRadians(angularOffset));
0066     emit angularOffsetChanged();
0067 }
0068 
0069 void Box2DMotorJoint::setMaxForce(float maxForce)
0070 {
0071     if (m_maxForce == maxForce)
0072         return;
0073 
0074     m_maxForce = maxForce;
0075     if (motorJoint())
0076         motorJoint()->SetMaxForce(maxForce);
0077     emit maxForceChanged();
0078 }
0079 
0080 void Box2DMotorJoint::setMaxTorque(float maxTorque)
0081 {
0082     if (m_maxTorque == maxTorque)
0083         return;
0084 
0085     m_maxTorque = maxTorque;
0086     if (motorJoint())
0087         motorJoint()->SetMaxTorque(maxTorque);
0088     emit maxTorqueChanged();
0089 }
0090 
0091 void Box2DMotorJoint::setCorrectionFactor(float correctionFactor)
0092 {
0093     if (m_correctionFactor == correctionFactor)
0094         return;
0095 
0096     m_correctionFactor = correctionFactor;
0097     if (motorJoint())
0098         motorJoint()->SetCorrectionFactor(correctionFactor);
0099     emit correctionFactorChanged();
0100 }
0101 
0102 b2Joint *Box2DMotorJoint::createJoint()
0103 {
0104     b2MotorJointDef jointDef;
0105     initializeJointDef(jointDef);
0106 
0107     if (m_defaultLinearOffset) {
0108         const b2Vec2 &positionB = jointDef.bodyB->GetPosition();
0109         jointDef.linearOffset = jointDef.bodyA->GetLocalPoint(positionB);
0110     } else {
0111         jointDef.linearOffset = world()->toMeters(m_linearOffset);
0112     }
0113 
0114     if (m_defaultAngularOffset) {
0115         float32 angleA = jointDef.bodyA->GetAngle();
0116         float32 angleB = jointDef.bodyB->GetAngle();
0117         jointDef.angularOffset = angleB - angleA;
0118     } else {
0119         jointDef.angularOffset = toRadians(m_angularOffset);
0120     }
0121 
0122     jointDef.maxForce = m_maxForce;
0123     jointDef.maxTorque = m_maxTorque;
0124     jointDef.correctionFactor = m_correctionFactor;
0125 
0126     return world()->world().CreateJoint(&jointDef);
0127 }