-
Notifications
You must be signed in to change notification settings - Fork 0
/
stickman.cpp
212 lines (149 loc) · 4.12 KB
/
stickman.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "32blit.hpp"
using namespace blit;
int speed;
float count, rotate, roll, spin;
Vec2 center;
Vec3 neck, hip;
Vec3 rot3d, pos;
struct bone { Vec3 joint1; Vec3 joint2; };
std::vector<bone> bones;
Pen black = Pen(0,0,0);
Pen white = Pen(255,255,255);
Pen green = Pen(0,255,0);
float deg2rad( float deg){ return ( (deg / 180.0f) * 3.142); }
Vec3 rotate3d (Vec3 point3d,Vec3 rot) {
static Vec2 point;
//rotate 3d point about X
point = Vec2(point3d.y,point3d.z);
point *= Mat3::rotation(rot.x);
point3d.y = point.x;
point3d.z = point.y;
// rotate 3d point about Y
point = Vec2(point3d.z,point3d.x);
point *= Mat3::rotation(rot.y);
point3d.z = point.x;
point3d.x = point.y;
// rotate 3d point about Z
point = Vec2(point3d.x,point3d.y);
point *= Mat3::rotation(rot.z);
point3d.x = point.x;
point3d.y = point.y;
return (point3d);
}
Vec2 to2d (Vec3 point3d) {
Vec2 point;
// project to screen
int z = point3d.z - 500;
point.x = point3d.x * 500 / z;
point.y = point3d.y * 500 / z;
point += center;
return (point);
}
void add_leg(int side) {
float angle;
Vec3 knee,ankle;
angle = sin(count + side) * 30;
angle = deg2rad(angle +170);
knee = Vec3 (sin (angle), -cos (angle),0) * 50;
knee += hip;
bones.push_back( {hip,knee} );
angle = sin (count + side - 0.873f) * 45;
angle = deg2rad(angle + 225);
ankle = Vec3 (sin (angle), -cos (angle),0) * 50;
ankle += knee;
bones.push_back ( {knee,ankle} );
}
void add_arm(int side) {
float angle;
Vec3 shoulder,elbow,wrist;
shoulder = neck + Vec3(0,10,0);
bones.push_back ( {shoulder,neck} );
angle = deg2rad(sin(count + side) * 60);
elbow = Vec3 (sin (angle), cos (angle),0) * 30;
elbow += shoulder;
bones.push_back ( {shoulder,elbow} );
angle = sin(count + side) * 60;
angle = deg2rad(angle + 50);
wrist = Vec3 (sin (angle), cos (angle),0) * 30;
wrist += elbow;
bones.push_back ( {elbow,wrist} );
}
void add_torso(void) {
neck = Vec3(0,-70,0);
neck += hip;
bones.push_back ( {hip,neck} );
}
void draw_head( Vec2 head) {
screen.circle(head,15);
screen.pen = black;
screen.circle(head,14);
}
void draw_stickman(void) {
for (auto bone:bones)
screen.line(to2d(bone.joint1), to2d(bone.joint2));
}
void draw_ground(void) {
static float dustx;
dustx -= speed / 20.0f;
if (dustx < 0) dustx = 100;
// make a grid of dots
for (int x=0; x < 10; x++)
for (int y=0; y < 10 ; y++) {
Vec3 dust = Vec3(x-5,0,y-5) * 50;
dust.x += dustx;
dust = rotate3d(dust,rot3d);
dust.y -= 100;
screen.pixel(to2d(dust));
}
}
void init() {
set_screen_mode(ScreenMode::hires);
center = Vec2(screen.bounds.w / 2, screen.bounds.h / 2);
speed = 100;
roll = 300;
}
void render(uint32_t time) {
float left,right = 3.142;
screen.pen = black;
screen.clear();
screen.pen = green;
draw_ground();
screen.pen = white;
bones.clear();
add_leg(left);
add_leg(right);
add_torso();
add_arm(left);
add_arm(right);
Vec3 head = neck - Vec3(0,20,0);
for (auto &bone:bones) {
bone.joint1 += pos;
bone.joint2 += pos;
}
head += pos;
//stickman
for (auto &bone:bones) {
bone.joint1 = rotate3d(bone.joint1, rot3d);
bone.joint2 = rotate3d(bone.joint2, rot3d);
}
head = rotate3d(head,rot3d);
draw_stickman();
draw_head(to2d(head));
}
void update(uint32_t time) {
static int demo=true;
count += speed / 2000.0f;
pos.x += blit::joystick.x;
pos.z += blit::joystick.y;
if (pressed(Button::Y) && speed > 20) speed--;
if (pressed(Button::A) && speed < 900) speed++;
if (pressed(DPAD_LEFT)) rotate--;
if (pressed(DPAD_RIGHT)) rotate++;
if (pressed(DPAD_UP)) roll--;
if (pressed(DPAD_DOWN)) roll++;
if (pressed(Button::X) ) spin++;
if (pressed(Button::B) ) spin--;
if (buttons.released) demo = false;
if (demo) rotate--;
rot3d = Vec3(roll/100,rotate/100,spin/100);
}