SDXFrameWork  0.13
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Motion.h
1 //Copyright © 2014 SDXFramework
2 //[License]GNU Affero General Public License, version 3
3 //[Contact]http://sourceforge.jp/projects/dxframework/
4 #pragma once
5 namespace SDX
6 {
7  namespace MOTION
8  {
9 
10  //コピペ用ひな形
11  /*
12  template <class TSpeed>
13  class Base : public IMotion
14  {
15  private:
16  TSpeed speed;
17  public:
18  Base(const TSpeed &速度):
19  speed(速度)
20  {}
21 
22  void Update(TShape* 移動対象) override
23  {
24  移動対象
25  }
26  };
27  */
28 
29 
31  class Stop : public IMotion
32  {
33  public:
34  Stop()
35  {}
36  };
37 
38  template <class TSpeed>
41  class ToFront : public IMotion
42  {
43  private:
44  TSpeed speed;
45  public:
46 
48  ToFront(const TSpeed &速度) :
49  speed(速度)
50  {}
51 
52  bool Update(IPosition* 移動対象) override
53  {
54  移動対象->MoveA(speed.Update(), 移動対象->GetAngle());
55 
56  return true;
57  }
58  };
59 
60  template <class TSpeed>
63  class Bound : public IMotion
64  {
65  private:
66  TSpeed speed;
67  double 進行方向;
68  Rect 移動範囲;
69  public:
70 
72  Bound(const TSpeed &速度, const Rect& 移動範囲, double 進行方向) :
73  speed(速度),
74  移動範囲(移動範囲),
75  進行方向(進行方向)
76  {}
77 
78  bool Update(IPosition* 移動対象) override
79  {
80  //範囲外にいる
81  if (移動対象->GetX() < 移動範囲.GetLeft())
82  {
83  if (進行方向 > PAI / 2 && 進行方向 < PAI * 3 / 2)
84  {
85  進行方向 = -PAI - 進行方向;
86  }
87  }
88 
89  if (移動対象->GetX() > 移動範囲.GetRight())
90  {
91  if (進行方向 < PAI / 2 || 進行方向 > PAI * 3 / 2)
92  {
93  進行方向 = PAI - 進行方向;
94  }
95  }
96  else if (移動対象->GetY() < 移動範囲.GetTop())
97  {
98  if (進行方向 > PAI)
99  {
100  進行方向 = PAI * 2 - 進行方向;
101  }
102  }
103  else if (移動対象->GetY() > 移動範囲.GetBottom())
104  {
105  if (進行方向 < PAI)
106  {
107  進行方向 = -進行方向;
108  }
109  }
110 
111  if (進行方向 < 0) 進行方向 += PAI * 2;
112  if (進行方向 > PAI * 2) 進行方向 -= PAI * 2;
113 
114  移動対象->MoveA(speed.Update(), 進行方向);
115 
116  return true;
117  }
118  };
119 
120  template <class TSpeed>
123  class ToPoint : public IMotion
124  {
125  private:
126  TSpeed speed;
127  Point 目標座標;
128  public:
130  ToPoint(const TSpeed &速度, const Point& 目標座標) :
131  speed(速度),
132  目標座標(目標座標)
133  {}
134 
135  bool Update(IPosition* 移動対象) override
136  {
137  //移動対象
138  const double nowSpeed = speed.Update();
139  const double lx = 目標座標.GetX() - 移動対象->GetX();
140  const double ly = 目標座標.GetY() - 移動対象->GetY();
141 
142  if (lx * lx + ly * ly <= nowSpeed * nowSpeed)
143  {
144  移動対象->SetPos(目標座標.GetX(), 目標座標.GetY());
145  return false;
146  }
147  else
148  {
149  const double angle = atan2(ly, lx);
150  移動対象->MoveA(nowSpeed, angle);
151  }
152 
153  return true;
154  }
155  };
156 
157  template <class TSpeed>
160  class Orbit : public IMotion
161  {
162  private:
163  TSpeed speed;
164  Point 軌道中心;
165  double 半径X;
166  double 半径Y;
167  double 角度;
168  double 前移動量X;
169  double 前移動量Y;
170  public:
171 
173  Orbit(const TSpeed &角速度 , double 半径X , double 半径Y , double 初期進行方向 = 0.0) :
174  speed(角速度),
175  半径X(半径X),
176  半径Y(半径Y),
177  角度(初期進行方向 - PAI / 2),
178  前移動量X(cos(初期進行方向 - PAI/2) * 半径X),
179  前移動量Y(sin(初期進行方向 - PAI/2) * 半径Y)
180  {}
181 
182  bool Update(IPosition* 移動対象) override
183  {
184  //移動対象
185  角度 += speed.Update();
186 
187  const double nextX = cos(角度) * 半径X;
188  const double nextY = sin(角度) * 半径Y;
189 
190  移動対象->Move(nextX - 前移動量X, nextY - 前移動量Y);
191 
192  前移動量X = nextX;
193  前移動量Y = nextY;
194 
195  return true;
196  }
197  };
198 
201  class Vibrate : public IMotion
202  {
203  private:
204  double 最大振れ幅;
205  double 移動X = 0;
206  double 移動Y = 0;
207  public:
208 
210  Vibrate(double 最大振れ幅) :
211  最大振れ幅(最大振れ幅)
212  {}
213 
214  bool Update(IPosition* 移動対象) override
215  {
216  double randX = Rand::Get(-最大振れ幅, 最大振れ幅);
217  double randY = Rand::Get(-最大振れ幅, 最大振れ幅);
218  移動対象->Move(randX-移動X, randY-移動Y);
219  移動X = randX;
220  移動Y = randY;
221 
222  return true;
223  }
224  };
225  }
226 }
目標座標に移動.
Definition: Motion.h:123
const double PAI
円周率
Definition: SDX.h:26
矩形を表す図形クラス.
Definition: Rect.h:22
virtual double GetAngle() const
角度を取得する.
Definition: IPosition.h:56
double GetBottom() const
下端のY座標を取得.
Definition: Rect.h:136
double GetTop() const
上端のY座標を取得.
Definition: Rect.h:124
double GetRight() const
右端のX座標を取得.
Definition: Rect.h:130
double GetLeft() const
左端のX座標を取得.
Definition: Rect.h:118
double GetY() const override
Y座標を取得.
Definition: Point.h:68
円周上を移動.
Definition: Motion.h:160
Bound(const TSpeed &速度, const Rect &移動範囲, double 進行方向)
速度と移動範囲、最初の移動方向を指定.
Definition: Motion.h:72
点を表す図形クラス.
Definition: Point.h:22
移動方法のインターフェース.
Definition: IMotion.h:14
ToFront(const TSpeed &速度)
速度を指定.
Definition: Motion.h:48
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:135
virtual double GetY() const =0
Y座標を取得.
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:214
Vibrate(double 最大振れ幅)
コンストラクタ.
Definition: Motion.h:210
virtual void Move(double X移動量, double Y移動量)=0
相対座標で移動.
ToPoint(const TSpeed &速度, const Point &目標座標)
コンストラクタ.
Definition: Motion.h:130
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:182
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:78
範囲内で跳ね返る.
Definition: Motion.h:63
その場所で停止.
Definition: Motion.h:31
前方に移動.
Definition: Motion.h:41
virtual void SetPos(double X座標, double Y座標)=0
指定座標に移動.
位置と方向を持つクラスのインターフェース.
Definition: IPosition.h:11
範囲内で振動.
Definition: Motion.h:201
virtual double GetX() const =0
X座標を取得.
static double Get(double 最大値)
0~最大値の乱数を取得.
Definition: Rand.h:29
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:52
double GetX() const override
X座標を取得.
Definition: Point.h:63
void MoveA(double 距離, double 方向)
極座標で移動.
Definition: IPosition.h:41
Orbit(const TSpeed &角速度, double 半径X, double 半径Y, double 初期進行方向=0.0)
コンストラクタ.
Definition: Motion.h:173