File: b2EdgeShape.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 (222 lines) | stat: -rw-r--r-- 5,468 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* 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_EDGE_SHAPE_H
#define B2_EDGE_SHAPE_H

#include "b2Shape.h"

/// This structure is used to build edge shapes.
struct b2EdgeChainDef : public b2ShapeDef
{
	b2EdgeChainDef()
	{
		type = e_edgeShape;
		vertexCount = 0;
		isALoop = true;
		vertices = NULL;
	}
	/// The vertices in local coordinates. You must manage the memory
	/// of this array on your own, outside of Box2D. 
	b2Vec2* vertices;
	
	/// The number of vertices in the chain. 
	int32 vertexCount;
	
	/// Whether to create an extra edge between the first and last vertices:
	bool isALoop;
};

/// The edge shape.
class b2EdgeShape : 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;

	/// @warning This only gives a consistent and sensible answer when when summed over a body only contains loops of edges
	/// @see b2Shape::ComputeSubmergedArea
	float32 ComputeSubmergedArea(	const b2Vec2& normal,
									float32 offset,
									const b2XForm& xf, 
									b2Vec2* c) const;
	
	/// Linear distance from vertex1 to vertex2:
	float32 GetLength() const;

	/// Local position of vertex in parent body
	const b2Vec2& GetVertex1() const;

	/// Local position of vertex in parent body
	const b2Vec2& GetVertex2() const;

	/// "Core" vertex with TOI slop for b2Distance functions:
	const b2Vec2& GetCoreVertex1() const;

	/// "Core" vertex with TOI slop for b2Distance functions:
	const b2Vec2& GetCoreVertex2() const;
	
	/// Perpendicular unit vector point, pointing from the solid side to the empty side: 
	const b2Vec2& GetNormalVector() const;
	
	/// Parallel unit vector, pointing from vertex1 to vertex2:
	const b2Vec2& GetDirectionVector() const;
	
	const b2Vec2& GetCorner1Vector() const;
	
	const b2Vec2& GetCorner2Vector() const;
	
	bool Corner1IsConvex() const;
	
	bool Corner2IsConvex() const;

	b2Vec2 GetFirstVertex(const b2XForm& xf) const;

	b2Vec2 Support(const b2XForm& xf, const b2Vec2& d) const;
	
	/// Get the next edge in the chain.
	b2EdgeShape* GetNextEdge() const;
	
	/// Get the previous edge in the chain.
	b2EdgeShape* GetPrevEdge() const;

	void SetPrevEdge(b2EdgeShape* edge, const b2Vec2& core, const b2Vec2& cornerDir, bool convex);
	void SetNextEdge(b2EdgeShape* edge, const b2Vec2& core, const b2Vec2& cornerDir, bool convex);
	
private:

	friend class b2Shape;
	friend class b2Body;

	b2EdgeShape(const b2Vec2& v1, const b2Vec2& v2, const b2ShapeDef* def);

	void UpdateSweepRadius(const b2Vec2& center);

	b2Vec2 m_v1;
	b2Vec2 m_v2;
	
	b2Vec2 m_coreV1;
	b2Vec2 m_coreV2;
	
	float32 m_length;
	
	b2Vec2 m_normal;
	
	b2Vec2 m_direction;
	
	// Unit vector halfway between m_direction and m_prevEdge.m_direction:
	b2Vec2 m_cornerDir1;
	
	// Unit vector halfway between m_direction and m_nextEdge.m_direction:
	b2Vec2 m_cornerDir2;
	
	bool m_cornerConvex1;
	bool m_cornerConvex2;
	
	b2EdgeShape* m_nextEdge;
	b2EdgeShape* m_prevEdge;
};

inline float32 b2EdgeShape::GetLength() const
{
	return m_length;
}

inline const b2Vec2& b2EdgeShape::GetVertex1() const
{
	return m_v1;
}

inline const b2Vec2& b2EdgeShape::GetVertex2() const
{
	return m_v2;
}

inline const b2Vec2& b2EdgeShape::GetCoreVertex1() const
{
	return m_coreV1;
}

inline const b2Vec2& b2EdgeShape::GetCoreVertex2() const
{
	return m_coreV2;
}

inline const b2Vec2& b2EdgeShape::GetNormalVector() const
{
	return m_normal;
}

inline const b2Vec2& b2EdgeShape::GetDirectionVector() const
{
	return m_direction;
}

inline const b2Vec2& b2EdgeShape::GetCorner1Vector() const
{
	return m_cornerDir1;
}

inline const b2Vec2& b2EdgeShape::GetCorner2Vector() const
{
	return m_cornerDir2;
}

inline b2EdgeShape* b2EdgeShape::GetNextEdge() const
{
	return m_nextEdge;
}

inline b2EdgeShape* b2EdgeShape::GetPrevEdge() const
{
	return m_prevEdge;
}

inline b2Vec2 b2EdgeShape::GetFirstVertex(const b2XForm& xf) const
{
	return b2Mul(xf, m_coreV1);
}

inline bool b2EdgeShape::Corner1IsConvex() const
{
	return m_cornerConvex1;
}

inline bool b2EdgeShape::Corner2IsConvex() const
{
	return m_cornerConvex2;
}
#endif