]> git.tdb.fi Git - netvis.git/blob - source/packet.cpp
Improve the algorithm for deleting hosts
[netvis.git] / source / packet.cpp
1 /* $Id$
2
3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
6 */
7
8 #include <cmath>
9 #include <cstdlib>
10 #include <msp/gl/immediate.h>
11 #include <msp/gl/matrix.h>
12 #include <msp/gl/transform.h>
13 #include <msp/time/units.h>
14 #include "host.h"
15 #include "packet.h"
16
17 using namespace Msp;
18
19 Packet::Packet(const Host &s, const Host *d, const GL::Color &c, unsigned i):
20         src(s),
21         dest(d),
22         color(c),
23         x(0),
24         size(cbrt(i)*1.5),
25         angle(rand()*M_PI*2/RAND_MAX),
26         rspeed(rand()*M_PI/RAND_MAX-M_PI/2)
27 { }
28
29 void Packet::tick(const Time::TimeDelta &dt)
30 {
31         double f=dt/Time::sec;
32         x+=f;
33         if(x>1)
34                 x=1;
35         angle+=rspeed*f;
36         if(angle<0)
37                 angle+=M_PI*2;
38         if(angle>=M_PI*2)
39                 angle-=M_PI;
40 }
41
42 void Packet::render(GL::PrimitiveBuilder &bld) const
43 {
44         if(x<0)
45                 return;
46         if(!src.get_active() || (dest && !dest->get_active()))
47                 return;
48
49         const Vector2 &spos=src.get_position();
50
51         if(dest)
52         {
53                 const Vector2 &dpos=dest->get_position();
54                 Vector2 pos(spos.x*(1-x)+dpos.x*x, spos.y*(1-x)+dpos.y*x);
55                 Vector2 corner(cos(angle)*size, sin(angle)*size);
56
57                 if(bld.get_type()!=GL::QUADS)
58                 {
59                         bld.end();
60                         bld.begin(GL::QUADS);
61                 }
62                 bld.color(color.r, color.g, color.b, color.a);
63                 bld.vertex(pos.x+corner.x, pos.y+corner.y);
64                 bld.vertex(pos.x-corner.y, pos.y+corner.x);
65                 bld.vertex(pos.x-corner.x, pos.y-corner.y);
66                 bld.vertex(pos.x+corner.y, pos.y-corner.x);
67         }
68         else
69         {
70                 bld.end();
71                 bld.begin(GL::TRIANGLE_FAN);
72                 bld.color(color.r, color.g, color.b, color.a*(1-x));
73                 bld.vertex(spos.x, spos.y);
74                 for(unsigned i=0; i<=24; ++i)
75                 {
76                         float a=i*M_PI/12;
77                         bld.vertex(spos.x+cos(a)*x*200, spos.y+sin(a)*x*200);
78                 }
79         }
80 }