1 /**
2 * Copyright (c) 2006-2011 LOVE Development Team
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 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 *    claim that you wrote the original software. If you use this software
14 *    in a product, an acknowledgment in the product documentation would be
15 *    appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 *    misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20 
21 #include "Joint.h"
22 
23 // STD
24 #include <bitset>
25 
26 // Module
27 #include "Body.h"
28 #include "World.h"
29 
30 namespace love
31 {
32 namespace physics
33 {
34 namespace box2d
35 {
Joint(Body * body1)36 	Joint::Joint(Body * body1)
37 		: body1(body1), body2(0), world(body1->world)
38 	{
39 		body1->retain();
40 	}
41 
Joint(Body * body1,Body * body2)42 	Joint::Joint(Body * body1, Body * body2)
43 		: body1(body1), body2(body2), world(body1->world)
44 	{
45 		body1->retain();
46 		body2->retain();
47 	}
48 
~Joint()49 	Joint::~Joint()
50 	{
51 		if(body1 != 0)
52 			body1->release();
53 		if(body2 != 0)
54 			body2->release();
55 
56 		joint = 0;
57 	}
58 
getType() const59 	Joint::Type Joint::getType() const
60 	{
61 		switch(joint->GetType())
62 		{
63 		case e_revoluteJoint:
64 			return JOINT_REVOLUTE;
65 		case e_prismaticJoint:
66 			return JOINT_PRISMATIC;
67 		case e_distanceJoint:
68 			return JOINT_DISTANCE;
69 		case e_pulleyJoint:
70 			return JOINT_PULLEY;
71 		case e_mouseJoint:
72 			return JOINT_MOUSE;
73 		case e_gearJoint:
74 			return JOINT_GEAR;
75 		default:
76 			return JOINT_INVALID;
77 		}
78 	}
79 
getAnchors(lua_State * L)80 	int Joint::getAnchors(lua_State * L)
81 	{
82 		lua_pushnumber(L, world->scaleUp(joint->GetAnchor1().x));
83 		lua_pushnumber(L, world->scaleUp(joint->GetAnchor1().y));
84 		lua_pushnumber(L, world->scaleUp(joint->GetAnchor2().x));
85 		lua_pushnumber(L, world->scaleUp(joint->GetAnchor2().y));
86 		return 4;
87 	}
88 
getReactionForce(lua_State * L)89 	int Joint::getReactionForce(lua_State * L)
90 	{
91 		b2Vec2 v = joint->GetReactionForce();
92 		lua_pushnumber(L, v.x);
93 		lua_pushnumber(L, v.y);
94 		return 2;
95 	}
96 
getReactionTorque()97 	float Joint::getReactionTorque()
98 	{
99 		return joint->GetReactionTorque();
100 	}
101 
setCollideConnected(bool collide)102 	void Joint::setCollideConnected(bool collide)
103 	{
104 		joint->m_collideConnected = collide;
105 	}
106 
getCollideConnected() const107 	bool Joint::getCollideConnected() const
108 	{
109 		return joint->m_collideConnected;
110 	}
111 
createJoint(b2JointDef * def)112 	b2Joint * Joint::createJoint(b2JointDef * def)
113 	{
114 		joint = world->world->CreateJoint(def);
115 		return joint;
116 	}
117 
destroyJoint(b2Joint * joint)118 	void Joint::destroyJoint(b2Joint * joint)
119 	{
120 		if (joint != NULL)
121 			world->world->DestroyJoint(joint);
122 	}
123 
124 } // box2d
125 } // physics
126 } // love
127