1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
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 "b2Joint.h"
20 #include "b2DistanceJoint.h"
21 #include "b2MouseJoint.h"
22 #include "b2RevoluteJoint.h"
23 #include "b2PrismaticJoint.h"
24 #include "b2PulleyJoint.h"
25 #include "b2GearJoint.h"
26 #include "../b2Body.h"
27 #include "../b2World.h"
28 #include "../../Common/b2BlockAllocator.h"
29 #include "../../Collision/b2BroadPhase.h"
30 
31 #include <new>
32 
Create(const b2JointDef * def,b2BlockAllocator * allocator)33 b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
34 {
35 	b2Joint* joint = NULL;
36 
37 	switch (def->type)
38 	{
39 	case e_distanceJoint:
40 		{
41 			void* mem = allocator->Allocate(sizeof(b2DistanceJoint));
42 			joint = new (mem) b2DistanceJoint((b2DistanceJointDef*)def);
43 		}
44 		break;
45 
46 	case e_mouseJoint:
47 		{
48 			void* mem = allocator->Allocate(sizeof(b2MouseJoint));
49 			joint = new (mem) b2MouseJoint((b2MouseJointDef*)def);
50 		}
51 		break;
52 
53 	case e_prismaticJoint:
54 		{
55 			void* mem = allocator->Allocate(sizeof(b2PrismaticJoint));
56 			joint = new (mem) b2PrismaticJoint((b2PrismaticJointDef*)def);
57 		}
58 		break;
59 
60 	case e_revoluteJoint:
61 		{
62 			void* mem = allocator->Allocate(sizeof(b2RevoluteJoint));
63 			joint = new (mem) b2RevoluteJoint((b2RevoluteJointDef*)def);
64 		}
65 		break;
66 
67 	case e_pulleyJoint:
68 		{
69 			void* mem = allocator->Allocate(sizeof(b2PulleyJoint));
70 			joint = new (mem) b2PulleyJoint((b2PulleyJointDef*)def);
71 		}
72 		break;
73 
74 	case e_gearJoint:
75 		{
76 			void* mem = allocator->Allocate(sizeof(b2GearJoint));
77 			joint = new (mem) b2GearJoint((b2GearJointDef*)def);
78 		}
79 		break;
80 
81 	default:
82 		b2Assert(false);
83 		break;
84 	}
85 
86 	return joint;
87 }
88 
Destroy(b2Joint * joint,b2BlockAllocator * allocator)89 void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
90 {
91 	joint->~b2Joint();
92 	switch (joint->m_type)
93 	{
94 	case e_distanceJoint:
95 		allocator->Free(joint, sizeof(b2DistanceJoint));
96 		break;
97 
98 	case e_mouseJoint:
99 		allocator->Free(joint, sizeof(b2MouseJoint));
100 		break;
101 
102 	case e_prismaticJoint:
103 		allocator->Free(joint, sizeof(b2PrismaticJoint));
104 		break;
105 
106 	case e_revoluteJoint:
107 		allocator->Free(joint, sizeof(b2RevoluteJoint));
108 		break;
109 
110 	case e_pulleyJoint:
111 		allocator->Free(joint, sizeof(b2PulleyJoint));
112 		break;
113 
114 	case e_gearJoint:
115 		allocator->Free(joint, sizeof(b2GearJoint));
116 		break;
117 
118 	default:
119 		b2Assert(false);
120 		break;
121 	}
122 }
123 
b2Joint(const b2JointDef * def)124 b2Joint::b2Joint(const b2JointDef* def)
125 {
126 	m_type = def->type;
127 	m_prev = NULL;
128 	m_next = NULL;
129 	m_body1 = def->body1;
130 	m_body2 = def->body2;
131 	m_collideConnected = def->collideConnected;
132 	m_islandFlag = false;
133 	m_userData = def->userData;
134 }
135