Skip to content

Instantly share code, notes, and snippets.

View makoConstruct's full-sized avatar

mako yass makoConstruct

View GitHub Profile
@makoConstruct
makoConstruct / hex_spiral.rs
Last active April 17, 2016 08:12
An iterator that spirals out over the cells of a hexagonal grid
use std::iter::Iterator;
//for hex schemes like this:
//. . x->
// . . .
// y . .
// \
// v
#[derive(Debug)]
@makoConstruct
makoConstruct / easingWithInitialVelocity.rs
Last active April 27, 2016 10:15
Methods for easing in and out at constant accelerations, starting with a given velocity and ending at 0.
//an easing function that eases in and out by constant acceleration in either direction (parabolic), that allows the user to specify a starting velocty. This allows a user to interrupt the animation partway through and send it in a different direction without any sudden jerks.
fn sq(a:f32)-> f32 { a*a }
//I'm including these because the way the constant acceleration functions handle cases where initial_velocity > 2 doesn't look very good, it overshoots the target, then comes back to it. You will probably prefer to use these linear acceleration-deceleration functions for those cases instead. You might like to use them instead of constant acceleration methods altogether, but I find they look a bit too jerky.
fn linear_acceleration_ease_in_out_with_initial_velocity(t:f32, initial_velocity:f32)-> f32 {
t*(t*((initial_velocity - 2f32)*t + (3f32 - 2f32*initial_velocity)) + initial_velocity)
}
precision mediump float;
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_uv;
attribute vec2 a_angleUnit;
attribute float a_pixelSize;
attribute float a_innerRadius;
uniform mat4 u_projection;
varying vec4 color;
precision mediump float;
varying vec4 color;
varying vec2 uv;
varying float pixelSize;
varying float innerRadius;
varying vec2 angleUnit;
@makoConstruct
makoConstruct / Memer.cpp
Last active August 1, 2019 05:57
A thing for sort of averaging a quantity over time with just a single float
const float MemerPower = 2;
struct Memer{ //where p represents the pulse magnitude and delta represents the amount of time that has passed since the last pulse, the Memer sort of remembers the last couple of pulses and how long ago they were. For any consistent p, d, Will arrive at an equilibrium v that is higher if delta is lower and higher if p are higher. And I think- I'm not sure of this but it seems like something like this will hold- I think that for any a,b p,d where a/b = p/d, if you keep pulsing (a,b) and (p,d) they'll end up similar?
float v;
Memer(float v = 1):v(v){}
void pulse(float p, float delta){
float xof = log(v)/log(MemerPower);
v = pow(MemerPower, xof-delta) + p;
}
};
@makoConstruct
makoConstruct / astar.cpp
Last active October 31, 2016 05:37
defines a graph and implements A Star pathfinding algo. Depends on a few utility functions and type aliases. It's obvious enough what they mean, so you'd have no trouble swapping them out for whatever you use.
//graph stuff
template<typename T>
struct GraphNode{
vector<GraphNode*> neighbors;
T v;
void unlink(){
for(GraphNode* neigh : neighbors){
removeEl(neigh->neighbors, this);
}
neighbors.clear();
@makoConstruct
makoConstruct / termpose_helpers.cpp
Created March 11, 2017 00:22
it's mindnumbing to go alone, take these
Term const* findTermConst(vector<Term> const& candidates, string const& key){
for(Term const& t : candidates){
if(t.isList() && t.l.l.size() > 1){
Term const& ft = t.l.l[0];
if(ft.isStr() && ft.s.s == key){
return &t;
}
}
}
return nullptr;
@makoConstruct
makoConstruct / pointAndConnectionAnimation.cpp
Created March 26, 2017 00:26
only part of the logic for animating rectangular point and line generata as a series of strokes
void FolkBodySpec::prepAnimation(){
//start by identifying clusters
auto groupColorings = fillCopies(itemTrayDimensionX*itemTrayDimensionY, 0);
u8 nextColoring = 1;
vector<Coord> centralNode;
overRegion(itemTrayDimensionX, itemTrayDimensionY, [&](Coord c){
uint ik = itemOffset(c);
if(locatedItems[ik]){
u8 gc;
if(!groupColorings[ik]){
@makoConstruct
makoConstruct / careful_easing.cpp
Created April 5, 2017 05:53
very smooth, 'smart' (but still pure functions of time) easing animations. Specify target, time to reach target, and initial velocity. Smoothly accelerates and decelerates to get there and stand still exactly on time
float linearAccelerationEaseInOutWithInitialVelocity(float t, float initialVelocity){
if(t >= 1.0){ return 1.0; }
return t*(t*((initialVelocity - 2)*t + (3.0f - 2*initialVelocity)) + initialVelocity);
}
float velocityOfLinearAccelerationEaseInOutFromInitialVelocity(float t, float initialVelocity){
if(t >= 1.0){ return 0.0; }
return t*((3.0f*initialVelocity - 6.0f)*t + (6.0f - 4.0f*initialVelocity)) + initialVelocity;
@makoConstruct
makoConstruct / Approacher2d.cpp
Last active October 25, 2017 04:55
Smoothly accelerates and decelerates to land exactly on target
void updateApproacher(v2& currentPosition, v2& currentVelocity, v2 target, float delta, float acceleration, float deceleration, float maxSpeed){
if(delta == 0) return;
v2 dto = target - currentPosition;
float dtm = dto.magnitude();
float vm = currentVelocity.magnitude();
auto distanceToStop = [&](float initialVelocity, float deceleration){
float deltdec = deceleration*delta;
float numberOfFramesToSlowDown = max(0.0f, floor((initialVelocity - deltdec)/(deltdec)));
return numberOfFramesToSlowDown*delta*(initialVelocity - (deltdec*(numberOfFramesToSlowDown + 1))/2);
};