INET Framework for OMNeT++/OMNEST
RectangleMobility.cc
Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2005 Andras Varga
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #include "RectangleMobility.h"
00019 #include "FWMath.h"
00020 
00021 
00022 Define_Module(RectangleMobility);
00023 
00024 
00030 void RectangleMobility::initialize(int stage)
00031 {
00032     BasicMobility::initialize(stage);
00033 
00034     EV << "initializing RectangleMobility stage " << stage << endl;
00035 
00036     if (stage == 1)
00037     {
00038         x1 = par("x1");
00039         y1 = par("y1");
00040         x2 = par("x2");
00041         y2 = par("y2");
00042         updateInterval = par("updateInterval");
00043         speed = par("speed");
00044 
00045         // if the initial speed is lower than 0, the node is stationary
00046         stationary = (speed == 0);
00047 
00048         // calculate helper vars
00049         double dx = x2-x1;
00050         double dy = y2-y1;
00051         corner1 = dx;
00052         corner2 = corner1 + dy;
00053         corner3 = corner2 + dx;
00054         corner4 = corner3 + dy;
00055 
00056         // determine start position
00057         double startPos = par("startPos");
00058         startPos = fmod(startPos,4);
00059         if (startPos<1)
00060             d = startPos*dx; // top side
00061         else if (startPos<2)
00062             d = corner1 + (startPos-1)*dy; // right side
00063         else if (startPos<3)
00064             d = corner2 + (startPos-2)*dx; // bottom side
00065         else
00066             d = corner3 + (startPos-3)*dy; // left side
00067         calculateXY();
00068         WATCH(d);
00069         updatePosition();
00070 
00071         // host moves the first time after some random delay to avoid synchronized movements
00072         if (!stationary)
00073             scheduleAt(simTime() + uniform(0, updateInterval), new cMessage("move"));
00074     }
00075 }
00076 
00077 
00082 void RectangleMobility::handleSelfMsg(cMessage * msg)
00083 {
00084     move();
00085     updatePosition();
00086     scheduleAt(simTime() + updateInterval, msg);
00087 }
00088 
00089 void RectangleMobility::move()
00090 {
00091     d += speed * updateInterval;
00092     while (d<0) d+=corner4;
00093     while (d>=corner4) d-=corner4;
00094 
00095     calculateXY();
00096     EV << " xpos= " << pos.x << " ypos=" << pos.y << " speed=" << speed << endl;
00097 }
00098 
00099 void RectangleMobility::calculateXY()
00100 {
00101     if (d < corner1)
00102     {
00103         // top side
00104         pos.x = x1 + d;
00105         pos.y = y1;
00106     }
00107     else if (d < corner2)
00108     {
00109         // right side
00110         pos.x = x2;
00111         pos.y = y1 + d - corner1;
00112     }
00113     else if (d < corner3)
00114     {
00115         // bottom side
00116         pos.x = x2 - d + corner2;
00117         pos.y = y2;
00118     }
00119     else
00120     {
00121         // left side
00122         pos.x = x1;
00123         pos.y = y2 - d + corner3;
00124     }
00125 }