Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindEffectsManager.java @ b041d424

1 815687bb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.DistortedObject;
25 b041d424 Leszek Koltunski
import org.distorted.library.type.Dynamic;
26 21819119 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
27 b041d424 Leszek Koltunski
import org.distorted.library.type.Dynamic5D;
28 21819119 Leszek Koltunski
import org.distorted.library.type.Static3D;
29 b041d424 Leszek Koltunski
import org.distorted.library.type.Static4D;
30
import org.distorted.library.type.Static5D;
31 815687bb Leszek Koltunski
32
class WindEffectsManager
33
  {
34 b041d424 Leszek Koltunski
  private Static3D  shearFactor;
35 21819119 Leszek Koltunski
  private Dynamic3D shearDynamic;
36 b041d424 Leszek Koltunski
  private Static3D  scaleFactor;
37 21819119 Leszek Koltunski
  private Dynamic3D scaleDynamic;
38 b041d424 Leszek Koltunski
  private Static3D  deformForce;
39
  private Static5D  windFactor1, windFactor2;
40
  private Dynamic5D windDynamic;
41 815687bb Leszek Koltunski
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 b041d424 Leszek Koltunski
  WindEffectsManager(DistortedObject obj)
45 815687bb Leszek Koltunski
    {
46 b041d424 Leszek Koltunski
    int h = obj.getHeight();
47
    int w = obj.getWidth();
48
49 21819119 Leszek Koltunski
    shearFactor = new Static3D(0,0,0);
50
    shearDynamic= new Dynamic3D();
51
    shearDynamic.add(shearFactor);
52 815687bb Leszek Koltunski
53 21819119 Leszek Koltunski
    scaleFactor = new Static3D(1,1,1);
54
    scaleDynamic= new Dynamic3D();
55
    scaleDynamic.add(scaleFactor);
56 b041d424 Leszek Koltunski
57
    deformForce = new Static3D(w/3,0,0);
58
59
    windFactor1 = new Static5D(h/10,h/5, 180, 0, 90);
60
    windFactor2 = new Static5D(h/10,h/5,-180, 0, 90);
61
    windDynamic = new Dynamic5D(1000,0.0f);
62
    windDynamic.add(windFactor1);
63
    windDynamic.add(windFactor2);
64
    windDynamic.setMode(Dynamic.MODE_JUMP);
65 21819119 Leszek Koltunski
    }
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
  synchronized void apply(DistortedObject obj, int wind)
70
    {
71 b041d424 Leszek Koltunski
    int h = obj.getHeight();
72
    int w = obj.getWidth();
73
74
    Static3D midLeft = new Static3D(0,h/2,0);
75
    Static3D midRight = new Static3D(w,h/2,0);
76
    Static4D windRegion = new Static4D(0,0,w,h);
77
78 21819119 Leszek Koltunski
    setWind(wind);
79
80 b041d424 Leszek Koltunski
    obj.shear(shearDynamic,midLeft);
81 21819119 Leszek Koltunski
    obj.scale(scaleDynamic);
82 b041d424 Leszek Koltunski
    obj.deform(deformForce,midRight);
83
    obj.wave(windDynamic, midRight, windRegion);
84 21819119 Leszek Koltunski
    }
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88
  synchronized void setWind(int wind)
89
    {
90
    float tanAngle = (wind-50)/50.0f;
91
92
    shearFactor.set2(tanAngle);
93
    scaleFactor.set1(1/(float)Math.sqrt(1+tanAngle*tanAngle));
94 b041d424 Leszek Koltunski
    windDynamic.setDuration( wind > 0 ? 100000/wind : Long.MAX_VALUE);
95 815687bb Leszek Koltunski
    }
96
  }