-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenShake.java
157 lines (124 loc) · 4.12 KB
/
ScreenShake.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2021 Spooky Games
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package games.spooky.gdx.gfx.demo.fx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.math.MathUtils;
import games.spooky.gdx.gfx.TemporalEffect;
/**
* @see http://jonny.morrill.me/blog/view/14
*/
public class ScreenShake implements TemporalEffect {
private final float duration;
private final float amplitude;
private final ShakeSamples samplesX;
private final ShakeSamples samplesY;
private final Camera camera;
private float time;
private float totalDeltaX;
private float totalDeltaY;
/**
*
* @param camera
* @param duration
* Length of the shake in seconds
* @param frequency
* Frequency of the shake in hertz
* @param amplitude
*/
public ScreenShake(Camera camera, float duration, float frequency, float amplitude) {
super();
this.camera = camera;
this.duration = duration;
this.amplitude = amplitude;
this.samplesX = new ShakeSamples(duration, frequency, amplitude);
this.samplesY = new ShakeSamples(duration, frequency, amplitude);
}
@Override
public void reset() {
time = 0f;
totalDeltaX = totalDeltaY = 0f;
}
@Override
public boolean update(float deltaTime) {
time += deltaTime;
if (time > duration) {
camera.translate(-totalDeltaX, -totalDeltaY, 0);
camera.update();
return true;
}
float deltaX = samplesX.amplitude(time);
float deltaY = samplesY.amplitude(time);
float newTotalDeltaX = totalDeltaX + deltaX;
float amplitudeLeftX = amplitude * (duration - time);
if(newTotalDeltaX > amplitudeLeftX) {
deltaX = totalDeltaX - amplitudeLeftX;
newTotalDeltaX = totalDeltaX + deltaX;
}
totalDeltaX = newTotalDeltaX;
camera.translate(deltaX, deltaY, 0);
camera.update();
return false;
}
private static class ShakeSamples {
// The duration in seconds
private final float duration;
// The frequency in Hz
private final float frequency;
private final float[] samples;
public ShakeSamples(float duration, float frequency, float amplitude) {
super();
this.duration = duration;
this.frequency = frequency;
// sample count = number of peaks/valleys in the shake
int sampleCount = (int) (duration * frequency);
// Populate the samples array with randomized values between
// -amplitude and +amplitude
samples = new float[sampleCount];
for (int i = 0; i < sampleCount; i++)
samples[i] = MathUtils.random(-amplitude, amplitude);;
}
float amplitude(float time) {
// Get the previous and next sample
float s = time * frequency;
int s0 = MathUtils.floor(s);
int s1 = s0 + 1;
// Get the current decay
float k = decay(time);
// Return the current amplitude
return (noise(s0) + (s - s0) * (noise(s1) - noise(s0))) * k;
};
private float noise(int sample) {
// Retrieve the randomized value from the samples
if (sample >= samples.length)
return 0;
return samples[sample];
};
private float decay(float time) {
// Linear decay
if (time >= duration)
return 0f;
return (duration - time) / duration;
};
}
}