-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshark.cpp
73 lines (62 loc) · 1.61 KB
/
shark.cpp
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
#include "shark.h"
#include "teximage.h"
#include "world.h"
#include "behavior.h"
#include <list>
#include <stdio.h>
#include <stdlib.h>
//近傍の限界距離
const int RADIUS = 50;
Shark::Shark(vec2d pos_, vec2d velo_, GLuint shader)
: Fish(pos_, velo_, NULL, 0.9) //最高速度を遅めに設定
{
//static TexImageWithShader img("shark.png", 19, 37, shader);
static TexImageWithShader img("newshark.jpg", 26, 60, shader);
tex = &img;
}
void Shark::update(World* p_world)
{
//最近傍のfishを探す
Fish* nearest_fish = NULL;
auto lfishes = p_world->get_neighborfishes(partidx);
for (auto& lfish : lfishes)
{
double lsq = (pos - lfish->get_pos()).lengthsq();
if (lsq < RADIUS*RADIUS)
{
if (!nearest_fish)
{
nearest_fish = lfish;
continue;
}
if (lsq < (nearest_fish->get_pos() - pos).lengthsq())
{
nearest_fish = lfish;
}
}
}
//追従行動および放浪行動を行う
vec2d force;
double fmax = 6.65;
for (;;)
{
if (nearest_fish)
force += behavior->seek(this, p_world, nearest_fish);
if (force.lengthsq() > fmax*fmax) break;
force += behavior->randomwalk(this, p_world);
break;
}
if (force.lengthsq() > fmax*fmax) force = force.norm()*fmax;
double M = 1.80;
velo += force / M;
if (velo.lengthsq() > 0.001)
dire = velo.norm();
if (velo.lengthsq() > max_speed*max_speed)
velo = velo.norm()*max_speed;
pos += velo;
if (pos.x < 0) pos.x += p_world->get_width();
if (pos.x > p_world->get_width()) pos.x -= p_world->get_width();
if (pos.y < 0) pos.y += p_world->get_height();
if (pos.y > p_world->get_height()) pos.y -= p_world->get_height();
}