2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
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.
20 #include "b2CircleShape.h"
21 #include "b2PolygonShape.h"
22 #include "../b2Collision.h"
23 #include "../b2BroadPhase.h"
24 #include "../../Common/b2BlockAllocator.h"
28 b2Shape* b2Shape::Create(const b2ShapeDef* def, b2BlockAllocator* allocator)
34 void* mem = allocator->Allocate(sizeof(b2CircleShape));
35 return new (mem) b2CircleShape(def);
40 void* mem = allocator->Allocate(sizeof(b2PolygonShape));
41 return new (mem) b2PolygonShape(def);
50 void b2Shape::Destroy(b2Shape* s, b2BlockAllocator* allocator)
56 allocator->Free(s, sizeof(b2CircleShape));
61 allocator->Free(s, sizeof(b2PolygonShape));
69 b2Shape::b2Shape(const b2ShapeDef* def)
71 m_userData = def->userData;
72 m_friction = def->friction;
73 m_restitution = def->restitution;
74 m_density = def->density;
80 m_proxyId = b2_nullProxy;
82 m_filter = def->filter;
84 m_isSensor = def->isSensor;
89 b2Assert(m_proxyId == b2_nullProxy);
92 void b2Shape::CreateProxy(b2BroadPhase* broadPhase, const b2XForm& transform)
94 b2Assert(m_proxyId == b2_nullProxy);
97 ComputeAABB(&aabb, transform);
99 bool inRange = broadPhase->InRange(aabb);
101 // You are creating a shape outside the world box.
106 m_proxyId = broadPhase->CreateProxy(aabb, this);
110 m_proxyId = b2_nullProxy;
114 void b2Shape::DestroyProxy(b2BroadPhase* broadPhase)
116 if (m_proxyId != b2_nullProxy)
118 broadPhase->DestroyProxy(m_proxyId);
119 m_proxyId = b2_nullProxy;
123 bool b2Shape::Synchronize(b2BroadPhase* broadPhase, const b2XForm& transform1, const b2XForm& transform2)
125 if (m_proxyId == b2_nullProxy)
130 // Compute an AABB that covers the swept shape (may miss some rotation effect).
132 ComputeSweptAABB(&aabb, transform1, transform2);
134 if (broadPhase->InRange(aabb))
136 broadPhase->MoveProxy(m_proxyId, aabb);
145 void b2Shape::RefilterProxy(b2BroadPhase* broadPhase, const b2XForm& transform)
147 if (m_proxyId == b2_nullProxy)
152 broadPhase->DestroyProxy(m_proxyId);
155 ComputeAABB(&aabb, transform);
157 bool inRange = broadPhase->InRange(aabb);
161 m_proxyId = broadPhase->CreateProxy(aabb, this);
165 m_proxyId = b2_nullProxy;