diff options
author | chai <chaifix@163.com> | 2020-09-10 20:30:31 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-09-10 20:30:31 +0800 |
commit | 639b34294ffc20721c66db46e59e07d9100ac4b8 (patch) | |
tree | 7e1d45b536fa35e9f1559e468ea66fca99524573 /ThirdParty/Box2D/Dynamics/Joints/b2Joint.cpp |
*init
Diffstat (limited to 'ThirdParty/Box2D/Dynamics/Joints/b2Joint.cpp')
-rw-r--r-- | ThirdParty/Box2D/Dynamics/Joints/b2Joint.cpp | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/ThirdParty/Box2D/Dynamics/Joints/b2Joint.cpp b/ThirdParty/Box2D/Dynamics/Joints/b2Joint.cpp new file mode 100644 index 0000000..8103b01 --- /dev/null +++ b/ThirdParty/Box2D/Dynamics/Joints/b2Joint.cpp @@ -0,0 +1,211 @@ +/* +* Copyright (c) 2006-2007 Erin Catto http://www.box2d.org +* +* This software is provided 'as-is', without any express or implied +* warranty. In no event will the authors be held liable for any damages +* arising from the use of this software. +* Permission is granted to anyone to use this software for any purpose, +* including commercial applications, and to alter it and redistribute it +* freely, subject to the following restrictions: +* 1. The origin of this software must not be misrepresented; you must not +* claim that you wrote the original software. If you use this software +* in a product, an acknowledgment in the product documentation would be +* appreciated but is not required. +* 2. Altered source versions must be plainly marked as such, and must not be +* misrepresented as being the original software. +* 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "Box2D/Dynamics/Joints/b2Joint.h" +#include "Box2D/Dynamics/Joints/b2DistanceJoint.h" +#include "Box2D/Dynamics/Joints/b2WheelJoint.h" +#include "Box2D/Dynamics/Joints/b2MouseJoint.h" +#include "Box2D/Dynamics/Joints/b2RevoluteJoint.h" +#include "Box2D/Dynamics/Joints/b2PrismaticJoint.h" +#include "Box2D/Dynamics/Joints/b2PulleyJoint.h" +#include "Box2D/Dynamics/Joints/b2GearJoint.h" +#include "Box2D/Dynamics/Joints/b2WeldJoint.h" +#include "Box2D/Dynamics/Joints/b2FrictionJoint.h" +#include "Box2D/Dynamics/Joints/b2RopeJoint.h" +#include "Box2D/Dynamics/Joints/b2MotorJoint.h" +#include "Box2D/Dynamics/b2Body.h" +#include "Box2D/Dynamics/b2World.h" +#include "Box2D/Common/b2BlockAllocator.h" + +#include <new> + +b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator) +{ + b2Joint* joint = nullptr; + + switch (def->type) + { + case e_distanceJoint: + { + void* mem = allocator->Allocate(sizeof(b2DistanceJoint)); + joint = new (mem) b2DistanceJoint(static_cast<const b2DistanceJointDef*>(def)); + } + break; + + case e_mouseJoint: + { + void* mem = allocator->Allocate(sizeof(b2MouseJoint)); + joint = new (mem) b2MouseJoint(static_cast<const b2MouseJointDef*>(def)); + } + break; + + case e_prismaticJoint: + { + void* mem = allocator->Allocate(sizeof(b2PrismaticJoint)); + joint = new (mem) b2PrismaticJoint(static_cast<const b2PrismaticJointDef*>(def)); + } + break; + + case e_revoluteJoint: + { + void* mem = allocator->Allocate(sizeof(b2RevoluteJoint)); + joint = new (mem) b2RevoluteJoint(static_cast<const b2RevoluteJointDef*>(def)); + } + break; + + case e_pulleyJoint: + { + void* mem = allocator->Allocate(sizeof(b2PulleyJoint)); + joint = new (mem) b2PulleyJoint(static_cast<const b2PulleyJointDef*>(def)); + } + break; + + case e_gearJoint: + { + void* mem = allocator->Allocate(sizeof(b2GearJoint)); + joint = new (mem) b2GearJoint(static_cast<const b2GearJointDef*>(def)); + } + break; + + case e_wheelJoint: + { + void* mem = allocator->Allocate(sizeof(b2WheelJoint)); + joint = new (mem) b2WheelJoint(static_cast<const b2WheelJointDef*>(def)); + } + break; + + case e_weldJoint: + { + void* mem = allocator->Allocate(sizeof(b2WeldJoint)); + joint = new (mem) b2WeldJoint(static_cast<const b2WeldJointDef*>(def)); + } + break; + + case e_frictionJoint: + { + void* mem = allocator->Allocate(sizeof(b2FrictionJoint)); + joint = new (mem) b2FrictionJoint(static_cast<const b2FrictionJointDef*>(def)); + } + break; + + case e_ropeJoint: + { + void* mem = allocator->Allocate(sizeof(b2RopeJoint)); + joint = new (mem) b2RopeJoint(static_cast<const b2RopeJointDef*>(def)); + } + break; + + case e_motorJoint: + { + void* mem = allocator->Allocate(sizeof(b2MotorJoint)); + joint = new (mem) b2MotorJoint(static_cast<const b2MotorJointDef*>(def)); + } + break; + + default: + b2Assert(false); + break; + } + + return joint; +} + +void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator) +{ + joint->~b2Joint(); + switch (joint->m_type) + { + case e_distanceJoint: + allocator->Free(joint, sizeof(b2DistanceJoint)); + break; + + case e_mouseJoint: + allocator->Free(joint, sizeof(b2MouseJoint)); + break; + + case e_prismaticJoint: + allocator->Free(joint, sizeof(b2PrismaticJoint)); + break; + + case e_revoluteJoint: + allocator->Free(joint, sizeof(b2RevoluteJoint)); + break; + + case e_pulleyJoint: + allocator->Free(joint, sizeof(b2PulleyJoint)); + break; + + case e_gearJoint: + allocator->Free(joint, sizeof(b2GearJoint)); + break; + + case e_wheelJoint: + allocator->Free(joint, sizeof(b2WheelJoint)); + break; + + case e_weldJoint: + allocator->Free(joint, sizeof(b2WeldJoint)); + break; + + case e_frictionJoint: + allocator->Free(joint, sizeof(b2FrictionJoint)); + break; + + case e_ropeJoint: + allocator->Free(joint, sizeof(b2RopeJoint)); + break; + + case e_motorJoint: + allocator->Free(joint, sizeof(b2MotorJoint)); + break; + + default: + b2Assert(false); + break; + } +} + +b2Joint::b2Joint(const b2JointDef* def) +{ + b2Assert(def->bodyA != def->bodyB); + + m_type = def->type; + m_prev = nullptr; + m_next = nullptr; + m_bodyA = def->bodyA; + m_bodyB = def->bodyB; + m_index = 0; + m_collideConnected = def->collideConnected; + m_islandFlag = false; + m_userData = def->userData; + + m_edgeA.joint = nullptr; + m_edgeA.other = nullptr; + m_edgeA.prev = nullptr; + m_edgeA.next = nullptr; + + m_edgeB.joint = nullptr; + m_edgeB.other = nullptr; + m_edgeB.prev = nullptr; + m_edgeB.next = nullptr; +} + +bool b2Joint::IsActive() const +{ + return m_bodyA->IsActive() && m_bodyB->IsActive(); +} |