i dont know if there are something like this in the forum( just let me know in that case ), here are some functions i created for a project i am working and are usefull for game dev, i am using JavaFX with Canvas only, dunno if they work on swing, any CC or sugestion will gradly received
CLAMP
/**
* if value is mayor than max value, max value will be returned,
* if value is minor than min value, min value will be returned,
* if value is in range between mayor and minor values, then
* value will be returned
* @param value
* @param minValue
* @param maxValue
* @return value will never falls below minValue or goes over maxValue
*/
public static float clamp( float value, float minValue, float maxValue )
{
// value must be between minValue and maxValue
return (value > maxValue) ? maxValue : (value < minValue) ? minValue : value;
}
LERP
/**
* this function creates linear interpolation depending on argument t,
* which is the percentage of v0 until v1
* @example lerp(0, 10, 0.5): will return 5.0, because is the middle ( 0.5 ) between 0 and 10
* @param v0
* @param v1
* @param t
* @return
*/
public static float Lerp(float v0, float v1, float t)
{
return (1-t)*v0 + t*v1;
}
LENGHT_DIR_X
/**
* returns X position of the vector determined by length and angleDirection
* <b>NOTE: you must add to this value to originX where will be added this vector
* for example sprite.getX()+lengthdirX( 100, 45 )</b>
* @param length
* @param angleDirection
* @return
*/
public static float lengthdirX( float length, float angleDirection )
{
return (float)Math.cos( angleDirection * (Math.PI/180) ) * length;
}
LENGHT_DIR_Y
/**
* returns Y position of the vector determined by length and angleDirection
* <b>NOTE: you must add to this value to originY where will be added this vector
* for example sprite.getY()+lengthdirY( 100, 45 )</b>
* @param length
* @param angleDirection
* @return
*/
public static float lengthdirY( float length, float angleDirection )
{
return (float)Math.sin( angleDirection * (Math.PI/180) ) * length;
}