File indexing completed on 2025-08-03 03:49:58

0001 /*
0002 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #include <Box2D/Dynamics/Joints/b2Joint.h>
0020 #include <Box2D/Dynamics/Joints/b2DistanceJoint.h>
0021 #include <Box2D/Dynamics/Joints/b2LineJoint.h>
0022 #include <Box2D/Dynamics/Joints/b2MouseJoint.h>
0023 #include <Box2D/Dynamics/Joints/b2RevoluteJoint.h>
0024 #include <Box2D/Dynamics/Joints/b2PrismaticJoint.h>
0025 #include <Box2D/Dynamics/Joints/b2PulleyJoint.h>
0026 #include <Box2D/Dynamics/Joints/b2GearJoint.h>
0027 #include <Box2D/Dynamics/Joints/b2WeldJoint.h>
0028 #include <Box2D/Dynamics/Joints/b2FrictionJoint.h>
0029 #include <Box2D/Dynamics/Joints/b2RopeJoint.h>
0030 #include <Box2D/Dynamics/b2Body.h>
0031 #include <Box2D/Dynamics/b2World.h>
0032 #include <Box2D/Common/b2BlockAllocator.h>
0033 
0034 #include <new>
0035 
0036 b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
0037 {
0038     b2Joint* joint = NULL;
0039 
0040     switch (def->type)
0041     {
0042     case e_distanceJoint:
0043         {
0044             void* mem = allocator->Allocate(sizeof(b2DistanceJoint));
0045             joint = new (mem) b2DistanceJoint((b2DistanceJointDef*)def);
0046         }
0047         break;
0048 
0049     case e_mouseJoint:
0050         {
0051             void* mem = allocator->Allocate(sizeof(b2MouseJoint));
0052             joint = new (mem) b2MouseJoint((b2MouseJointDef*)def);
0053         }
0054         break;
0055 
0056     case e_prismaticJoint:
0057         {
0058             void* mem = allocator->Allocate(sizeof(b2PrismaticJoint));
0059             joint = new (mem) b2PrismaticJoint((b2PrismaticJointDef*)def);
0060         }
0061         break;
0062 
0063     case e_revoluteJoint:
0064         {
0065             void* mem = allocator->Allocate(sizeof(b2RevoluteJoint));
0066             joint = new (mem) b2RevoluteJoint((b2RevoluteJointDef*)def);
0067         }
0068         break;
0069 
0070     case e_pulleyJoint:
0071         {
0072             void* mem = allocator->Allocate(sizeof(b2PulleyJoint));
0073             joint = new (mem) b2PulleyJoint((b2PulleyJointDef*)def);
0074         }
0075         break;
0076 
0077     case e_gearJoint:
0078         {
0079             void* mem = allocator->Allocate(sizeof(b2GearJoint));
0080             joint = new (mem) b2GearJoint((b2GearJointDef*)def);
0081         }
0082         break;
0083 
0084     case e_lineJoint:
0085         {
0086             void* mem = allocator->Allocate(sizeof(b2LineJoint));
0087             joint = new (mem) b2LineJoint((b2LineJointDef*)def);
0088         }
0089         break;
0090 
0091     case e_weldJoint:
0092         {
0093             void* mem = allocator->Allocate(sizeof(b2WeldJoint));
0094             joint = new (mem) b2WeldJoint((b2WeldJointDef*)def);
0095         }
0096         break;
0097         
0098     case e_frictionJoint:
0099         {
0100             void* mem = allocator->Allocate(sizeof(b2FrictionJoint));
0101             joint = new (mem) b2FrictionJoint((b2FrictionJointDef*)def);
0102         }
0103         break;
0104 
0105     case e_ropeJoint:
0106         {
0107             void* mem = allocator->Allocate(sizeof(b2RopeJoint));
0108             joint = new (mem) b2RopeJoint((b2RopeJointDef*)def);
0109         }
0110         break;
0111 
0112     default:
0113         b2Assert(false);
0114         break;
0115     }
0116 
0117     return joint;
0118 }
0119 
0120 void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
0121 {
0122     joint->~b2Joint();
0123     switch (joint->m_type)
0124     {
0125     case e_distanceJoint:
0126         allocator->Free(joint, sizeof(b2DistanceJoint));
0127         break;
0128 
0129     case e_mouseJoint:
0130         allocator->Free(joint, sizeof(b2MouseJoint));
0131         break;
0132 
0133     case e_prismaticJoint:
0134         allocator->Free(joint, sizeof(b2PrismaticJoint));
0135         break;
0136 
0137     case e_revoluteJoint:
0138         allocator->Free(joint, sizeof(b2RevoluteJoint));
0139         break;
0140 
0141     case e_pulleyJoint:
0142         allocator->Free(joint, sizeof(b2PulleyJoint));
0143         break;
0144 
0145     case e_gearJoint:
0146         allocator->Free(joint, sizeof(b2GearJoint));
0147         break;
0148 
0149     case e_lineJoint:
0150         allocator->Free(joint, sizeof(b2LineJoint));
0151         break;
0152     
0153     case e_weldJoint:
0154         allocator->Free(joint, sizeof(b2WeldJoint));
0155         break;
0156 
0157     case e_frictionJoint:
0158         allocator->Free(joint, sizeof(b2FrictionJoint));
0159         break;
0160 
0161     case e_ropeJoint:
0162         allocator->Free(joint, sizeof(b2RopeJoint));
0163         break;
0164 
0165     default:
0166         b2Assert(false);
0167         break;
0168     }
0169 }
0170 
0171 b2Joint::b2Joint(const b2JointDef* def)
0172 {
0173     b2Assert(def->bodyA != def->bodyB);
0174 
0175     m_type = def->type;
0176     m_prev = NULL;
0177     m_next = NULL;
0178     m_bodyA = def->bodyA;
0179     m_bodyB = def->bodyB;
0180     m_collideConnected = def->collideConnected;
0181     m_islandFlag = false;
0182     m_userData = def->userData;
0183 
0184     m_edgeA.joint = NULL;
0185     m_edgeA.other = NULL;
0186     m_edgeA.prev = NULL;
0187     m_edgeA.next = NULL;
0188 
0189     m_edgeB.joint = NULL;
0190     m_edgeB.other = NULL;
0191     m_edgeB.prev = NULL;
0192     m_edgeB.next = NULL;
0193 }
0194 
0195 bool b2Joint::IsActive() const
0196 {
0197     return m_bodyA->IsActive() && m_bodyB->IsActive();
0198 }