File: b2PolygonShape.h

package info (click to toggle)
python-box2d 2.0.2%2Bsvn20100109.244-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 2,864 kB
  • ctags: 3,280
  • sloc: cpp: 11,679; python: 10,103; xml: 477; makefile: 85; sh: 8
file content (169 lines) | stat: -rw-r--r-- 4,800 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* 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.
*/

#ifndef B2_POLYGON_SHAPE_H
#define B2_POLYGON_SHAPE_H

#include "b2Shape.h"

/// Convex polygon. The vertices must be in CCW order for a right-handed
/// coordinate system with the z-axis coming out of the screen.
struct b2PolygonDef : public b2ShapeDef
{
	b2PolygonDef()
	{
		type = e_polygonShape;
		vertexCount = 0;
	}

	/// Build vertices to represent an axis-aligned box.
	/// @param hx the half-width.
	/// @param hy the half-height.
	void SetAsBox(float32 hx, float32 hy);

	/// Build vertices to represent an oriented box.
	/// @param hx the half-width.
	/// @param hy the half-height.
	/// @param center the center of the box in local coordinates.
	/// @param angle the rotation of the box in local coordinates.
	void SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle);

	/// The polygon vertices in local coordinates.
	b2Vec2 vertices[b2_maxPolygonVertices];

	/// The number of polygon vertices.
	int32 vertexCount;
};


/// A convex polygon.
class b2PolygonShape : public b2Shape
{
public:
	/// @see b2Shape::TestPoint
	bool TestPoint(const b2XForm& transform, const b2Vec2& p) const;

	/// @see b2Shape::TestSegment
	b2SegmentCollide TestSegment(	const b2XForm& transform,
		float32* lambda,
		b2Vec2* normal,
		const b2Segment& segment,
		float32 maxLambda) const;

	/// @see b2Shape::ComputeAABB
	void ComputeAABB(b2AABB* aabb, const b2XForm& transform) const;

	/// @see b2Shape::ComputeSweptAABB
	void ComputeSweptAABB(	b2AABB* aabb,
		const b2XForm& transform1,
		const b2XForm& transform2) const;

	/// @see b2Shape::ComputeMass
	void ComputeMass(b2MassData* massData) const;

	/// @see b2Shape::ComputeSubmergedArea
	float32 ComputeSubmergedArea(	const b2Vec2& normal,
									float32 offset,
									const b2XForm& xf, 
									b2Vec2* c) const;

	/// Get the oriented bounding box relative to the parent body.
	const b2OBB& GetOBB() const;

	/// Get local centroid relative to the parent body.
	const b2Vec2& GetCentroid() const;

	/// Get the vertex count.
	int32 GetVertexCount() const;

	/// Get the vertices in local coordinates.
	const b2Vec2* GetVertices() const;

	/// Get the core vertices in local coordinates. These vertices
	/// represent a smaller polygon that is used for time of impact
	/// computations.
	const b2Vec2* GetCoreVertices() const;

	/// Get the edge normal vectors. There is one for each vertex.
	const b2Vec2* GetNormals() const;

	/// Get the first vertex and apply the supplied transform.
	b2Vec2 GetFirstVertex(const b2XForm& xf) const;

	/// Get the centroid and apply the supplied transform.
	b2Vec2 Centroid(const b2XForm& xf) const;

	/// Get the support point in the given world direction.
	/// Use the supplied transform.
	b2Vec2 Support(const b2XForm& xf, const b2Vec2& d) const;

private:

	friend class b2Shape;

	b2PolygonShape(const b2ShapeDef* def);

	void UpdateSweepRadius(const b2Vec2& center);

	// Local position of the polygon centroid.
	b2Vec2 m_centroid;

	b2OBB m_obb;

	b2Vec2 m_vertices[b2_maxPolygonVertices];
	b2Vec2 m_normals[b2_maxPolygonVertices];
	b2Vec2 m_coreVertices[b2_maxPolygonVertices];
	int32 m_vertexCount;
};

inline b2Vec2 b2PolygonShape::GetFirstVertex(const b2XForm& xf) const
{
	return b2Mul(xf, m_coreVertices[0]);
}

inline const b2OBB& b2PolygonShape::GetOBB() const
{
	return m_obb;
}

inline const b2Vec2& b2PolygonShape::GetCentroid() const
{
	return m_centroid;
}

inline int32 b2PolygonShape::GetVertexCount() const
{
	return m_vertexCount;
}

inline const b2Vec2* b2PolygonShape::GetVertices() const
{
	return m_vertices;
}

inline const b2Vec2* b2PolygonShape::GetCoreVertices() const
{
	return m_coreVertices;
}

inline const b2Vec2* b2PolygonShape::GetNormals() const
{
	return m_normals;
}

#endif