bool Box::isInterior(const Vector3d &point) const { const bool xIsInterior = ( minX < point.getX() && point.getX() < maxX ); const bool yIsInterior = ( minY < point.getY() && point.getY() < maxY ); const bool zIsInterior = ( minZ < point.getZ() && point.getZ() < maxZ ); return xIsInterior && yIsInterior && zIsInterior; }
float Vector3d::inverseVectorDistanceSquared(Vector3d v1, Vector3d v2){ Vector3d tmpVector(v1.getX() - v2.getX(), v1.getY() - v2.getY(), v1.getZ() - v2.getZ()); return(tmpVector.getX() * tmpVector.getX() + tmpVector.getY() * tmpVector.getY() + tmpVector.getZ() * tmpVector.getZ()); }
Box::Box(const Vector3d& pointX, const Vector3d& pointY) : maxX( (pointX.getX() > pointY.getX() ) ? pointX.getX() : pointY.getX() ), minX( (pointX.getX() < pointY.getX() ) ? pointX.getX() : pointY.getX() ), maxY( (pointX.getY() > pointY.getY() ) ? pointX.getY() : pointY.getY() ), minY( (pointX.getY() < pointY.getY() ) ? pointX.getY() : pointY.getY() ), maxZ( (pointX.getZ() > pointY.getZ() ) ? pointX.getZ() : pointY.getZ() ), minZ( (pointX.getZ() < pointY.getZ() ) ? pointX.getZ() : pointY.getZ() ) {}
/** this method returns a vector that is the current vector, rotated by an angle alpha (in radians) around the axis given as parameter. IT IS ASSUMED THAT THE VECTOR PASSED IN IS A UNIT VECTOR!!! */ Vector3d Vector3d::rotate(double alpha, const Vector3d &axis){ //ok... this is fairly hard - check the red book for details. double xP = axis.getX(); double yP = axis.getY(); double zP = axis.getZ(); double cosa = cos(alpha); double sina = sin(alpha); double s[3][3] = {{0, -zP, yP}, {zP, 0, -xP}, {-yP, xP, 0}}; double UUT[3][3] = { {xP*xP, xP*yP, xP*zP}, {yP*xP, yP*yP, yP*zP}, {zP*xP, zP*yP, zP*zP}}; double I[3][3] = {{1,0,0},{0,1,0},{0,0,1}}; double R[3][3] = {{0,0,0},{0,0,0},{0,0,0}}; for (int i=0;i<3;i++) for (int j = 0;j<3;j++) R[i][j] = UUT[i][j] + cosa*(I[i][j] - UUT[i][j]) + sina*s[i][j]; //now that we finally have the transformation matrix set up, we can rotate the vector Vector3d result; result.setX(R[0][0]*x + R[0][1]*y + R[0][2]*z); result.setY(R[1][0]*x + R[1][1]*y + R[1][2]*z); result.setZ(R[2][0]*x + R[2][1]*y + R[2][2]*z); return result; }
Ellipsoid<T>::Ellipsoid(const Vector3d<T>& center, const Vector3d<T>& radii) : IPrimitive3d<T>(center), radii( radii ) { assert(radii.getX() > T{ 0 }); assert(radii.getY() > T{ 0 }); assert(radii.getZ() > T{ 0 }); }
Quaternion<T>::Quaternion(const Vector3d<T>& axis, const T angle) : x(axis.getX() * sin(angle * 0.5f)), y(axis.getY() * sin(angle * 0.5f)), z(axis.getZ() * sin(angle * 0.5f)), w(cos(angle * 0.5f)) { assert(axis.isNormalized()); }
// Sphere Plane Collision - returns true if collsion exists - takes Point3d params bool Collision::SpherePlane(Vector3d& centerSphere, float radiusSphere, Vector3d planeNormal, Point3d p1, Point3d p2, Point3d p3, Point3d p4){ float dist1 = 0.0; float dist2 = 0.0; // this checks for the collsion on both sides of the plane if(RayPlane(planeNormal.getX(), planeNormal.getY(), planeNormal.getZ(), p1.getX(), p1.getY(), p1.getZ(), centerSphere.getX(), centerSphere.getY(), centerSphere.getZ(), -planeNormal.getX(), -planeNormal.getY(), -planeNormal.getZ(), p1, p2, p3, p4, &dist1) || RayPlane(-planeNormal.getX(), -planeNormal.getY(), -planeNormal.getZ(), p1.getX(), p1.getY(), p1.getZ(), centerSphere.getX(), centerSphere.getY(), centerSphere.getZ(), planeNormal.getX(), planeNormal.getY(), planeNormal.getZ(), p1, p2, p3, p4, &dist2)){ if(dist1 > radiusSphere || dist2 > radiusSphere){ // if either distance is > the radiusSphere....no collision return false; } // indicates that we are on the side where dist1 has been determined to be > 0 // the else is the other side of the plane. This calculates the point as well // as the new location of the sphere if(dist1 > 0 ){ centerSphere.setX(centerSphere.getX() + planeNormal.getX() * (radiusSphere - dist1)); centerSphere.setY(centerSphere.getY() + planeNormal.getY() * (radiusSphere - dist1)); centerSphere.setZ(centerSphere.getZ() + planeNormal.getZ() * (radiusSphere - dist1)); }else{ centerSphere.setX(centerSphere.getX() + planeNormal.getX() * (radiusSphere - dist2)); centerSphere.setY(centerSphere.getY() + planeNormal.getY() * (radiusSphere - dist2)); centerSphere.setZ(centerSphere.getZ() + planeNormal.getZ() * (radiusSphere - dist2)); } return true; } return false; }
void Wormhole::packExtraData(OutputFrame::Ptr frame) { Vector3d end = getEndB(); frame->packInt64(end.getX()); frame->packInt64(end.getY()); frame->packInt64(end.getZ()); }
bool Move::doOrder(IGObject * ob){ Vector3d dest = coords->getPosition(); Fleet* fleet = ((Fleet*)(ob->getObjectBehaviour())); unsigned long long distance = dest.getDistance(fleet->getPosition()); unsigned long long max_speed = 3000000; Logger::getLogger()->debug("Moving %lld at %lld speed", distance, max_speed); if(distance <= max_speed){ uint32_t parentid; Logger::getLogger()->debug("Object(%d)->Move->doOrder(): Is arriving at [%lld, %lld, %lld] ", ob->getID(), dest.getX(), dest.getY(), dest.getZ()); fleet->setVelocity(Vector3d(0,0,0)); parentid = ob->getParent(); // recontainerise if necessary int containertype = Game::getGame()->getObjectManager()->getObject(parentid)->getContainerType(); Game::getGame()->getObjectManager()->doneWithObject(parentid); if(fleet->getPosition() != dest && containertype >= 1){ //removeFromParent(); std::set<unsigned int> oblist = ((MTSecTurn*)(Game::getGame()->getTurnProcess()))->getContainerIds(); for(std::set<unsigned int>::reverse_iterator itcurr = oblist.rbegin(); itcurr != oblist.rend(); ++itcurr){ IGObject* testedobject = Game::getGame()->getObjectManager()->getObject(*itcurr); Position3dObjectParam * pos = dynamic_cast<Position3dObjectParam*>(testedobject->getParameterByType(obpT_Position_3D)); SizeObjectParam * size = dynamic_cast<SizeObjectParam*>(testedobject->getParameterByType(obpT_Size)); if(pos == NULL || size == NULL){ Game::getGame()->getObjectManager()->doneWithObject(*itcurr); continue; } Vector3d pos1 = pos->getPosition(); uint64_t size1 = size->getSize(); uint64_t diff = dest.getDistance(pos1); if(diff <= fleet->getSize() / 2 + size1 / 2){ Logger::getLogger()->debug("Container object %d", *itcurr); //if(Game::getGame()->getObject(*itcurr)->getType() <= 2){ //if(*itcurr != id){ if(size1 >= fleet->getSize()){ if(*itcurr != parentid){ ob->removeFromParent(); ob->addToParent(*itcurr); } Game::getGame()->getObjectManager()->doneWithObject(*itcurr); parentid = *itcurr; break; } } Game::getGame()->getObjectManager()->doneWithObject(*itcurr); //} } if(parentid == 0){ ob->removeFromParent(); ob->addToParent(0); } } fleet->setPosition(dest); Message * msg = new Message(); msg->setSubject("Move order complete"); msg->setBody("The fleet '" + ob->getName() + "' has reached it's destination."); msg->addReference(rst_Action_Order, rsorav_Completion); msg->addReference(rst_Object, ob->getID()); msg->addReference(rst_Object, parentid); /* It's parent */ Game::getGame()->getPlayerManager()->getPlayer(fleet->getOwner())->postToBoard(msg); return true; }else{ Vector3d velo = (dest - fleet->getPosition()).makeLength(max_speed); Vector3d arriveat = fleet->getPosition()+velo; Logger::getLogger()->debug("Move->doOrder(%d): Velocity is [%lld, %lld, %lld] (will arrive at [%lld, %lld, %lld])", ob->getID(), velo.getX(), velo.getY(), velo.getZ(), arriveat.getX(), arriveat.getY(), arriveat.getZ()); fleet->setVelocity(velo); uint32_t parentid = ob->getParent(); // recontainerise if necessary uint32_t containertype = Game::getGame()->getObjectManager()->getObject(parentid)->getContainerType(); Game::getGame()->getObjectManager()->doneWithObject(parentid); if(fleet->getPosition() != arriveat && containertype >= 1){ //removeFromParent(); std::set<uint32_t> oblist = ((MTSecTurn*)(Game::getGame()->getTurnProcess()))->getContainerIds(); for(std::set<uint32_t>::reverse_iterator itcurr = oblist.rbegin(); itcurr != oblist.rend(); ++itcurr){ IGObject* testedobject = Game::getGame()->getObjectManager()->getObject(*itcurr); Position3dObjectParam * pos = dynamic_cast<Position3dObjectParam*>(testedobject->getParameterByType(obpT_Position_3D)); SizeObjectParam * size = dynamic_cast<SizeObjectParam*>(testedobject->getParameterByType(obpT_Size)); if(pos == NULL || size == NULL){ Game::getGame()->getObjectManager()->doneWithObject(*itcurr); continue; } Vector3d pos1 = pos->getPosition(); uint64_t size1 = size->getSize(); uint64_t diff = arriveat.getDistance(pos1); if(diff <= fleet->getSize() / 2 + size1 / 2){ Logger::getLogger()->debug("Container object %d", *itcurr); //if(Game::getGame()->getObject(*itcurr)->getType() <= 2){ //if(*itcurr != id){ if(size1 >= fleet->getSize()){ if(*itcurr != parentid){ ob->removeFromParent(); ob->addToParent(*itcurr); } Game::getGame()->getObjectManager()->doneWithObject(*itcurr); parentid = *itcurr; break; } } Game::getGame()->getObjectManager()->doneWithObject(*itcurr); //} } if(parentid == 0){ ob->removeFromParent(); ob->addToParent(0); } } fleet->setPosition(arriveat); return false; } }
Vector3d Matrix::matrixMulVec(const Matrix& mat, const Vector3d& vec){ Vector3d noTranslate = matrixMulDir(mat,vec); return Vector3d( noTranslate.getX()+mat(0,3), noTranslate.getY()+mat(1,3), noTranslate.getZ()+mat(2,3)); }
// subtracts a vector from this one and returns a new one Vector3d Vector3d::operator - (const Vector3d& vector){ return (Vector3d(this->getX() - vector.getX(), this->getY() - vector.getY(), this->getZ() - vector.getZ())); }
bool Vector3d::operator != (const Vector3d& vector){ return (this->x != vector.getX() && this->y != vector.getY() && this->z != vector.getZ()); }
void Vector3d::subtract( Vector3d v ) { this->x -= v.getX(); this->y -= v.getY(); this->z -= v.getZ(); }
// gets the area of a triangle using the Heron formula float Collision::triangleArea(Vector3d p1, Vector3d p2, Vector3d p3){ // get the length of the triagle's sides....a,b, and c float a = sqrt((p2.getX() - p1.getX()) * (p2.getX() - p1.getX()) + (p2.getY() - p1.getY()) * (p2.getY() - p1.getY()) + (p2.getZ() - p1.getZ()) * (p2.getZ() - p1.getZ())); float b = sqrt((p3.getX() - p2.getX()) * (p3.getX() - p2.getX()) + (p3.getY() - p2.getY()) * (p3.getY() - p2.getY()) + (p3.getZ() - p2.getZ()) * (p3.getZ() - p2.getZ())); float c = sqrt((p3.getX() - p1.getX()) * (p3.getX() - p1.getX()) + (p3.getY() - p1.getY()) * (p3.getY() - p1.getY()) + (p3.getZ() - p1.getZ()) * (p3.getZ() - p1.getZ())); // triangle's semi-parameter s float s = (a + b + c) / 2; return (sqrt(s * ((s - a) * (s - b) * (s - c)))); }
float getAngleAccelerationZ( float x1, float x2, float x3, const Vector3d& I, const Vector3d& N) { return ( N.getZ() + ( I.getX() - I.getY() ) * x1 * x2 ) / I.getZ() - 10.0f * x3; }
bool Vector3d::operator == (const Vector3d& vector){ return (this->x == vector.getX() && this->y == vector.getY() && this->z == vector.getZ()); }
void Vector3d::add( Vector3d v ) { this->x += v.getX(); this->y += v.getY(); this->z += v.getZ(); }