taisei/src/move.h

80 lines
2.1 KiB
C
Raw Normal View History

/*
2019-10-04 05:04:12 +02:00
* This software is licensed under the terms of the MIT License.
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
*/
#pragma once
#include "taisei.h"
/*
* Simple generalized projectile movement based on laochailan's idea
*/
typedef struct MoveParams {
2019-12-18 15:19:13 +01:00
cmplx velocity, acceleration, retention;
cmplx attraction;
cmplx attraction_point;
2021-05-02 21:10:51 +02:00
real attraction_exponent;
} MoveParams;
2019-12-18 15:19:13 +01:00
cmplx move_update(cmplx *restrict pos, MoveParams *restrict params);
cmplx move_update_multiple(uint times, cmplx *restrict pos, MoveParams *restrict params);
INLINE MoveParams move_next(cmplx pos, MoveParams move) {
move_update(&pos, &move);
return move;
}
2019-12-18 15:19:13 +01:00
INLINE MoveParams move_linear(cmplx vel) {
return (MoveParams) { vel, 0, 1 };
}
2019-12-18 15:19:13 +01:00
INLINE MoveParams move_accelerated(cmplx vel, cmplx accel) {
return (MoveParams) { vel, accel, 1 };
}
2019-12-18 15:19:13 +01:00
INLINE MoveParams move_asymptotic(cmplx vel0, cmplx vel1, cmplx retention) {
return (MoveParams) { vel0, vel1 * (1 - retention), retention };
}
2020-02-19 03:06:14 +01:00
INLINE MoveParams move_asymptotic_halflife(cmplx vel0, cmplx vel1, double halflife) {
return move_asymptotic(vel0, vel1, exp2(-1.0 / halflife));
}
2019-12-18 15:19:13 +01:00
INLINE MoveParams move_asymptotic_simple(cmplx vel, double boost_factor) {
// NOTE: this matches the old asymptotic rule semantics exactly
double retention = 0.8;
return move_asymptotic(vel * (1 + boost_factor), vel, retention);
}
2019-12-18 15:19:13 +01:00
INLINE MoveParams move_towards(cmplx target, cmplx attraction) {
2019-09-01 02:23:36 +02:00
return (MoveParams) {
.attraction = attraction,
.attraction_point = target,
2021-05-02 21:10:51 +02:00
.attraction_exponent = 1
};
}
INLINE MoveParams move_towards_power(cmplx target, cmplx attraction, real exponent) {
return (MoveParams) {
.attraction = attraction,
.attraction_point = target,
.attraction_exponent = exponent
2019-09-01 02:23:36 +02:00
};
}
INLINE MoveParams move_dampen(cmplx vel, cmplx retention) {
return (MoveParams) {
.velocity = vel,
.retention = retention,
};
}
attr_deprecated("Use move_dampen instead")
2019-12-18 15:19:13 +01:00
INLINE MoveParams move_stop(cmplx retention) {
2019-10-06 12:50:58 +02:00
return (MoveParams) { .retention = retention };
}