Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindEffectsManager.java @ 513b2e9c

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 lastTime;
43
  private float mWind;
44
  private int mHeight, mWidth;
45

    
46
  private Static3D  shearFactor;
47
  private Static3D  scaleFactor;
48
  private Static3D  deformForce;
49
  private Static5D  windFactor11, windFactor12;
50
  private Dynamic5D windDynamic1;
51
  private Static5D  windFactor21, windFactor22;
52
  private Dynamic5D windDynamic2;
53
  private Static5D  windFactor31, windFactor32;
54
  private Dynamic5D windDynamic3;
55
  private Static5D  windFactor41, windFactor42;
56
  private Dynamic5D windDynamic4;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  WindEffectsManager(int width, int height)
61
    {
62
    lastTime = 0;
63

    
64
    mWidth = width;
65
    mHeight= height;
66

    
67
    shearFactor = new Static3D(0,0,0);
68
    scaleFactor = new Static3D(1,1,1);
69
    deformForce = new Static3D(mWidth/3,0,0);
70

    
71
    windFactor11 = new Static5D(mHeight/10,mHeight/5, 180, 0, 90);
72
    windFactor12 = new Static5D(mHeight/10,mHeight/5,-180, 0, 90);
73
    windDynamic1 = new Dynamic5D(1000,0.0f);
74
    windDynamic1.add(windFactor11);
75
    windDynamic1.add(windFactor12);
76
    windDynamic1.setMode(Dynamic.MODE_JUMP);
77
    windDynamic1.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
78

    
79
    windFactor21 = new Static5D(mHeight/10,mHeight/5,-180, 90, 10);
80
    windFactor22 = new Static5D(mHeight/10,mHeight/5,+180, 90, 10);
81
    windDynamic2 = new Dynamic5D(1000,0.0f);
82
    windDynamic2.add(windFactor21);
83
    windDynamic2.add(windFactor22);
84
    windDynamic2.setMode(Dynamic.MODE_JUMP);
85
    windDynamic2.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
86

    
87
    windFactor31 = new Static5D(mHeight/10,mHeight/10,-180, 90, 90);
88
    windFactor32 = new Static5D(mHeight/10,mHeight/10,+180, 90, 90);
89
    windDynamic3 = new Dynamic5D(1000,0.0f);
90
    windDynamic3.add(windFactor31);
91
    windDynamic3.add(windFactor32);
92
    windDynamic3.setMode(Dynamic.MODE_JUMP);
93
    windDynamic3.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
94

    
95
    windFactor41 = new Static5D(mHeight/10,mHeight/5,-180,  0,  0);
96
    windFactor42 = new Static5D(mHeight/10,mHeight/5,+180,  0,  0);
97
    windDynamic4 = new Dynamic5D(1000,0.0f);
98
    windDynamic4.add(windFactor41);
99
    windDynamic4.add(windFactor42);
100
    windDynamic4.setMode(Dynamic.MODE_JUMP);
101
    windDynamic4.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  void apply(DistortedEffects effects)
107
    {
108
    Static3D midLeft   = new Static3D(-mWidth/2,0,0);
109
    Static3D midRight  = new Static3D( mWidth/2,0,0);
110
    Static3D tadRight  = new Static3D( mWidth/4,0,0);
111
    Static4D windRegion= new Static4D(0,0,0,mHeight);
112

    
113
    setWind(0);
114

    
115
    effects.apply( new MatrixEffectScale(scaleFactor) );
116
    effects.apply( new MatrixEffectShear(shearFactor,midLeft) );
117

    
118
    effects.apply( new VertexEffectDeform(deformForce, new Static1D(mWidth/2), midRight) );
119
    effects.apply( new VertexEffectWave(windDynamic1, midRight, windRegion) );
120
    effects.apply( new VertexEffectWave(windDynamic2, midRight, windRegion) );
121
    effects.apply( new VertexEffectWave(windDynamic3, midRight, windRegion) );
122
    effects.apply( new VertexEffectWave(windDynamic4, tadRight, windRegion) );
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void setWind(int wind)
128
    {
129
    mWind = wind;
130

    
131
    float tanAngle = (wind-50)/50.0f;
132
    shearFactor.set1(tanAngle);
133
    scaleFactor.set0(1/(float)Math.sqrt(1+tanAngle*tanAngle));
134

    
135
    windDynamic1.setDuration( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
136
    windDynamic2.setDuration( wind > 0 ? 720 +  8000/wind : Long.MAX_VALUE);
137
    windDynamic3.setDuration( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
138

    
139
    float wave2 = mHeight*( 0.05f + 0.002f*wind);
140
    windFactor21.set0(wave2);
141
    windFactor22.set0(wave2);
142

    
143
    float wave3 = (mHeight/(wind+5.0f));
144
    windFactor31.set0(wave3);
145
    windFactor32.set0(wave3);
146

    
147
    float wave4 = (mHeight*(wind*(100-wind)/50000.0f));
148
    windFactor41.set0(wave4);
149
    windFactor42.set0(wave4);
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  synchronized void increaseWind(long currTime)
155
    {
156
    if( lastTime>0 )
157
      {
158
      long diff = currTime-lastTime;
159
      float diffWind = (100.0f*diff/TIME_TO_GET_UP);
160

    
161
      mWind += diffWind;
162
      if( mWind>100.0f ) mWind=100.0f;
163

    
164
      setWind( (int)mWind );
165
      }
166

    
167
    lastTime = currTime;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  synchronized void decreaseWind(long currTime)
173
    {
174
    if( lastTime>0 )
175
      {
176
      long diff = currTime-lastTime;
177
      float diffWind = (100.0f*diff/TIME_TO_FALL_DOWN);
178

    
179
      mWind -= diffWind;
180
      if( mWind<0.0f ) mWind=0.0f;
181

    
182
      setWind( (int)mWind );
183
      }
184

    
185
    lastTime = currTime;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
  }
(2-2/5)