Files
pvvmud/common/lib/srvcli/direction.C
2025-03-05 08:37:43 +01:00

134 lines
3.4 KiB
C

/*
* PVVMUD a 3D MUD
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <math.h>
#include "pvvmud.H"
#include "direction.H"
#define NORMALIZE(index) \
direction[index] -= floor(direction[index]/360.0) * 360.0;
CDirection::CDirection(){
direction[0] = 0;
direction[1] = 0;
direction[2] = 0;
}
CDirection::CDirection(double * direction){
this->direction[0] = direction[0];
this->direction[1] = direction[1];
this->direction[2] = direction[2];
normalize();
}
CDirection::CDirection(double h, double r, double p){
direction[0] = h;
direction[1] = r;
direction[2] = p;
normalize();
}
CDirection& CDirection::operator= (const CDirection& direction) {
this->direction[0] = direction.direction[0];
this->direction[1] = direction.direction[1];
this->direction[2] = direction.direction[2];
return *this;
}
CDirection& CDirection::operator+= (const CDirection& direction) {
this->direction[0] += direction.direction[0];
this->direction[1] += direction.direction[1];
this->direction[2] += direction.direction[2];
normalize();
return *this;
}
CDirection CDirection::operator+ (const CDirection& direction) {
CDirection result;
result.direction[0] = this->direction[0] + direction.direction[0];
result.direction[1] = this->direction[1] + direction.direction[1];
result.direction[2] = this->direction[2] + direction.direction[2];
result.normalize();
return result;
}
CDirection CDirection::operator- () const {
CDirection result;
result.direction[0] = -direction[0];
result.direction[1] = -direction[1];
result.direction[2] = -direction[2];
result.normalize();
return result;
}
CDirection CDirection::operator* (double scale) const {
CDirection result;
result.direction[0] = direction[0]*scale;
result.direction[1] = direction[1]*scale;
result.direction[2] = direction[2]*scale;
result.normalize();
return result;
}
void CDirection::normalize(){
NORMALIZE(0);
NORMALIZE(1);
NORMALIZE(2);
}
double * CDirection::getVector(){
return direction;
}
double CDirection::getValue(int ii) const {
return direction[ii];
}
void CDirection::setValue(int ii,double value) {
direction[ii] += value;
NORMALIZE(ii);
}
double CDirection::rotateHeading(double deltaHeading){
direction[0] += deltaHeading;
NORMALIZE(0);
return direction[0];
}
double CDirection::rotateRoll(double deltaRoll){
direction[1] += deltaRoll;
NORMALIZE(1);
return direction[1];
}
double CDirection::rotatePitch(double deltaPitch){
direction[2] += deltaPitch;
NORMALIZE(2);
return direction[2];
}
ostream& operator<<(ostream&s,const CDirection& d){
return s << "(" << d.getHeading() << "," << d.getRoll()
<< "," << d.getPitch() << ")";
}