1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18
19 #include <Box2D/Dynamics/Joints/b2Joint.h>
20 #include <Box2D/Dynamics/Joints/b2DistanceJoint.h>
21 #include <Box2D/Dynamics/Joints/b2WheelJoint.h>
22 #include <Box2D/Dynamics/Joints/b2MouseJoint.h>
23 #include <Box2D/Dynamics/Joints/b2RevoluteJoint.h>
24 #include <Box2D/Dynamics/Joints/b2PrismaticJoint.h>
25 #include <Box2D/Dynamics/Joints/b2PulleyJoint.h>
26 #include <Box2D/Dynamics/Joints/b2GearJoint.h>
27 #include <Box2D/Dynamics/Joints/b2WeldJoint.h>
28 #include <Box2D/Dynamics/Joints/b2FrictionJoint.h>
29 #include <Box2D/Dynamics/Joints/b2RopeJoint.h>
30 #include <Box2D/Dynamics/b2Body.h>
31 #include <Box2D/Dynamics/b2World.h>
32 #include <Box2D/Common/b2BlockAllocator.h>
33
34 #include <new>
35
Create(const b2JointDef * def,b2BlockAllocator * allocator)36 b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
37 {
38 b2Joint* joint = NULL;
39
40 switch (def->type)
41 {
42 case e_distanceJoint:
43 {
44 void* mem = allocator->Allocate(sizeof(b2DistanceJoint));
45 joint = new (mem) b2DistanceJoint((b2DistanceJointDef*)def);
46 }
47 break;
48
49 case e_mouseJoint:
50 {
51 void* mem = allocator->Allocate(sizeof(b2MouseJoint));
52 joint = new (mem) b2MouseJoint((b2MouseJointDef*)def);
53 }
54 break;
55
56 case e_prismaticJoint:
57 {
58 void* mem = allocator->Allocate(sizeof(b2PrismaticJoint));
59 joint = new (mem) b2PrismaticJoint((b2PrismaticJointDef*)def);
60 }
61 break;
62
63 case e_revoluteJoint:
64 {
65 void* mem = allocator->Allocate(sizeof(b2RevoluteJoint));
66 joint = new (mem) b2RevoluteJoint((b2RevoluteJointDef*)def);
67 }
68 break;
69
70 case e_pulleyJoint:
71 {
72 void* mem = allocator->Allocate(sizeof(b2PulleyJoint));
73 joint = new (mem) b2PulleyJoint((b2PulleyJointDef*)def);
74 }
75 break;
76
77 case e_gearJoint:
78 {
79 void* mem = allocator->Allocate(sizeof(b2GearJoint));
80 joint = new (mem) b2GearJoint((b2GearJointDef*)def);
81 }
82 break;
83
84 case e_wheelJoint:
85 {
86 void* mem = allocator->Allocate(sizeof(b2WheelJoint));
87 joint = new (mem) b2WheelJoint((b2WheelJointDef*)def);
88 }
89 break;
90
91 case e_weldJoint:
92 {
93 void* mem = allocator->Allocate(sizeof(b2WeldJoint));
94 joint = new (mem) b2WeldJoint((b2WeldJointDef*)def);
95 }
96 break;
97
98 case e_frictionJoint:
99 {
100 void* mem = allocator->Allocate(sizeof(b2FrictionJoint));
101 joint = new (mem) b2FrictionJoint((b2FrictionJointDef*)def);
102 }
103 break;
104
105 case e_ropeJoint:
106 {
107 void* mem = allocator->Allocate(sizeof(b2RopeJoint));
108 joint = new (mem) b2RopeJoint((b2RopeJointDef*)def);
109 }
110 break;
111
112 default:
113 b2Assert(false);
114 break;
115 }
116
117 return joint;
118 }
119
Destroy(b2Joint * joint,b2BlockAllocator * allocator)120 void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
121 {
122 joint->~b2Joint();
123 switch (joint->m_type)
124 {
125 case e_distanceJoint:
126 allocator->Free(joint, sizeof(b2DistanceJoint));
127 break;
128
129 case e_mouseJoint:
130 allocator->Free(joint, sizeof(b2MouseJoint));
131 break;
132
133 case e_prismaticJoint:
134 allocator->Free(joint, sizeof(b2PrismaticJoint));
135 break;
136
137 case e_revoluteJoint:
138 allocator->Free(joint, sizeof(b2RevoluteJoint));
139 break;
140
141 case e_pulleyJoint:
142 allocator->Free(joint, sizeof(b2PulleyJoint));
143 break;
144
145 case e_gearJoint:
146 allocator->Free(joint, sizeof(b2GearJoint));
147 break;
148
149 case e_wheelJoint:
150 allocator->Free(joint, sizeof(b2WheelJoint));
151 break;
152
153 case e_weldJoint:
154 allocator->Free(joint, sizeof(b2WeldJoint));
155 break;
156
157 case e_frictionJoint:
158 allocator->Free(joint, sizeof(b2FrictionJoint));
159 break;
160
161 case e_ropeJoint:
162 allocator->Free(joint, sizeof(b2RopeJoint));
163 break;
164
165 default:
166 b2Assert(false);
167 break;
168 }
169 }
170
b2Joint(const b2JointDef * def)171 b2Joint::b2Joint(const b2JointDef* def)
172 {
173 b2Assert(def->bodyA != def->bodyB);
174
175 m_type = def->type;
176 m_prev = NULL;
177 m_next = NULL;
178 m_bodyA = def->bodyA;
179 m_bodyB = def->bodyB;
180 m_index = 0;
181 m_collideConnected = def->collideConnected;
182 m_islandFlag = false;
183 m_userData = def->userData;
184
185 m_edgeA.joint = NULL;
186 m_edgeA.other = NULL;
187 m_edgeA.prev = NULL;
188 m_edgeA.next = NULL;
189
190 m_edgeB.joint = NULL;
191 m_edgeB.other = NULL;
192 m_edgeB.prev = NULL;
193 m_edgeB.next = NULL;
194 }
195
IsActive() const196 bool b2Joint::IsActive() const
197 {
198 return m_bodyA->IsActive() && m_bodyB->IsActive();
199 }
200