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/Joints/b2MotorJoint.h> |
31 | #include <Box2D/Dynamics/b2Body.h> |
32 | #include <Box2D/Dynamics/b2World.h> |
33 | #include <Box2D/Common/b2BlockAllocator.h> |
34 | |
35 | #include <new> |
36 | |
37 | b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator) |
38 | { |
39 | b2Joint* joint = NULL; |
40 | |
41 | switch (def->type) |
42 | { |
43 | case e_distanceJoint: |
44 | { |
45 | void* mem = allocator->Allocate(sizeof(b2DistanceJoint)); |
46 | joint = new (mem) b2DistanceJoint(static_cast<const b2DistanceJointDef*>(def)); |
47 | } |
48 | break; |
49 | |
50 | case e_mouseJoint: |
51 | { |
52 | void* mem = allocator->Allocate(sizeof(b2MouseJoint)); |
53 | joint = new (mem) b2MouseJoint(static_cast<const b2MouseJointDef*>(def)); |
54 | } |
55 | break; |
56 | |
57 | case e_prismaticJoint: |
58 | { |
59 | void* mem = allocator->Allocate(sizeof(b2PrismaticJoint)); |
60 | joint = new (mem) b2PrismaticJoint(static_cast<const b2PrismaticJointDef*>(def)); |
61 | } |
62 | break; |
63 | |
64 | case e_revoluteJoint: |
65 | { |
66 | void* mem = allocator->Allocate(sizeof(b2RevoluteJoint)); |
67 | joint = new (mem) b2RevoluteJoint(static_cast<const b2RevoluteJointDef*>(def)); |
68 | } |
69 | break; |
70 | |
71 | case e_pulleyJoint: |
72 | { |
73 | void* mem = allocator->Allocate(sizeof(b2PulleyJoint)); |
74 | joint = new (mem) b2PulleyJoint(static_cast<const b2PulleyJointDef*>(def)); |
75 | } |
76 | break; |
77 | |
78 | case e_gearJoint: |
79 | { |
80 | void* mem = allocator->Allocate(sizeof(b2GearJoint)); |
81 | joint = new (mem) b2GearJoint(static_cast<const b2GearJointDef*>(def)); |
82 | } |
83 | break; |
84 | |
85 | case e_wheelJoint: |
86 | { |
87 | void* mem = allocator->Allocate(sizeof(b2WheelJoint)); |
88 | joint = new (mem) b2WheelJoint(static_cast<const b2WheelJointDef*>(def)); |
89 | } |
90 | break; |
91 | |
92 | case e_weldJoint: |
93 | { |
94 | void* mem = allocator->Allocate(sizeof(b2WeldJoint)); |
95 | joint = new (mem) b2WeldJoint(static_cast<const b2WeldJointDef*>(def)); |
96 | } |
97 | break; |
98 | |
99 | case e_frictionJoint: |
100 | { |
101 | void* mem = allocator->Allocate(sizeof(b2FrictionJoint)); |
102 | joint = new (mem) b2FrictionJoint(static_cast<const b2FrictionJointDef*>(def)); |
103 | } |
104 | break; |
105 | |
106 | case e_ropeJoint: |
107 | { |
108 | void* mem = allocator->Allocate(sizeof(b2RopeJoint)); |
109 | joint = new (mem) b2RopeJoint(static_cast<const b2RopeJointDef*>(def)); |
110 | } |
111 | break; |
112 | |
113 | case e_motorJoint: |
114 | { |
115 | void* mem = allocator->Allocate(sizeof(b2MotorJoint)); |
116 | joint = new (mem) b2MotorJoint(static_cast<const b2MotorJointDef*>(def)); |
117 | } |
118 | break; |
119 | |
120 | default: |
121 | b2Assert(false); |
122 | break; |
123 | } |
124 | |
125 | return joint; |
126 | } |
127 | |
128 | void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator) |
129 | { |
130 | joint->~b2Joint(); |
131 | switch (joint->m_type) |
132 | { |
133 | case e_distanceJoint: |
134 | allocator->Free(joint, sizeof(b2DistanceJoint)); |
135 | break; |
136 | |
137 | case e_mouseJoint: |
138 | allocator->Free(joint, sizeof(b2MouseJoint)); |
139 | break; |
140 | |
141 | case e_prismaticJoint: |
142 | allocator->Free(joint, sizeof(b2PrismaticJoint)); |
143 | break; |
144 | |
145 | case e_revoluteJoint: |
146 | allocator->Free(joint, sizeof(b2RevoluteJoint)); |
147 | break; |
148 | |
149 | case e_pulleyJoint: |
150 | allocator->Free(joint, sizeof(b2PulleyJoint)); |
151 | break; |
152 | |
153 | case e_gearJoint: |
154 | allocator->Free(joint, sizeof(b2GearJoint)); |
155 | break; |
156 | |
157 | case e_wheelJoint: |
158 | allocator->Free(joint, sizeof(b2WheelJoint)); |
159 | break; |
160 | |
161 | case e_weldJoint: |
162 | allocator->Free(joint, sizeof(b2WeldJoint)); |
163 | break; |
164 | |
165 | case e_frictionJoint: |
166 | allocator->Free(joint, sizeof(b2FrictionJoint)); |
167 | break; |
168 | |
169 | case e_ropeJoint: |
170 | allocator->Free(joint, sizeof(b2RopeJoint)); |
171 | break; |
172 | |
173 | case e_motorJoint: |
174 | allocator->Free(joint, sizeof(b2MotorJoint)); |
175 | break; |
176 | |
177 | default: |
178 | b2Assert(false); |
179 | break; |
180 | } |
181 | } |
182 | |
183 | b2Joint::b2Joint(const b2JointDef* def) |
184 | { |
185 | b2Assert(def->bodyA != def->bodyB); |
186 | |
187 | m_type = def->type; |
188 | m_prev = NULL; |
189 | m_next = NULL; |
190 | m_bodyA = def->bodyA; |
191 | m_bodyB = def->bodyB; |
192 | m_index = 0; |
193 | m_collideConnected = def->collideConnected; |
194 | m_islandFlag = false; |
195 | m_userData = def->userData; |
196 | |
197 | m_edgeA.joint = NULL; |
198 | m_edgeA.other = NULL; |
199 | m_edgeA.prev = NULL; |
200 | m_edgeA.next = NULL; |
201 | |
202 | m_edgeB.joint = NULL; |
203 | m_edgeB.other = NULL; |
204 | m_edgeB.prev = NULL; |
205 | m_edgeB.next = NULL; |
206 | } |
207 | |
208 | bool b2Joint::IsActive() const |
209 | { |
210 | return m_bodyA->IsActive() && m_bodyB->IsActive(); |
211 | } |
212 | |