Mirai's Miscellaneous Misadventures

M53 / 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,
7    int x0, int scale)
8{
9    momentum->dx += (value - x0) * 256 / scale - momentum->x;
10}
11
12void mimimi_linear_apply(struct mimimi_momentum *momentum, int *value,
13    int x0, int scale)
14{
15    *value = momentum->x * scale / 256 + x0;
16}
17
18void mimimi_angular_propagate(struct mimimi_momentum *momentum,
19    struct mimimi_position *position, int x0, int y0, int a0, int scale)
20{
21    int a, x, y, dx;
22
23    x = position->x - x0;
24    y = position->y - y0;
25    a = mimimi_angle(x, y);
26
27    dx = (a * 4 - a0 * 256) * 256 - momentum->x * scale;
28    dx = mimimi_mod(dx + 0x800000, 0x1000000) - 0x800000;
29    momentum->dx += dx / scale;
30}
31
32void mimimi_angular_apply(struct mimimi_momentum *momentum,
33    struct mimimi_position *position, int x0, int y0, int a0, int scale,
34    int length)
35{
36    int a, x, y;
37
38    a = mimimi_mod(momentum->x * scale / 256 / 32 + a0 * 8, 2048);
39
40    x = mimimi_sine[a] * length / 128;
41    y = mimimi_cosine[a] * length / 128;
42
43    position->x = x + x0;
44    position->y = y + y0;
45}
46
47void mimimi_linear_motion_propagate(struct mimimi_linear_motion *motion,
48    struct mimimi_momentum *momentum)
49{
50    mimimi_linear_propagate(momentum, *motion->value, motion->origin,
51        motion->scale);
52}
53
54void mimimi_linear_motion_apply(struct mimimi_linear_motion *motion,
55    struct mimimi_momentum *momentum)
56{
57    mimimi_linear_apply(momentum, motion->value, motion->origin,
58        motion->scale);
59}
60
61void mimimi_linear_motion(struct mimimi_linear_motion *motion, int *value,
62    int origin, int scale)
63{
64    motion->value = value;
65    motion->origin = origin;
66    motion->scale = scale;
67}
68
69void mimimi_angular_motion_propagate(struct mimimi_angular_motion *motion,
70    struct mimimi_momentum *momentum)
71{
72    mimimi_angular_propagate(momentum, motion->value, motion->origin.x,
73        motion->origin.y, motion->angle, motion->scale);
74}
75
76void mimimi_angular_motion_apply(struct mimimi_angular_motion *motion,
77    struct mimimi_momentum *momentum)
78{
79    mimimi_angular_apply(momentum, motion->value, motion->origin.x,
80        motion->origin.y, motion->angle, motion->scale, motion->height);
81}
82
83void mimimi_angular_motion(struct mimimi_angular_motion *motion,
84    struct mimimi_position *value, int x0, int y0, int a0, int scale,
85    int height)
86{
87    motion->value = value;
88    motion->origin.x = x0;
89    motion->origin.y = y0;
90    motion->angle = a0;
91    motion->scale = scale;
92    motion->height = height;
93}