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
|
#include "GFX.h"
#include "Timer.hpp"
#include <Box2D\Box2D.h>
#define M_PI 3.14159
void DrawBox (float x,float y,float w,float h);
void DrawCircle (float x,float y,float r);
#pragma warning (disable:4482)
class TestBody
{
public:
TestBody (bool ci,b2World* world)
{
circle=ci;
bodyDef.type = b2BodyType::b2_dynamicBody;
bodyDef.position.Set(250.0f, 200.0f);
body = world->CreateBody(&bodyDef);
b2Shape* shape;
if (circle) {
shape=new b2CircleShape ();
((b2CircleShape*)(shape))->m_radius=10.0f;
}
else {
shape=new b2PolygonShape ();
((b2PolygonShape*)(shape))->SetAsBox(10.0f,10.0f);
}
fixtureDef;
fixtureDef.shape = shape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
}
void Draw ()
{
glColor3f(1.0f,0.0f,0.0f);
float r=body->GetAngle()*(180/M_PI);
float tx=body->GetPosition().x;
float ty=body->GetPosition().y;
glTranslatef(tx,ty,0.0f);
glRotatef(r,0.0f,0.0f,1.0f);
if (circle)
DrawCircle(0.0f,0.0f,10.0f);
else
DrawBox(0.0f,0.0f,10.0f,10.0f);
glRotatef(r,0.0f,0.0f,-1.0f);
glTranslatef(-tx,-ty,0.0f);
glColor3f(1.0f,1.0f,1.0f);
}
b2BodyDef bodyDef;
b2Body* body;
b2FixtureDef fixtureDef;
bool circle;
};
int main (int argc,char* argv[])
{
GFX* gfx=new GFX ();
if (!gfx->Init ())
return -1;
bool isRunning=true;
SDL_Event e;
sdlTimer->Update ();
b2Vec2 gr (0.0f,-100.0f);
b2World* world=new b2World(gr);
b2BodyDef groundBodyDef;
groundBodyDef.type=b2BodyType::b2_staticBody;
groundBodyDef.position.Set(0.0f, 0.0f);
b2Body* groundBody = world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
groundBox.SetAsBox(500.0f, 50.0f);
groundBody->CreateFixture(&groundBox, 0.0f);
glColor3f(1.0f,1.0f,1.0f);
vector<TestBody*> bodies;
vector<TestBody*>::iterator i;
while (isRunning)
{
gfx->BeginDraw();
DrawBox (groundBody->GetPosition().x,groundBody->GetPosition().y,500.0f,50.0f);
for (i=bodies.begin();i!=bodies.end();++i)
(*i)->Draw();
gfx->EndDraw ();
if (SDL_PollEvent(&e)) {
switch (e.type)
{
case(SDL_QUIT):{isRunning=false;}break;
case(SDL_KEYDOWN):{
switch(e.key.keysym.sym)
{
case(SDLK_ESCAPE):{isRunning=false;}break;
case(SDLK_SPACE):{
bodies.push_back(new TestBody(false,world));
}break;
case(SDLK_x):{
for (i=bodies.begin();i!=bodies.end();++i)
cout << (*i)->body->GetPosition().y << endl;
}break;
}
}break;
}
}
sdlTimer->Update ();
world->Step(sdlTimer->GetElapsed(),6,2);
}
for (i=bodies.begin();i!=bodies.end();++i)
delete (*i);
delete world;
gfx->Quit ();
return 0;
}
void DrawBox (float x,float y,float w,float h)
{
glBegin(GL_QUADS);
glVertex2f(x,y);
glVertex2f(x+w,y);
glVertex2f(x+w,y+h);
glVertex2f(x,y+h);
glEnd ();
}
void DrawCircle (float x,float y,float r)
{
}
|