Hi folks.
I keep teasing this concept, and occasionally quietly solve this-or-that problem using these methods without really explaining things, so it's finally time for a bare-bones demo of... drum roll please... parameterized motion.
Punchline first: If you see what's going on here, you will know how to move anything on screen, to anywhere, in any way you want, in full generality. This code ignores rotations, there is plenty to study with just translations.
When you run the code below, what you see, simply, is a circle moving from a beginning point A to an ending point B. The motion is of course not linear. The moving ball is doing two things:
(i) Trying to move straight in a straight line from A to B, using a speed function governed by the hyperbolic tangent. It starts slow, gets quick, and ends slow.
(ii) Trying to wiggle in a direction perpendicular to the motion, using wacky sine function stuff.
The combination of (i) and (ii) is what you see on screen. While it's running, even right in the middle of the motion, click+drag to move the target point around, and all of the motion adjusts seamlessly. Once you see the power and ease of this, you can imagine that literally *any* kind of motion can be tuned into this thing.
Play with some numbers and see how it applies to your projects:
PointA.x = -200
PointA.y = -100
PointB.x = 200
PointB.y = 100
' Main loop
q = -4 * (1 - 2 * t)
p = .5 * (1 + tanh(q)) ' Goes from 0 to 1.
Player.x
= PointA.x
+ Displacement.x
* (p
) + Tangential.x
* (40 * SIN(2 * pi
* p
)) Player.y
= PointA.y
+ Displacement.y
* (p
) + Tangential.y
* (40 * SIN(5 * pi
* p
))
'CLS
LOCATE 1, 1:
PRINT "Left-Click+Drag to move target point." CALL ccircle
(PointA.x
, PointA.y
, 20, _RGBA(255, 0, 0, 255)) CALL ccircle
(Player.x
, Player.y
, 20, _RGBA(0, 255, 0, 255)) CALL ccircle
(PointB.x
, PointB.y
, 20, _RGBA(0, 0, 255, 255))
Displacement.x = PointB.x - PointA.x
Displacement.y = PointB.y - PointA.y
Tangential.x = -Displacement.y
Tangential.y = Displacement.x
mag
= SQR(Tangential.x
^ 2 + Tangential.y
^ 2) Tangential.x = Tangential.x / mag
Tangential.y = Tangential.y / mag
tanh
= (EXP(2 * x
) - 1) / (EXP(2 * x
) + 1)