Vuo 2.4.4
Loading...
Searching...
No Matches
VuoSmooth.h
Go to the documentation of this file.
1
10#ifndef VUOSMOOTH_H
11#define VUOSMOOTH_H
12
16typedef struct _VuoSmoothInertia
17{
18 bool moving;
19 bool updateNeededAfterSetPosition;
20 VuoGenericType1 positionLastFrame;
21 VuoGenericType1 velocity;
22 VuoGenericType1 target;
23 VuoGenericType1 positionWhenTargetChanged;
24 VuoGenericType1 velocityWhenTargetChanged;
25 VuoReal timeLastFrame;
26 VuoReal timeWhenTargetChanged;
27 VuoReal duration;
29
33static VuoSmoothInertia VuoSmoothInertia_make(VuoGenericType1 initialPosition)
34{
35 VuoSmoothInertia s = (VuoSmoothInertia)calloc(1,sizeof(struct _VuoSmoothInertia));
36 VuoRegister(s, free);
37 s->positionLastFrame = initialPosition;
38 return s;
39}
40
44static void VuoSmoothInertia_setPosition(VuoSmoothInertia s, VuoGenericType1 position)
45{
46 s->positionLastFrame = position;
47 s->moving = false;
48 s->velocity = VuoGenericType1_makeFromJson(NULL);
49 s->updateNeededAfterSetPosition = true;
50}
51
55static void VuoSmoothInertia_setTarget(VuoSmoothInertia s, VuoReal time, VuoGenericType1 target)
56{
57 s->target = target;
58 s->positionWhenTargetChanged = s->positionLastFrame;
59 s->velocityWhenTargetChanged = s->velocity;
60 s->timeWhenTargetChanged = time;
61 s->moving = true;
62}
63
68{
69 s->duration = MAX(duration, 0.00001);
70}
71
79static bool VuoSmoothInertia_step(VuoSmoothInertia s, VuoReal time, VuoGenericType1 *calculatedPosition)
80{
81 bool movedDuringThisStep = false;
82
83 if (s->updateNeededAfterSetPosition)
84 {
85 movedDuringThisStep = true;
86 *calculatedPosition = s->positionLastFrame;
87 s->updateNeededAfterSetPosition = false;
88 }
89
90 if (s->moving)
91 {
92 movedDuringThisStep = true;
93 VuoReal timeSinceLastFrame = time - s->timeLastFrame;
94 if (timeSinceLastFrame != 0)
95 {
96 double timeSinceTargetChanged = MIN(time - s->timeWhenTargetChanged, s->duration);
97 double curviness = s->duration/(3.*timeSinceLastFrame);
98 VuoGenericType1 p1 = VuoGenericType1_add(s->positionWhenTargetChanged, VuoGenericType1_multiply(s->velocityWhenTargetChanged, timeSinceLastFrame*curviness));
99 *calculatedPosition = VuoGenericType1_bezier3(s->positionWhenTargetChanged, p1, s->target, s->target, timeSinceTargetChanged/s->duration);
100
101 s->velocity = VuoGenericType1_multiply(VuoGenericType1_subtract(*calculatedPosition, s->positionLastFrame), 1./timeSinceLastFrame);
102 s->positionLastFrame = *calculatedPosition;
103
104 if (time - s->timeWhenTargetChanged > s->duration)
105 {
106 s->moving = false;
107 s->velocity = VuoGenericType1_makeFromJson(NULL);
108 }
109 }
110 }
111
112 s->timeLastFrame = time;
113
114 return movedDuringThisStep;
115}
116
117#endif