Mirai's Miscellaneous Misadventures

M52 / core / motion.c

1/* license: AGPLv3 or later */
2/* copyright 2024 zamfofex */
3
4#include "../mimimi.h"
5
6void mimimi_linear_propagate(struct mimimi_momentum *momentum, int value, int x0, int scale)
7{
8	momentum->dx += (value - x0) * 256 / scale - momentum->x;
9}
10
11void mimimi_linear_apply(struct mimimi_momentum *momentum, int *value, int x0, int scale)
12{
13	*value = momentum->x * scale / 256 + x0;
14}
15
16void mimimi_angular_propagate(struct mimimi_momentum *momentum, struct mimimi_position *position, int x0, int y0, int a0, int scale)
17{
18	int a, x, y, dx;
19	
20	x = position->x - x0;
21	y = position->y - y0;
22	a = mimimi_angle(x, y);
23	
24	dx = (a * 4 - a0 * 256) * 256 - momentum->x * scale;
25	dx = mimimi_mod(dx + 0x800000, 0x1000000) - 0x800000;
26	momentum->dx += dx / scale;
27}
28
29void mimimi_angular_apply(struct mimimi_momentum *momentum, struct mimimi_position *position, int x0, int y0, int a0, int scale, int length)
30{
31	int a, x, y;
32	
33	a = mimimi_mod(momentum->x * scale / 256 / 32 + a0 * 8, 2048);
34	
35	x = mimimi_sine[a] * length / 128;
36	y = mimimi_cosine[a] * length / 128;
37	
38	position->x = x + x0;
39	position->y = y + y0;
40}
41
42void mimimi_linear_motion_propagate(struct mimimi_linear_motion *motion, struct mimimi_momentum *momentum)
43{
44	mimimi_linear_propagate(momentum, *motion->value, motion->origin, motion->scale);
45}
46
47void mimimi_linear_motion_apply(struct mimimi_linear_motion *motion, struct mimimi_momentum *momentum)
48{
49	mimimi_linear_apply(momentum, motion->value, motion->origin, motion->scale);
50}
51
52void mimimi_linear_motion(struct mimimi_linear_motion *motion, int *value, int origin, int scale)
53{
54	motion->value = value;
55	motion->origin = origin;
56	motion->scale = scale;
57}
58
59void mimimi_angular_motion_propagate(struct mimimi_angular_motion *motion, struct mimimi_momentum *momentum)
60{
61	mimimi_angular_propagate(momentum, motion->value, motion->origin.x, motion->origin.y, motion->angle, motion->scale);
62}
63
64void mimimi_angular_motion_apply(struct mimimi_angular_motion *motion, struct mimimi_momentum *momentum)
65{
66	mimimi_angular_apply(momentum, motion->value, motion->origin.x, motion->origin.y, motion->angle, motion->scale, motion->height);
67}
68
69void mimimi_angular_motion(struct mimimi_angular_motion *motion, struct mimimi_position *value, int x0, int y0, int a0, int scale, int height)
70{
71	motion->value = value;
72	motion->origin.x = x0;
73	motion->origin.y = y0;
74	motion->angle = a0;
75	motion->scale = scale;
76	motion->height = height;
77}