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

« back to all changes in this revision

Viewing changes to tests/box2d/Testbed/Tests/BodyTypes.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 BODY_TYPES_H
 
20
#define BODY_TYPES_H
 
21
 
 
22
class BodyTypes : public Test
 
23
{
 
24
public:
 
25
        BodyTypes()
 
26
        {
 
27
                b2Body* ground = NULL;
 
28
                {
 
29
                        b2BodyDef bd;
 
30
                        ground = m_world->CreateBody(&bd);
 
31
 
 
32
                        b2EdgeShape shape;
 
33
                        shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
 
34
 
 
35
                        b2FixtureDef fd;
 
36
                        fd.shape = &shape;
 
37
 
 
38
                        ground->CreateFixture(&fd);
 
39
                }
 
40
 
 
41
                // Define attachment
 
42
                {
 
43
                        b2BodyDef bd;
 
44
                        bd.type = b2_dynamicBody;
 
45
                        bd.position.Set(0.0f, 3.0f);
 
46
                        m_attachment = m_world->CreateBody(&bd);
 
47
 
 
48
                        b2PolygonShape shape;
 
49
                        shape.SetAsBox(0.5f, 2.0f);
 
50
                        m_attachment->CreateFixture(&shape, 2.0f);
 
51
                }
 
52
 
 
53
                // Define platform
 
54
                {
 
55
                        b2BodyDef bd;
 
56
                        bd.type = b2_dynamicBody;
 
57
                        bd.position.Set(-4.0f, 5.0f);
 
58
                        m_platform = m_world->CreateBody(&bd);
 
59
 
 
60
                        b2PolygonShape shape;
 
61
                        shape.SetAsBox(0.5f, 4.0f, b2Vec2(4.0f, 0.0f), 0.5f * b2_pi);
 
62
 
 
63
                        b2FixtureDef fd;
 
64
                        fd.shape = &shape;
 
65
                        fd.friction = 0.6f;
 
66
                        fd.density = 2.0f;
 
67
                        m_platform->CreateFixture(&fd);
 
68
 
 
69
                        b2RevoluteJointDef rjd;
 
70
                        rjd.Initialize(m_attachment, m_platform, b2Vec2(0.0f, 5.0f));
 
71
                        rjd.maxMotorTorque = 50.0f;
 
72
                        rjd.enableMotor = true;
 
73
                        m_world->CreateJoint(&rjd);
 
74
 
 
75
                        b2PrismaticJointDef pjd;
 
76
                        pjd.Initialize(ground, m_platform, b2Vec2(0.0f, 5.0f), b2Vec2(1.0f, 0.0f));
 
77
 
 
78
                        pjd.maxMotorForce = 1000.0f;
 
79
                        pjd.enableMotor = true;
 
80
                        pjd.lowerTranslation = -10.0f;
 
81
                        pjd.upperTranslation = 10.0f;
 
82
                        pjd.enableLimit = true;
 
83
 
 
84
                        m_world->CreateJoint(&pjd);
 
85
 
 
86
                        m_speed = 3.0f;
 
87
                }
 
88
 
 
89
                // Create a payload
 
90
                {
 
91
                        b2BodyDef bd;
 
92
                        bd.type = b2_dynamicBody;
 
93
                        bd.position.Set(0.0f, 8.0f);
 
94
                        b2Body* body = m_world->CreateBody(&bd);
 
95
 
 
96
                        b2PolygonShape shape;
 
97
                        shape.SetAsBox(0.75f, 0.75f);
 
98
 
 
99
                        b2FixtureDef fd;
 
100
                        fd.shape = &shape;
 
101
                        fd.friction = 0.6f;
 
102
                        fd.density = 2.0f;
 
103
 
 
104
                        body->CreateFixture(&fd);
 
105
                }
 
106
        }
 
107
 
 
108
        void Keyboard(unsigned char key)
 
109
        {
 
110
                switch (key)
 
111
                {
 
112
                case 'd':
 
113
                        m_platform->SetType(b2_dynamicBody);
 
114
                        break;
 
115
 
 
116
                case 's':
 
117
                        m_platform->SetType(b2_staticBody);
 
118
                        break;
 
119
 
 
120
                case 'k':
 
121
                        m_platform->SetType(b2_kinematicBody);
 
122
                        m_platform->SetLinearVelocity(b2Vec2(-m_speed, 0.0f));
 
123
                        m_platform->SetAngularVelocity(0.0f);
 
124
                        break;
 
125
                }
 
126
        }
 
127
 
 
128
        void Step(Settings* settings)
 
129
        {
 
130
                // Drive the kinematic body.
 
131
                if (m_platform->GetType() == b2_kinematicBody)
 
132
                {
 
133
                        b2Vec2 p = m_platform->GetTransform().p;
 
134
                        b2Vec2 v = m_platform->GetLinearVelocity();
 
135
 
 
136
                        if ((p.x < -10.0f && v.x < 0.0f) ||
 
137
                                (p.x > 10.0f && v.x > 0.0f))
 
138
                        {
 
139
                                v.x = -v.x;
 
140
                                m_platform->SetLinearVelocity(v);
 
141
                        }
 
142
                }
 
143
 
 
144
                Test::Step(settings);
 
145
                m_debugDraw.DrawString(5, m_textLine, "Keys: (d) dynamic, (s) static, (k) kinematic");
 
146
                m_textLine += 15;
 
147
        }
 
148
 
 
149
        static Test* Create()
 
150
        {
 
151
                return new BodyTypes;
 
152
        }
 
153
 
 
154
        b2Body* m_attachment;
 
155
        b2Body* m_platform;
 
156
        float32 m_speed;
 
157
};
 
158
 
 
159
#endif