~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/box2d/Testbed/Tests/CollisionFiltering.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
 
3
*
 
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.
 
17
*/
 
18
 
 
19
#ifndef COLLISION_FILTERING_H
 
20
#define COLLISION_FILTERING_H
 
21
 
 
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;
 
30
 
 
31
const uint16 k_defaultCategory = 0x0001;
 
32
const uint16 k_triangleCategory = 0x0002;
 
33
const uint16 k_boxCategory = 0x0004;
 
34
const uint16 k_circleCategory = 0x0008;
 
35
 
 
36
const uint16 k_triangleMask = 0xFFFF;
 
37
const uint16 k_boxMask = 0xFFFF ^ k_triangleCategory;
 
38
const uint16 k_circleMask = 0xFFFF;
 
39
 
 
40
class CollisionFiltering : public Test
 
41
{
 
42
public:
 
43
        CollisionFiltering()
 
44
        {
 
45
                // Ground body
 
46
                {
 
47
                        b2EdgeShape shape;
 
48
                        shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
 
49
 
 
50
                        b2FixtureDef sd;
 
51
                        sd.shape = &shape;
 
52
                        sd.friction = 0.3f;
 
53
 
 
54
                        b2BodyDef bd;
 
55
                        b2Body* ground = m_world->CreateBody(&bd);
 
56
                        ground->CreateFixture(&sd);
 
57
                }
 
58
 
 
59
                // Small triangle
 
60
                b2Vec2 vertices[3];
 
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);
 
66
 
 
67
                b2FixtureDef triangleShapeDef;
 
68
                triangleShapeDef.shape = &polygon;
 
69
                triangleShapeDef.density = 1.0f;
 
70
 
 
71
                triangleShapeDef.filter.groupIndex = k_smallGroup;
 
72
                triangleShapeDef.filter.categoryBits = k_triangleCategory;
 
73
                triangleShapeDef.filter.maskBits = k_triangleMask;
 
74
 
 
75
                b2BodyDef triangleBodyDef;
 
76
                triangleBodyDef.type = b2_dynamicBody;
 
77
                triangleBodyDef.position.Set(-5.0f, 2.0f);
 
78
 
 
79
                b2Body* body1 = m_world->CreateBody(&triangleBodyDef);
 
80
                body1->CreateFixture(&triangleShapeDef);
 
81
 
 
82
                // Large triangle (recycle definitions)
 
83
                vertices[0] *= 2.0f;
 
84
                vertices[1] *= 2.0f;
 
85
                vertices[2] *= 2.0f;
 
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!
 
90
 
 
91
                b2Body* body2 = m_world->CreateBody(&triangleBodyDef);
 
92
                body2->CreateFixture(&triangleShapeDef);
 
93
 
 
94
                {
 
95
                        b2BodyDef bd;
 
96
                        bd.type = b2_dynamicBody;
 
97
                        bd.position.Set(-5.0f, 10.0f);
 
98
                        b2Body* body = m_world->CreateBody(&bd);
 
99
 
 
100
                        b2PolygonShape p;
 
101
                        p.SetAsBox(0.5f, 1.0f);
 
102
                        body->CreateFixture(&p, 1.0f);
 
103
 
 
104
                        b2PrismaticJointDef jd;
 
105
                        jd.bodyA = body2;
 
106
                        jd.bodyB = body;
 
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;
 
113
 
 
114
                        m_world->CreateJoint(&jd);
 
115
                }
 
116
 
 
117
                // Small box
 
118
                polygon.SetAsBox(1.0f, 0.5f);
 
119
                b2FixtureDef boxShapeDef;
 
120
                boxShapeDef.shape = &polygon;
 
121
                boxShapeDef.density = 1.0f;
 
122
                boxShapeDef.restitution = 0.1f;
 
123
 
 
124
                boxShapeDef.filter.groupIndex = k_smallGroup;
 
125
                boxShapeDef.filter.categoryBits = k_boxCategory;
 
126
                boxShapeDef.filter.maskBits = k_boxMask;
 
127
 
 
128
                b2BodyDef boxBodyDef;
 
129
                boxBodyDef.type = b2_dynamicBody;
 
130
                boxBodyDef.position.Set(0.0f, 2.0f);
 
131
 
 
132
                b2Body* body3 = m_world->CreateBody(&boxBodyDef);
 
133
                body3->CreateFixture(&boxShapeDef);
 
134
 
 
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);
 
139
 
 
140
                b2Body* body4 = m_world->CreateBody(&boxBodyDef);
 
141
                body4->CreateFixture(&boxShapeDef);
 
142
 
 
143
                // Small circle
 
144
                b2CircleShape circle;
 
145
                circle.m_radius = 1.0f;
 
146
 
 
147
                b2FixtureDef circleShapeDef;
 
148
                circleShapeDef.shape = &circle;
 
149
                circleShapeDef.density = 1.0f;
 
150
 
 
151
                circleShapeDef.filter.groupIndex = k_smallGroup;
 
152
                circleShapeDef.filter.categoryBits = k_circleCategory;
 
153
                circleShapeDef.filter.maskBits = k_circleMask;
 
154
 
 
155
                b2BodyDef circleBodyDef;
 
156
                circleBodyDef.type = b2_dynamicBody;
 
157
                circleBodyDef.position.Set(5.0f, 2.0f);
 
158
                
 
159
                b2Body* body5 = m_world->CreateBody(&circleBodyDef);
 
160
                body5->CreateFixture(&circleShapeDef);
 
161
 
 
162
                // Large circle
 
163
                circle.m_radius *= 2.0f;
 
164
                circleShapeDef.filter.groupIndex = k_largeGroup;
 
165
                circleBodyDef.position.Set(5.0f, 6.0f);
 
166
 
 
167
                b2Body* body6 = m_world->CreateBody(&circleBodyDef);
 
168
                body6->CreateFixture(&circleShapeDef);
 
169
        }
 
170
        static Test* Create()
 
171
        {
 
172
                return new CollisionFiltering;
 
173
        }
 
174
};
 
175
 
 
176
#endif