Skip to content

Instantly share code, notes, and snippets.

@lotus128
lotus128 / input.rs
Created November 25, 2024 22:12
pole-based character controller in Bevy
use bevy::{
log::debug,
math::{Dir3, Vec3},
};
fn map_range(from_range: (f32, f32), to_range: (f32, f32), s: f32) -> f32 {
to_range.0
+ (s - from_range.0) * (to_range.1 - to_range.0)
/ (from_range.1 - from_range.0)
}
@lotus128
lotus128 / utils.rs
Created November 23, 2024 22:13
bevy_rapier3d character controller heel surface
pub fn determine_next_heel_stage_surface(
rapier_context: &Res<RapierContext>,
root_position: Vec3,
root_rotation: Quat,
cast_origin_forward: f32,
cast_origin_down: f32,
cast_distance: f32,
cast_max_angle: f32,
) -> Option<StageSurfaceData> {
@lotus128
lotus128 / bevy_rapier_3d_character_controller.rs
Created November 23, 2024 20:13
step down character controller
/// Determine the next "step down" stage surface for a character.
///
/// # Parameters
pub fn get_step_down_floor(
rapier_context: &RapierContext,
query_filter: QueryFilter,
root_source_translation: Vec3,
root_source_rotation: Quat,
wall_cast_origin_local_neg_y: f32,
wall_cast_distance: f32,
@lotus128
lotus128 / character_contorller_utils_.rs
Last active November 23, 2024 03:36
Bevy character controller rotate body around root
/// translate and rotate a point around anhter
pub fn rotate_point_around_pivot(
point: Vec3,
pivot: Vec3,
rotation: Quat,
) -> Vec3 {
// Translate the point to the origin
let translated_point = point - pivot;
// Apply the rotation
@lotus128
lotus128 / character_controller_3d.rs
Created November 16, 2024 14:55
3D character controller in Bevy with Avian3D
/// determine next linear velocity for a character.
/// "with body" means veloicity is applied to the body.
// TODO move to avian3d_lib
pub fn determine_next_step_linear_velocity_for_rigidbody_with_body(
spatial_query: &SpatialQuery,
query_filter: SpatialQueryFilter,
start_root_global_translation: Vec3,
start_root_global_rotation: Quat,
body_shape: SharedShape,
body_local_translation: Vec3,
@lotus128
lotus128 / index.ts
Created December 21, 2023 16:29
fiddle-typescript-type-maps
import { ValueOf } from 'type-fest';
/**
* a "type map" is a map of type tag/ discriminator to type.
* https://javascript.plainenglish.io/tagged-union-types-in-typescript-leveraging-type-safety-and-flexibility-be0e60145815
*
* type maps are useful for creating event maps or action maps.
*
* Actions as in Redux action: https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow#actions
*/
@lotus128
lotus128 / lab-typescript-mapped-types-select-with-order.ts
Created October 12, 2023 21:47
Lab, TypeScript mapped types: select from Record with order
function pickValueOfRecordUsingOrder<
TKey extends PropertyKey,
const TOrder extends ReadonlyArray<TKey>,
const TInputRecord extends Record<TOrder[number], number>,
TResultArray extends {
[X in keyof TOrder]: TInputRecord[TOrder[X]] // any value can be selected
},
>(
input: TInputRecord,
order: TOrder
@lotus128
lotus128 / lab-typescript-mapped-types-tuples.ts
Last active October 12, 2023 20:31
Lab, TypeScript Mapped Types with Tuples
interface HasGetValue<TValue> {
getValue: () => TValue
}
function transformArray<
TInputs extends Array<HasGetValue<unknown>>,
TOuptuts = { [X in keyof TInputs]: ReturnType<TInputs[X]['getValue']> }
>(
inputs: TInputs
): TOuptuts {
@lotus128
lotus128 / deploy-update
Created April 20, 2020 17:55
AWS Lambda Node.js deployment npm script
"deploy-update": "zip -r function.zip . -x \".git/*\" -x \".gitignore\" -x \"./node_modules/aws-sdk/*\" -x \"./node_modules/events/\" -x \"./node_modules/ieee754/\" -x \"./node_modules/jmespath/\" -x \"./node_modules/querystring/\" -x \"./node_modules/sax/\" -x \"./node_modules/url/\" -x \"./node_modules/uuid/\" -x \"./node_modules/xml2js/\" && aws lambda update-function-code --function-name ComicPagePublish --zip-file fileb://function.zip"
@lotus128
lotus128 / toGIF.bat
Last active December 30, 2020 00:16
ffmpeg; Windows batch file to convert an image to a gif via ffmpeg.
:: creates a .gif from an input file
:: supports drag and drop
ffmpeg -y -i %~1 -vf "fps=12,scale=640:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 %~1.gif