Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Calculate a speed of colour-shifting  (Read 568 times)
0 Members and 1 Guest are viewing this topic.
Offline 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?

Offline sirkarpfen

Junior Member


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!
Offline 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?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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:
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/Color.java#L203

More info on 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.

Offline 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.

Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


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

https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/math/Interpolation.java
Also see InterpolationTest for nice graphs of each interpolation.

Offline 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...

Offline 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.

Offline sirkarpfen

Junior Member


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 Smiley.
Offline 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (12 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!