Particle.cpp

 /*
* Copyright (C) 2009 Yuan Chuan <2eris@163.com>
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this Program.  If not, see <http://www.gnu.org/licenses/>.
*/



#include "Particle.h"

bool Particle::MoveTo(float dx,float dy)
{
//If reached
//----------------------
if(fabs(x-dx)<=vel && fabs(y-dy)<=vel)
   return true;
  
// Vertical   ==90  ==270
//----------------------
if(x==dx){
if(y<dy)  y+=vel;
else      y-=vel;
return false;
}

//Horizontal  ==180 ==360
//-----------------------
if(y==dy){
if(x<dx)  x+=vel;
else      x-=vel;
return false;
}

//Others    <90    >90
//----------------------
float ky=fabs(dy-y);
float kx=fabs(dx-x);
float k=ky/kx;

if(kx>=ky){
if(x<dx)  x+=vel;
else      x-=vel;
if(y<dy)  y+=vel*k;
else      y-=vel*k;
}
else if(kx<ky){
if(x<dx)  x+=vel/k;
else      x-=vel/k;
if(y<dy)  y+=vel;
else      y-=vel;
}

return false;
}



//--------------------------------------------
bool Particle::RotateTo(float cx,float cy)
{
float dx=cx-x;
float dy=cy-y;
float theta=2*PI/0.13;

float newX=dx*cos(-theta)-dy*sin(-theta);
float newY=dx*sin(-theta)+dy*cos(-theta);

return MoveTo(newX+cx,newY+cy);
}




//------------------------------------------------------------------------------

Project Homepage: