Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (797)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Utility Functions for JavaFX Game Development ( lerp, clamp, etc, etc )  (Read 402 times)
0 Members and 1 Guest are viewing this topic.
Offline pavul

Senior Devvie


Medals: 4
Exp: 4 years



« Posted 2019-07-18 05:07:38 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
/**
 * 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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
/**
 * 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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
/**
 * 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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
/**
 * 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;
}


its better not know why it works, that know why is not working
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (3672 views)
2018-11-16 13:46:03

hadezbladez (1315 views)
2018-11-16 13:41:33

hadezbladez (3679 views)
2018-11-16 13:35:35

hadezbladez (710 views)
2018-11-16 13:32:03

EgonOlsen (4020 views)
2018-06-10 19:43:48

EgonOlsen (4546 views)
2018-06-10 19:43:44

EgonOlsen (2706 views)
2018-06-10 19:43:20

DesertCoockie (3575 views)
2018-05-13 18:23:11

nelsongames (3722 views)
2018-04-24 18:15:36

nelsongames (4753 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!