File indexing completed on 2024-12-29 03:29:27
0001 /* 0002 * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 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/b2WheelJoint.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/Joints/b2MotorJoint.h> 0031 #include <Box2D/Dynamics/b2Body.h> 0032 #include <Box2D/Dynamics/b2World.h> 0033 #include <Box2D/Common/b2BlockAllocator.h> 0034 0035 #include <new> 0036 0037 b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator) 0038 { 0039 b2Joint* joint = NULL; 0040 0041 switch (def->type) 0042 { 0043 case e_distanceJoint: 0044 { 0045 void* mem = allocator->Allocate(sizeof(b2DistanceJoint)); 0046 joint = new (mem) b2DistanceJoint(static_cast<const b2DistanceJointDef*>(def)); 0047 } 0048 break; 0049 0050 case e_mouseJoint: 0051 { 0052 void* mem = allocator->Allocate(sizeof(b2MouseJoint)); 0053 joint = new (mem) b2MouseJoint(static_cast<const b2MouseJointDef*>(def)); 0054 } 0055 break; 0056 0057 case e_prismaticJoint: 0058 { 0059 void* mem = allocator->Allocate(sizeof(b2PrismaticJoint)); 0060 joint = new (mem) b2PrismaticJoint(static_cast<const b2PrismaticJointDef*>(def)); 0061 } 0062 break; 0063 0064 case e_revoluteJoint: 0065 { 0066 void* mem = allocator->Allocate(sizeof(b2RevoluteJoint)); 0067 joint = new (mem) b2RevoluteJoint(static_cast<const b2RevoluteJointDef*>(def)); 0068 } 0069 break; 0070 0071 case e_pulleyJoint: 0072 { 0073 void* mem = allocator->Allocate(sizeof(b2PulleyJoint)); 0074 joint = new (mem) b2PulleyJoint(static_cast<const b2PulleyJointDef*>(def)); 0075 } 0076 break; 0077 0078 case e_gearJoint: 0079 { 0080 void* mem = allocator->Allocate(sizeof(b2GearJoint)); 0081 joint = new (mem) b2GearJoint(static_cast<const b2GearJointDef*>(def)); 0082 } 0083 break; 0084 0085 case e_wheelJoint: 0086 { 0087 void* mem = allocator->Allocate(sizeof(b2WheelJoint)); 0088 joint = new (mem) b2WheelJoint(static_cast<const b2WheelJointDef*>(def)); 0089 } 0090 break; 0091 0092 case e_weldJoint: 0093 { 0094 void* mem = allocator->Allocate(sizeof(b2WeldJoint)); 0095 joint = new (mem) b2WeldJoint(static_cast<const b2WeldJointDef*>(def)); 0096 } 0097 break; 0098 0099 case e_frictionJoint: 0100 { 0101 void* mem = allocator->Allocate(sizeof(b2FrictionJoint)); 0102 joint = new (mem) b2FrictionJoint(static_cast<const b2FrictionJointDef*>(def)); 0103 } 0104 break; 0105 0106 case e_ropeJoint: 0107 { 0108 void* mem = allocator->Allocate(sizeof(b2RopeJoint)); 0109 joint = new (mem) b2RopeJoint(static_cast<const b2RopeJointDef*>(def)); 0110 } 0111 break; 0112 0113 case e_motorJoint: 0114 { 0115 void* mem = allocator->Allocate(sizeof(b2MotorJoint)); 0116 joint = new (mem) b2MotorJoint(static_cast<const b2MotorJointDef*>(def)); 0117 } 0118 break; 0119 0120 default: 0121 b2Assert(false); 0122 break; 0123 } 0124 0125 return joint; 0126 } 0127 0128 void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator) 0129 { 0130 joint->~b2Joint(); 0131 switch (joint->m_type) 0132 { 0133 case e_distanceJoint: 0134 allocator->Free(joint, sizeof(b2DistanceJoint)); 0135 break; 0136 0137 case e_mouseJoint: 0138 allocator->Free(joint, sizeof(b2MouseJoint)); 0139 break; 0140 0141 case e_prismaticJoint: 0142 allocator->Free(joint, sizeof(b2PrismaticJoint)); 0143 break; 0144 0145 case e_revoluteJoint: 0146 allocator->Free(joint, sizeof(b2RevoluteJoint)); 0147 break; 0148 0149 case e_pulleyJoint: 0150 allocator->Free(joint, sizeof(b2PulleyJoint)); 0151 break; 0152 0153 case e_gearJoint: 0154 allocator->Free(joint, sizeof(b2GearJoint)); 0155 break; 0156 0157 case e_wheelJoint: 0158 allocator->Free(joint, sizeof(b2WheelJoint)); 0159 break; 0160 0161 case e_weldJoint: 0162 allocator->Free(joint, sizeof(b2WeldJoint)); 0163 break; 0164 0165 case e_frictionJoint: 0166 allocator->Free(joint, sizeof(b2FrictionJoint)); 0167 break; 0168 0169 case e_ropeJoint: 0170 allocator->Free(joint, sizeof(b2RopeJoint)); 0171 break; 0172 0173 case e_motorJoint: 0174 allocator->Free(joint, sizeof(b2MotorJoint)); 0175 break; 0176 0177 default: 0178 b2Assert(false); 0179 break; 0180 } 0181 } 0182 0183 b2Joint::b2Joint(const b2JointDef* def) 0184 { 0185 b2Assert(def->bodyA != def->bodyB); 0186 0187 m_type = def->type; 0188 m_prev = NULL; 0189 m_next = NULL; 0190 m_bodyA = def->bodyA; 0191 m_bodyB = def->bodyB; 0192 m_index = 0; 0193 m_collideConnected = def->collideConnected; 0194 m_islandFlag = false; 0195 m_userData = def->userData; 0196 0197 m_edgeA.joint = NULL; 0198 m_edgeA.other = NULL; 0199 m_edgeA.prev = NULL; 0200 m_edgeA.next = NULL; 0201 0202 m_edgeB.joint = NULL; 0203 m_edgeB.other = NULL; 0204 m_edgeB.prev = NULL; 0205 m_edgeB.next = NULL; 0206 } 0207 0208 bool b2Joint::IsActive() const 0209 { 0210 return m_bodyA->IsActive() && m_bodyB->IsActive(); 0211 }