Project

General

Profile

Download (8.3 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / wind / WindEffectsManager.java @ 30f337e5

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.wind;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
import org.distorted.library.effect.MatrixEffectScale;
25
import org.distorted.library.effect.MatrixEffectShear;
26
import org.distorted.library.effect.VertexEffectDeform;
27
import org.distorted.library.effect.VertexEffectWave;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.type.Dynamic;
30
import org.distorted.library.type.Dynamic5D;
31
import org.distorted.library.type.Static1D;
32
import org.distorted.library.type.Static3D;
33
import org.distorted.library.type.Static4D;
34
import org.distorted.library.type.Static5D;
35

    
36
class WindEffectsManager
37
  {
38
  private static final long TIME_TO_GET_UP    = 400; // time it takes for the max wind to
39
                                                     // make the flag horizontal (milliseconds)
40
  private static final long TIME_TO_FALL_DOWN =2000; // time it takes for the flag to
41
                                                     // fall back down if no wind (milliseconds)
42
  private long mLastTime, mPausedTime;
43
  private float mWind;
44
  private final int mHeight;
45
    private final int mWidth;
46

    
47
  private final Static3D  shearFactor;
48
  private final Static3D  scaleFactor;
49
  private final Static3D  deformForce;
50
  private final Static5D  windFactor11;
51
    private final Static5D windFactor12;
52
  private final Dynamic5D windDynamic1;
53
  private final Static5D  windFactor21;
54
    private final Static5D windFactor22;
55
  private final Dynamic5D windDynamic2;
56
  private final Static5D  windFactor31;
57
    private final Static5D windFactor32;
58
  private final Dynamic5D windDynamic3;
59
  private final Static5D  windFactor41;
60
    private final Static5D windFactor42;
61
  private final Dynamic5D windDynamic4;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  WindEffectsManager(int width, int height)
66
    {
67
    mLastTime = 0;
68

    
69
    mWidth = width;
70
    mHeight= height;
71

    
72
    shearFactor = new Static3D(0,0,0);
73
    scaleFactor = new Static3D(1,1,1);
74
    deformForce = new Static3D(mWidth/3,0,0);
75

    
76
    windFactor11 = new Static5D(mHeight/10,mHeight/5, 180, 0, 90);
77
    windFactor12 = new Static5D(mHeight/10,mHeight/5,-180, 0, 90);
78
    windDynamic1 = new Dynamic5D(1000,0.0f);
79
    windDynamic1.add(windFactor11);
80
    windDynamic1.add(windFactor12);
81
    windDynamic1.setMode(Dynamic.MODE_JUMP);
82
    windDynamic1.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
83

    
84
    windFactor21 = new Static5D(mHeight/10,mHeight/5,-180, 90, 10);
85
    windFactor22 = new Static5D(mHeight/10,mHeight/5,+180, 90, 10);
86
    windDynamic2 = new Dynamic5D(1000,0.0f);
87
    windDynamic2.add(windFactor21);
88
    windDynamic2.add(windFactor22);
89
    windDynamic2.setMode(Dynamic.MODE_JUMP);
90
    windDynamic2.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
91

    
92
    windFactor31 = new Static5D(mHeight/10,mHeight/10,-180, 90, 90);
93
    windFactor32 = new Static5D(mHeight/10,mHeight/10,+180, 90, 90);
94
    windDynamic3 = new Dynamic5D(1000,0.0f);
95
    windDynamic3.add(windFactor31);
96
    windDynamic3.add(windFactor32);
97
    windDynamic3.setMode(Dynamic.MODE_JUMP);
98
    windDynamic3.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
99

    
100
    windFactor41 = new Static5D(mHeight/10,mHeight/5,-180,  0,  0);
101
    windFactor42 = new Static5D(mHeight/10,mHeight/5,+180,  0,  0);
102
    windDynamic4 = new Dynamic5D(1000,0.0f);
103
    windDynamic4.add(windFactor41);
104
    windDynamic4.add(windFactor42);
105
    windDynamic4.setMode(Dynamic.MODE_JUMP);
106
    windDynamic4.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  void apply(DistortedEffects effects)
112
    {
113
    Static3D midLeft   = new Static3D(-mWidth/2,0,0);
114
    Static3D midRight  = new Static3D( mWidth/2,0,0);
115
    Static3D tadRight  = new Static3D( mWidth/4,0,0);
116
    Static4D windRegion= new Static4D(0,0,0,mHeight);
117

    
118
    setWind(0);
119

    
120
    effects.apply( new MatrixEffectScale(scaleFactor) );
121
    effects.apply( new MatrixEffectShear(shearFactor,midLeft) );
122

    
123
    effects.apply( new VertexEffectDeform(deformForce, new Static1D(mWidth/2), midRight) );
124
    effects.apply( new VertexEffectWave(windDynamic1, midRight, windRegion) );
125
    effects.apply( new VertexEffectWave(windDynamic2, midRight, windRegion) );
126
    effects.apply( new VertexEffectWave(windDynamic3, midRight, windRegion) );
127
    effects.apply( new VertexEffectWave(windDynamic4, tadRight, windRegion) );
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  private void setWind(int wind)
133
    {
134
    mWind = wind;
135

    
136
    float tanAngle = (wind-50)/50.0f;
137
    shearFactor.set1(tanAngle);
138
    scaleFactor.set0(1/(float)Math.sqrt(1+tanAngle*tanAngle));
139

    
140
    windDynamic1.setDuration( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
141
    windDynamic2.setDuration( wind > 0 ? 720 +  8000/wind : Long.MAX_VALUE);
142
    windDynamic3.setDuration( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
143

    
144
    float wave2 = mHeight*( 0.05f + 0.002f*wind);
145
    windFactor21.set0(wave2);
146
    windFactor22.set0(wave2);
147

    
148
    float wave3 = (mHeight/(wind+5.0f));
149
    windFactor31.set0(wave3);
150
    windFactor32.set0(wave3);
151

    
152
    float wave4 = (mHeight*(wind*(100-wind)/50000.0f));
153
    windFactor41.set0(wave4);
154
    windFactor42.set0(wave4);
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  void pauseWind()
160
    {
161
    mPausedTime = System.currentTimeMillis();
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  void resumeWind()
167
    {
168
    long gap = System.currentTimeMillis() - mPausedTime;
169
    mLastTime += gap;
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  synchronized void increaseWind(long currTime)
175
    {
176
    if( mLastTime>0 )
177
      {
178
      long diff = currTime-mLastTime;
179
      float diffWind = (100.0f*diff/TIME_TO_GET_UP);
180

    
181
      mWind += diffWind;
182
      if( mWind>100.0f ) mWind=100.0f;
183

    
184
      setWind( (int)mWind );
185
      }
186

    
187
    mLastTime = currTime;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  synchronized void decreaseWind(long currTime)
193
    {
194
    if( mLastTime>0 )
195
      {
196
      long diff = currTime-mLastTime;
197
      float diffWind = (100.0f*diff/TIME_TO_FALL_DOWN);
198

    
199
      mWind -= diffWind;
200
      if( mWind<0.0f ) mWind=0.0f;
201

    
202
      setWind( (int)mWind );
203
      }
204

    
205
    mLastTime = currTime;
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
  }
(2-2/5)