Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Calculate a speed of colour-shifting  (Read 789 times) 0 Members and 1 Guest are viewing this topic.
VaPS
 « Posted 2013-08-09 19:45:47 »

Look at Reply #2 for an easier explanation!

 1  2  3  4 `public Background(Color cc, int aa, int tt) {      c = cc;      s = ???;   }`

cc is colour, aa is colour deviation and tt is time in seconds. The game updates 60 times/second.

For example, if I passed in Color.PINK, 50, 3 I would basically say this:
My background is in the colour pink by default.
Its colour is going to continuously shift shades.
The time for a shift from the darkest shade to the brightest is going to take 3 seconds.

How would I calculate the speed s with known deviation and time?

sirkarpfen

Junior Devvie

Medals: 2
Exp: 2 years

 « Reply #1 - Posted 2013-08-09 20:22:31 »

Well from that small info you give us, i would assume that you calculate the delta time between the game updates.

You could use this delta time to calculate the time for each color shift like this:

 1  2  3  4  5  6  7 `public void gameLoop() {      // calculate the delta here      shiftTime += delta // then add it up on a variable      if(shiftTime >= 3) {        // You do not need to make a new Background every time you shift colors        // just make a method that shifts the color for your background.        background.shiftColor(Color, deviation)`

If you don't calculate the delta time here is how you do that:

 1  2  3  4  5  6  7  8  9  10  11 `public void gameLoop() {      long loopTime = System.nanoTime();      long optimalLoopTime = 1000000000/60 // 60 is your target FPS      while(running) {            long now = System.nanoTime();            long updateLength = now - loopTime;            lastLoopTime = now;            double delta = updateLength / ((double)OPTIMAL_TIME);            // render your stuff      }}`

Code taken from: Game Loops!
VaPS
 « Reply #2 - Posted 2013-08-09 20:26:15 »

Excuse my messy describing, this is to clear things up:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `public static void update() {      boolean bool = false;      r += s;      if (r > cmax) {         debug1 = System.currentTimeMillis();         r = cmax;         s = -s;         bool = true;      } else if (r < cmin) {         debug2 = System.currentTimeMillis();         r = cmin;         s = -s;         bool = true;      }      c = new Color((int) r,c.getGreen(),c.getBlue());      if (bool) System.out.println(debug1-debug2);   }`

s is speed, c is colour, r is amount of red in the original colour
cmax & cmin make the interval r is allowed to be inside.

In the Background class, this method is called 60 times/second.

What I want is the console to print out when r has reached a peak from another.
I'm successful doing so, but I don't know what the speed's value is supposed to be.

Edit: A simpler way of saying this:
I want value r to increase s in the time t.
The update method increases r with s and is called 60 times/second.
What is the value of s?

davedes
 « Reply #3 - Posted 2013-08-09 20:30:52 »

I'm not really sure what you're trying to accomplish; but best guess is that you are having trouble shifting smoothly from one value to another.

You should look into easing functions and linear interpolation. Easing functions look like this:
https://github.com/mattdesl/cisc226game/blob/master/SpaceGame/src/space/engine/easing/Easing.java

The code might look like this:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 `void update() {    timer += delta;        //easing start/end values    float start = 1f; //starting brightness    float end = 0.45f; //ending brightness        //returns a value between start and end, based on time passed and easing function    float value = (timer > duration)              ? end            : Easing.LINEAR.ease(timer, start, (end-start), duration);            //starting color      Color orig = Color.PINK;            Color result = new Color((int)(orig.getRed() * val), (int)(orig.getGreen() * val),                     (int)(orig.getBlue() * val), orig.getAlpha());      setColor(result);      draw( ... );}`

Linear interpolation is also useful to know; it's a simple way of "mixing" two values based on an interpolation factor.
For example, here is a basic color lerp:

http://en.wikipedia.org/wiki/Linear_interpolation

For more advanced color shifting you will probably want to work in HSL or HSV color space. That way, you adjust only one aspect (hue, saturation, or brightness). Then you convert to RGB before rendering.

All of this stuff will transfer nicely when/if you pick up GLSL.

VaPS
 « Reply #4 - Posted 2013-08-09 21:21:58 »

Well from that small info you give us, i would assume that you calculate the delta time between the game updates.
I'm not really sure what you're trying to accomplish; but best guess is that you are having trouble shifting smoothly from one value to another.
Check Reply #2 if you want a more proper explanation of my problem.

Nate

« JGO Bitwise Duke »

Medals: 164
Projects: 4
Exp: 14 years

Esoteric Software

 « Reply #5 - Posted 2013-08-10 10:31:40 »

Also see InterpolationTest for nice graphs of each interpolation.

davedes
 « Reply #6 - Posted 2013-08-10 13:53:37 »

Well from that small info you give us, i would assume that you calculate the delta time between the game updates.
I'm not really sure what you're trying to accomplish; but best guess is that you are having trouble shifting smoothly from one value to another.
Check Reply #2 if you want a more proper explanation of my problem.
Are you or are you not trying to animate one color to another? Just use the code I posted, or any other easing/interpolation utility.

Using a "speed" variable and a series of if-else statements is not the correct approach...

VaPS
 « Reply #7 - Posted 2013-08-10 18:21:30 »

Well from that small info you give us, i would assume that you calculate the delta time between the game updates.
I'm not really sure what you're trying to accomplish; but best guess is that you are having trouble shifting smoothly from one value to another.
Check Reply #2 if you want a more proper explanation of my problem.
Are you or are you not trying to animate one color to another? Just use the code I posted, or any other easing/interpolation utility.

Using a "speed" variable and a series of if-else statements is not the correct approach...
See Reply #2 for a much more narrowed down question. I simply want to make my value rise to a specific value over a specific time in seconds. It's all about how to calculate the speed really.

sirkarpfen

Junior Devvie

Medals: 2
Exp: 2 years

 « Reply #8 - Posted 2013-08-10 20:50:44 »

With my posted example you can narrow down the time between the frames.

 1  2  3  4  5  6  7 `public void gameLoop() {      // calculate the delta here      shiftTime += delta // then add it up on a variable      if(shiftTime >= 3) {        // You do not need to make a new Background every time you shift colors        // just make a method that shifts the color for your background.        background.shiftColor(Color, deviation)`

if shiftTime == 3, 3 seconds have passed. You then can call your update method inside or do whatever you want to. This way you get pretty good control of how often the update() happens, just alter the 3 to whatever you want. You then can alter your r value with a fixed amount, and just change the speed of the update() to happen .
davedes
 « Reply #9 - Posted 2013-08-10 20:58:41 »

Quote
I simply want to make my value rise to a specific value over a specific time in seconds. It's all about how to calculate the speed really.

* face palm *

This is what my earlier posted code does.

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 theagentd (22 views) 2015-05-27 22:21:17 theagentd (30 views) 2015-05-27 22:21:08 CopyableCougar4 (21 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (32 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21 BurntPizza (79 views) 2015-05-10 15:53:18 FrozenShade (64 views) 2015-05-07 09:11:21 TheLopais (227 views) 2015-05-06 13:36:48
 Spasi 23x Rayvolution 23x Riven 16x theagentd 15x Drenius 15x BurntPizza 15x ra4king 13x opiop65 12x EgonOlsen 11x princec 11x DavidBVal 11x Husk 11x KevinWorkman 10x scanevaro 8x orangepascal 8x kevglass 8x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 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