2
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
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.
19
#ifndef COLLISION_FILTERING_H
20
#define COLLISION_FILTERING_H
22
// This is a test of collision filtering.
23
// There is a triangle, a box, and a circle.
24
// There are 6 shapes. 3 large and 3 small.
25
// The 3 small ones always collide.
26
// The 3 large ones never collide.
27
// The boxes don't collide with triangles (except if both are small).
28
const int16 k_smallGroup = 1;
29
const int16 k_largeGroup = -1;
31
const uint16 k_defaultCategory = 0x0001;
32
const uint16 k_triangleCategory = 0x0002;
33
const uint16 k_boxCategory = 0x0004;
34
const uint16 k_circleCategory = 0x0008;
36
const uint16 k_triangleMask = 0xFFFF;
37
const uint16 k_boxMask = 0xFFFF ^ k_triangleCategory;
38
const uint16 k_circleMask = 0xFFFF;
40
class CollisionFiltering : public Test
48
shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
55
b2Body* ground = m_world->CreateBody(&bd);
56
ground->CreateFixture(&sd);
61
vertices[0].Set(-1.0f, 0.0f);
62
vertices[1].Set(1.0f, 0.0f);
63
vertices[2].Set(0.0f, 2.0f);
64
b2PolygonShape polygon;
65
polygon.Set(vertices, 3);
67
b2FixtureDef triangleShapeDef;
68
triangleShapeDef.shape = &polygon;
69
triangleShapeDef.density = 1.0f;
71
triangleShapeDef.filter.groupIndex = k_smallGroup;
72
triangleShapeDef.filter.categoryBits = k_triangleCategory;
73
triangleShapeDef.filter.maskBits = k_triangleMask;
75
b2BodyDef triangleBodyDef;
76
triangleBodyDef.type = b2_dynamicBody;
77
triangleBodyDef.position.Set(-5.0f, 2.0f);
79
b2Body* body1 = m_world->CreateBody(&triangleBodyDef);
80
body1->CreateFixture(&triangleShapeDef);
82
// Large triangle (recycle definitions)
86
polygon.Set(vertices, 3);
87
triangleShapeDef.filter.groupIndex = k_largeGroup;
88
triangleBodyDef.position.Set(-5.0f, 6.0f);
89
triangleBodyDef.fixedRotation = true; // look at me!
91
b2Body* body2 = m_world->CreateBody(&triangleBodyDef);
92
body2->CreateFixture(&triangleShapeDef);
96
bd.type = b2_dynamicBody;
97
bd.position.Set(-5.0f, 10.0f);
98
b2Body* body = m_world->CreateBody(&bd);
101
p.SetAsBox(0.5f, 1.0f);
102
body->CreateFixture(&p, 1.0f);
104
b2PrismaticJointDef jd;
107
jd.enableLimit = true;
108
jd.localAnchorA.Set(0.0f, 4.0f);
109
jd.localAnchorB.SetZero();
110
jd.localAxisA.Set(0.0f, 1.0f);
111
jd.lowerTranslation = -1.0f;
112
jd.upperTranslation = 1.0f;
114
m_world->CreateJoint(&jd);
118
polygon.SetAsBox(1.0f, 0.5f);
119
b2FixtureDef boxShapeDef;
120
boxShapeDef.shape = &polygon;
121
boxShapeDef.density = 1.0f;
122
boxShapeDef.restitution = 0.1f;
124
boxShapeDef.filter.groupIndex = k_smallGroup;
125
boxShapeDef.filter.categoryBits = k_boxCategory;
126
boxShapeDef.filter.maskBits = k_boxMask;
128
b2BodyDef boxBodyDef;
129
boxBodyDef.type = b2_dynamicBody;
130
boxBodyDef.position.Set(0.0f, 2.0f);
132
b2Body* body3 = m_world->CreateBody(&boxBodyDef);
133
body3->CreateFixture(&boxShapeDef);
135
// Large box (recycle definitions)
136
polygon.SetAsBox(2.0f, 1.0f);
137
boxShapeDef.filter.groupIndex = k_largeGroup;
138
boxBodyDef.position.Set(0.0f, 6.0f);
140
b2Body* body4 = m_world->CreateBody(&boxBodyDef);
141
body4->CreateFixture(&boxShapeDef);
144
b2CircleShape circle;
145
circle.m_radius = 1.0f;
147
b2FixtureDef circleShapeDef;
148
circleShapeDef.shape = &circle;
149
circleShapeDef.density = 1.0f;
151
circleShapeDef.filter.groupIndex = k_smallGroup;
152
circleShapeDef.filter.categoryBits = k_circleCategory;
153
circleShapeDef.filter.maskBits = k_circleMask;
155
b2BodyDef circleBodyDef;
156
circleBodyDef.type = b2_dynamicBody;
157
circleBodyDef.position.Set(5.0f, 2.0f);
159
b2Body* body5 = m_world->CreateBody(&circleBodyDef);
160
body5->CreateFixture(&circleShapeDef);
163
circle.m_radius *= 2.0f;
164
circleShapeDef.filter.groupIndex = k_largeGroup;
165
circleBodyDef.position.Set(5.0f, 6.0f);
167
b2Body* body6 = m_world->CreateBody(&circleBodyDef);
168
body6->CreateFixture(&circleShapeDef);
170
static Test* Create()
172
return new CollisionFiltering;