/
pushbox.cpp
146 lines (109 loc) · 3.04 KB
/
pushbox.cpp
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
#include <stdio.h>
#include <GL/gl.h>
#include "pushbox.hpp"
using namespace Microcosm;
inline b2Vec2 rad2vec(float r) {
return b2Vec2(cos(r), sin(r));
}
inline float vec2rad(b2Vec2 v) {
return atan2(v.y, v.x);
}
PushBox::PushBox(b2World* world, float x, float y)
: world(world),
mainThrust(200.0f),
turnThrust(100000.0f) {
maxSpeed = 80;
position.x = x;
position.y = y;
angle = 90 * DEG2RAD;
b2BodyDef *bodyDef = new b2BodyDef();
bodyDef->type = b2_dynamicBody;
bodyDef->position.Set(x, y);
bodyDef->angle = angle;
bodyDef->linearDamping = 0;
bodyDef->angularDamping = 0;
body = world->CreateBody(bodyDef);
b2PolygonShape *dynamicBox = new b2PolygonShape();
dynamicBox->SetAsBox(10.f, 10.f);
b2FixtureDef *fixtureDef = new b2FixtureDef();
fixtureDef->shape = dynamicBox;
fixtureDef->density = 1.0f;
body->CreateFixture(fixtureDef);
mainThrust /= body->GetMass();
}
void PushBox::update() {
engine = false;
if (getState(SHIP_THRUST)) thrust();
else if (getState(SHIP_BRAKE)) brake();
int dir = getState(SHIP_TURN_LEFT) - getState(SHIP_TURN_RIGHT);
if (dir) turn(dir);
else stopTurn();
}
void PushBox::thrust() {
engine = true;
b2Vec2 force = rad2vec(angle);
force *= mainThrust;
b2Vec2 vel = body->GetLinearVelocity();
b2Vec2 newVel = vel + force;
if (newVel.Length() > vel.Length()) {
float b = 1 - vel.LengthSquared() / (maxSpeed*maxSpeed);
if (b <= 0) b = 0;
double lorentz_factor_reciprocal = sqrt(b);
force *= lorentz_factor_reciprocal;
}
vel += force;
if (vel.Length() > 0) {
newVel.Normalize();
newVel *= vel.Length();
vel = newVel;
}
body->SetLinearVelocity(vel);
}
void PushBox::turn(int dir) {
float vel = body->GetAngularVelocity();
float force = turnThrust * (1 - (fabs(vel) / PI));
body->ApplyTorque(dir * force);
}
void PushBox::stopTurn() {
float vel = body->GetAngularVelocity();
if (fabs(vel) < 0.05) {
body->SetAngularVelocity(0);
return;
}
body->SetAngularVelocity(vel * 0.9);
return;
}
void PushBox::brake() {
b2Vec2 vel = body->GetLinearVelocity();
if (!vel.Length()) return;
float angle = body->GetAngle();
float wantAngle = vec2rad(-vel);
float diff = wantAngle - angle;
if (fabs(diff) < 0.01) {
body->SetAngularVelocity(0);
body->SetTransform(body->GetPosition(), wantAngle);
thrust();
b2Vec2 newVel = body->GetLinearVelocity();
float component = b2Dot(newVel, vel);
if (component < 0) body->SetLinearVelocity(b2Vec2(0, 0));
return;
}
float mod = sqrt(fabs(diff)) * (500 / body->GetMass());
if (diff < 0) mod = -mod;
body->SetAngularVelocity(mod);
}
void PushBox::render() {
position = body->GetPosition();
angle = body->GetAngle();
glLoadIdentity();
glTranslatef(position.x, position.y, 0);
glRotatef(angle * RAD2DEG, 0, 0, 1);
glBegin(GL_QUADS);
if (engine) glColor3f(1.0, 0.0, 0.0);
glVertex3f(-10.f, -10.f, 0);
glVertex3f(-10.f, 10.f, 0);
glColor3f(1.0, 1.0, 1.0);
glVertex3f( 10.f, 7.f, 0);
glVertex3f( 10.f, -7.f, 0);
glEnd();
}