axmol/external/Box2D/Collision/Shapes/b2ChainShape.cpp

192 lines
4.8 KiB
C++
Raw Normal View History

2013-10-12 14:15:32 +08:00
/*
* Copyright (c) 2006-2010 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/Collision/Shapes/b2ChainShape.h>
#include <Box2D/Collision/Shapes/b2EdgeShape.h>
#include <new>
2013-11-25 14:50:11 +08:00
#include <memory.h>
2013-11-25 15:55:01 +08:00
#include <string.h>
2013-10-12 14:15:32 +08:00
b2ChainShape::~b2ChainShape()
{
2013-11-25 14:50:11 +08:00
b2Free(m_vertices);
m_vertices = NULL;
m_count = 0;
2013-10-12 14:15:32 +08:00
}
void b2ChainShape::CreateLoop(const b2Vec2* vertices, int32 count)
{
2013-11-25 14:50:11 +08:00
b2Assert(m_vertices == NULL && m_count == 0);
b2Assert(count >= 3);
for (int32 i = 1; i < count; ++i)
{
b2Vec2 v1 = vertices[i-1];
b2Vec2 v2 = vertices[i];
// If the code crashes here, it means your vertices are too close together.
b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop);
}
m_count = count + 1;
m_vertices = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2));
memcpy(m_vertices, vertices, count * sizeof(b2Vec2));
m_vertices[count] = m_vertices[0];
m_prevVertex = m_vertices[m_count - 2];
m_nextVertex = m_vertices[1];
m_hasPrevVertex = true;
m_hasNextVertex = true;
2013-10-12 14:15:32 +08:00
}
void b2ChainShape::CreateChain(const b2Vec2* vertices, int32 count)
{
2013-11-25 14:50:11 +08:00
b2Assert(m_vertices == NULL && m_count == 0);
b2Assert(count >= 2);
for (int32 i = 1; i < count; ++i)
{
b2Vec2 v1 = vertices[i-1];
b2Vec2 v2 = vertices[i];
// If the code crashes here, it means your vertices are too close together.
b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop);
}
m_count = count;
m_vertices = (b2Vec2*)b2Alloc(count * sizeof(b2Vec2));
memcpy(m_vertices, vertices, m_count * sizeof(b2Vec2));
m_hasPrevVertex = false;
m_hasNextVertex = false;
m_prevVertex.SetZero();
m_nextVertex.SetZero();
2013-10-12 14:15:32 +08:00
}
void b2ChainShape::SetPrevVertex(const b2Vec2& prevVertex)
{
2013-11-25 14:50:11 +08:00
m_prevVertex = prevVertex;
m_hasPrevVertex = true;
2013-10-12 14:15:32 +08:00
}
void b2ChainShape::SetNextVertex(const b2Vec2& nextVertex)
{
2013-11-25 14:50:11 +08:00
m_nextVertex = nextVertex;
m_hasNextVertex = true;
2013-10-12 14:15:32 +08:00
}
b2Shape* b2ChainShape::Clone(b2BlockAllocator* allocator) const
{
2013-11-25 14:50:11 +08:00
void* mem = allocator->Allocate(sizeof(b2ChainShape));
b2ChainShape* clone = new (mem) b2ChainShape;
clone->CreateChain(m_vertices, m_count);
clone->m_prevVertex = m_prevVertex;
clone->m_nextVertex = m_nextVertex;
clone->m_hasPrevVertex = m_hasPrevVertex;
clone->m_hasNextVertex = m_hasNextVertex;
return clone;
2013-10-12 14:15:32 +08:00
}
int32 b2ChainShape::GetChildCount() const
{
2013-11-25 14:50:11 +08:00
// edge count = vertex count - 1
return m_count - 1;
2013-10-12 14:15:32 +08:00
}
void b2ChainShape::GetChildEdge(b2EdgeShape* edge, int32 index) const
{
2013-11-25 14:50:11 +08:00
b2Assert(0 <= index && index < m_count - 1);
edge->m_type = b2Shape::e_edge;
edge->m_radius = m_radius;
edge->m_vertex1 = m_vertices[index + 0];
edge->m_vertex2 = m_vertices[index + 1];
if (index > 0)
{
edge->m_vertex0 = m_vertices[index - 1];
edge->m_hasVertex0 = true;
}
else
{
edge->m_vertex0 = m_prevVertex;
edge->m_hasVertex0 = m_hasPrevVertex;
}
if (index < m_count - 2)
{
edge->m_vertex3 = m_vertices[index + 2];
edge->m_hasVertex3 = true;
}
else
{
edge->m_vertex3 = m_nextVertex;
edge->m_hasVertex3 = m_hasNextVertex;
}
2013-10-12 14:15:32 +08:00
}
bool b2ChainShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
{
2013-11-25 14:50:11 +08:00
B2_NOT_USED(xf);
B2_NOT_USED(p);
return false;
2013-10-12 14:15:32 +08:00
}
bool b2ChainShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
2013-11-25 14:50:11 +08:00
const b2Transform& xf, int32 childIndex) const
2013-10-12 14:15:32 +08:00
{
2013-11-25 14:50:11 +08:00
b2Assert(childIndex < m_count);
2013-10-12 14:15:32 +08:00
2013-11-25 14:50:11 +08:00
b2EdgeShape edgeShape;
2013-10-12 14:15:32 +08:00
2013-11-25 14:50:11 +08:00
int32 i1 = childIndex;
int32 i2 = childIndex + 1;
if (i2 == m_count)
{
i2 = 0;
}
2013-10-12 14:15:32 +08:00
2013-11-25 14:50:11 +08:00
edgeShape.m_vertex1 = m_vertices[i1];
edgeShape.m_vertex2 = m_vertices[i2];
2013-10-12 14:15:32 +08:00
2013-11-25 14:50:11 +08:00
return edgeShape.RayCast(output, input, xf, 0);
2013-10-12 14:15:32 +08:00
}
void b2ChainShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
{
2013-11-25 14:50:11 +08:00
b2Assert(childIndex < m_count);
2013-10-12 14:15:32 +08:00
2013-11-25 14:50:11 +08:00
int32 i1 = childIndex;
int32 i2 = childIndex + 1;
if (i2 == m_count)
{
i2 = 0;
}
2013-10-12 14:15:32 +08:00
2013-11-25 14:50:11 +08:00
b2Vec2 v1 = b2Mul(xf, m_vertices[i1]);
b2Vec2 v2 = b2Mul(xf, m_vertices[i2]);
2013-10-12 14:15:32 +08:00
2013-11-25 14:50:11 +08:00
aabb->lowerBound = b2Min(v1, v2);
aabb->upperBound = b2Max(v1, v2);
2013-10-12 14:15:32 +08:00
}
void b2ChainShape::ComputeMass(b2MassData* massData, float32 density) const
{
2013-11-25 14:50:11 +08:00
B2_NOT_USED(density);
2013-10-12 14:15:32 +08:00
2013-11-25 14:50:11 +08:00
massData->mass = 0.0f;
massData->center.SetZero();
massData->I = 0.0f;
2013-10-12 14:15:32 +08:00
}