summaryrefslogtreecommitdiff
path: root/main.c
blob: 70c30a044af4f33cf89e8c30a031773cf96e5f2c (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <geometry.h>
#include "dat.h"
#include "fns.h"

double PIX2M = 5;

Mousectl *mc;
Keyboardctl *kc;
Channel *scrsync;
RFrame worldrf;
Projectile ball;
double t0, Δt;
double v0;
double A;	/* area of the projectile that meets the air */
Point2 target;

char stats[SLEN][64];
Image *statc;

void *
emalloc(ulong n)
{
	void *p;

	p = malloc(n);
	if(p == nil)
		sysfatal("malloc: %r");
	memset(p, 0, n);
	setmalloctag(p, getcallerpc(&n));
	return p;
}

Point
toscreen(Point2 p)
{
	p = invrframexform(p, worldrf);
	return Pt(p.x, p.y);
}

Point2
fromscreen(Point p)
{
	return rframexform(Pt2(p.x, p.y, 1), worldrf);
}

void
∫(double Δt)
{
	Point2 Fd;	/* drag force */

	Fd = mulpt2(mulpt2(ball.v, -vec2len(ball.v)), 0.5 * Cd*ρ*A);	/* ½CdρAv² */
	ball.v = addpt2(ball.v, mulpt2(addpt2(Vec2(0,-Eg), divpt2(Fd, ball.mass)), Δt));
	ball.p = addpt2(ball.p, mulpt2(ball.v, Δt));
	snprint(stats[Spos], sizeof(stats[Spos]), "p: %v", ball.p);
	snprint(stats[Sdrag], sizeof(stats[Sdrag]), "Fd: %v", divpt2(Fd, ball.mass));
	if(ball.p.y <= (2+1)*M2PIX){
		ball.p.y = (2+1)*M2PIX;
		ball.v = Vec2(0,0);
	}
}

void
drawstats(void)
{
	int i;

	snprint(stats[Svel], sizeof(stats[Svel]), "|v|: %gm/s", vec2len(ball.v));
	snprint(stats[Sdeltax], sizeof(stats[Sdeltax]), "Δx: %gm", target.x-ball.p.x);
	for(i = 0; i < nelem(stats); i++)
		stringn(screen, addpt(screen->r.min, Pt(10, font->height*i+1)), statc, ZP, font, stats[i], sizeof(stats[i]));
}

void
redraw(void)
{
	lockdisplay(display);
	draw(screen, screen->r, display->black, nil, ZP);
	fillellipse(screen, toscreen(ball.p), 2, 2, display->white, ZP);
	line(screen, toscreen(Pt2(ball.p.x, 0, 1)), toscreen(target), 0, 0, 1, statc, ZP);
	drawstats();
	flushimage(display, 1);
	unlockdisplay(display);
}

void
mmb(void)
{
	enum {
		SETV0,
		SETPOS,
		SETMASS,
		SETDIAM,
	};
	static char *items[] = {
	 [SETV0]	"set v0",
	 [SETPOS]	"set position",
	 [SETMASS]	"set mass",
	 [SETDIAM]	"set diameter",
		nil
	};
	static Menu menu = { .item = items };
	char buf[32], *p;

	switch(menuhit(2, mc, &menu, nil)){
	case SETV0:
		snprint(buf, sizeof(buf), "%g", v0);
		enter("v0(m/s):", buf, sizeof(buf), mc, kc, nil);
		if(buf[0] != 0)
			v0 = strtod(buf, nil);
		break;
	case SETPOS:
		snprint(buf, sizeof(buf), "%g, %g", ball.p.x, ball.p.y);
		enter("pos(x,y):", buf, sizeof(buf), mc, kc, nil);
		if(buf[0] != 0 && (p = strchr(buf, ',')) != nil){
			ball.p.x = strtod(buf, nil);
			ball.p.y = strtod(p+1, nil);
		}
		break;
	case SETMASS:
		snprint(buf, sizeof(buf), "%g", ball.mass);
		enter("mass(kg):", buf, sizeof(buf), mc, kc, nil);
		if(buf[0] != 0)
			ball.mass = strtod(buf, nil);
		break;
	case SETDIAM:
		snprint(buf, sizeof(buf), "%g", 2*ball.r);
		enter("diameter(m):", buf, sizeof(buf), mc, kc, nil);
		if(buf[0] != 0){
			ball.r = strtod(buf, nil)/2;
			A = 2*PI*ball.r*ball.r;	/* ½(4πr²) */
		}
		break;
	}
}

void
rmb(void)
{
	enum {
		RST,
		QUIT,
	};
	static char *items[] = {
	 [RST]	"reset",
	 [QUIT]	"quit",
		nil,
	};
	static Menu menu = { .item = items };

	switch(menuhit(3, mc, &menu, nil)){
	case RST:
		ball.p = Pt2((2+1)*M2PIX,(2+1)*M2PIX,1);
		ball.v = Vec2(0,0);
		break;
	case QUIT:
		threadexitsall(nil);
	}
}

void
zoomin(void)
{
	PIX2M += 0.01;
	worldrf.bx = Vec2(PIX2M,0);
	worldrf.by = Vec2(0,-PIX2M);
	snprint(stats[Sscale], sizeof(stats[Sscale]), "s: %gm/pix", M2PIX);
}

void
zoomout(void)
{
	PIX2M -= 0.01;
	worldrf.bx = Vec2(PIX2M,0);
	worldrf.by = Vec2(0,-PIX2M);
	snprint(stats[Sscale], sizeof(stats[Sscale]), "s: %gm/px", M2PIX);
}

void
mouse(void)
{
	Point2 p;
	double θ, dist, eta;

	if(ball.p.y <= (2+1)*M2PIX){
		p = subpt2(fromscreen(mc->xy), ball.p);
		θ = atan2(p.y, p.x);
		snprint(stats[Stheta], sizeof(stats[Stheta]), "θ: %g°", θ/DEG);
		dist = v0*v0*sin(2*θ)/Eg;
		target = Pt2(ball.p.x+dist, 0, 1);
		eta = 2*v0*sin(θ)/Eg;
		snprint(stats[Seta], sizeof(stats[Seta]), "eta: %gs", eta);
		if((mc->buttons & 1) != 0)
			ball.v = Vec2(v0*cos(θ), v0*sin(θ));
	}
	if((mc->buttons & 2) != 0)
		mmb();
	if((mc->buttons & 4) != 0)
		rmb();
	if((mc->buttons & 8) != 0)
		zoomin();
	if((mc->buttons & 16) != 0)
		zoomout();
}

void
key(Rune r)
{
	switch(r){
	case Kdel:
	case 'q':
		threadexitsall(nil);
	}
}

void
resized(void)
{
	lockdisplay(display);
	if(getwindow(display, Refnone) < 0)
		fprint(2, "can't reattach to window\n");
	worldrf.p = Pt2(screen->r.min.x, screen->r.max.y, 1);
	unlockdisplay(display);
	redraw();
}

void
scrsyncproc(void *)
{
	for(;;){
		send(scrsync, nil);
		sleep(HZ2MS(60));
	}
}

void
usage(void)
{
	fprint(2, "usage: %s\n", argv0);
	exits("usage");
}

void
threadmain(int argc, char *argv[])
{
	Rune r;

	GEOMfmtinstall();
	ARGBEGIN{
	default: usage();
	}ARGEND;

	if(initdraw(nil, nil, "ballistics") < 0)
		sysfatal("initdraw: %r");
	mc = initmouse(nil, screen);
	if(mc == nil)
		sysfatal("initmouse: %r");
	kc = initkeyboard(nil);
	if(kc == nil)
		sysfatal("initkeyboard: %r");
	statc = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DYellow);
	if(statc == nil)
		sysfatal("allocimage: %r");

	snprint(stats[Sscale], sizeof(stats[Sscale]), "s: %gm/pix", M2PIX);

	worldrf.p = Pt2(screen->r.min.x, screen->r.max.y, 1);
	worldrf.bx = Vec2(PIX2M,0);
	worldrf.by = Vec2(0,-PIX2M);

	ball.p = Pt2((2+1)*M2PIX,(2+1)*M2PIX,1);
	ball.v = Vec2(0, 0);
	ball.mass = 0.149;
	ball.r = 0.375;		/* 3.75cm */
	A = 2*PI*ball.r*ball.r;	/* ½(4πr²) */
	v0 = 53.64;		/* avg baseball hit speed */

	scrsync = chancreate(1, 0);
	display->locking = 1;
	unlockdisplay(display);

	proccreate(scrsyncproc, 0, mainstacksize);

	t0 = nsec();
	for(;;){
		Alt a[] = {
			{mc->c, &mc->Mouse, CHANRCV},
			{mc->resizec, nil, CHANRCV},
			{kc->c, &r, CHANRCV},
			{scrsync, nil, CHANRCV},
			{nil, nil, CHANEND}
		};
		switch(alt(a)){
		case 0: mouse(); break;
		case 1: resized(); break;
		case 2: key(r); break;
		case 3: redraw(); break;
		}
		Δt = (nsec()-t0);
		∫(Δt/1e9);
		t0 += Δt;
	}
}