Matrix Scene::getPerspectiveMatrix(float x, float y, float z, Vector direction, Vector up, float fovy, float aspect, float near, float far) { // first we set camera params direction = direction.normalized(); Vector normal = direction.cross(up).normalized(); Vector u = normal.cross(direction).normalized(); direction = Vector(-direction.getX(), -direction.getY(), -direction.getZ()); Vector eye(x,y,z); Matrix camera( normal.getX(), normal.getY(), normal.getZ(), -normal.dot(eye), u.getX(), u.getY(), u.getZ(), -u.dot(eye), direction.getX(), direction.getY(), direction.getZ(), -direction.dot(eye), 0, 0, 0, 1 ); float f = 1.0 / tan(fovy / 2); float d = far - near; Matrix perspective( f/aspect, 0.0, 0, 0, 0.0, f, 0.0, 0.0, 0.0, 0.0, -(near + far)/d, -2*near*far/d, 0.0, 0.0, -1, 0.0 ); Matrix result = perspective.mult(camera); return result; }
Vector Vector::Vectoriel(Vector Vector2){ Vector *V= new Vector(); V->setX( getY() * Vector2.getZ() - getZ() * Vector2.getY() ); V->setY( getZ() * Vector2.getX() - getX() * Vector2.getZ() ); V->setZ( getX() * Vector2.getY() - getY() * Vector2.getX() ); return *V; }
/* \fn calculate the distance from the given Vector \param vec \li the vector to calculate the distance from \return \li the distance of this from vec. */ real distance(const Vector& vec1, const Vector& vec2){ // sqrt (x*x + y*y real xdelta = vec1.getX() - vec2.getX(); real ydelta = vec1.getY() - vec2.getY(); real zdelta = vec1.getZ() - vec2.getZ(); return sqrt(xdelta * xdelta + ydelta * ydelta + zdelta * zdelta); }
/* \fn calculate the crossProduct of this vector and passed vector \param vec \return \li the cross product of the two vectors */ Vector crossProduct(const Vector& vec1, const Vector& vec2){ real newx = vec1.getY()*vec2.getZ() + vec1.getZ()*vec2.getY(); real newy = vec1.getZ()*vec2.getX() + vec1.getX()*vec2.getZ(); real newz = vec1.getX()*vec2.getY() + vec1.getY()*vec2.getX(); return Vector(newx, newy, newz); }
std::vector<float> VoxeledModel::genNormals() { std::vector<float> v; for (int i = 0; i<m_array.getSizeX(); i++) { for (int j = 0; j<m_array.getSizeY(); j++) { for (int k = 0; k<m_array.getSizeZ(); k++) { if (m_array(i,j,k)) { Vector n = voxelNormal(i,j,k).normalized(); v.push_back(n.getX()); v.push_back(n.getY()); v.push_back(n.getZ()); v.push_back(n.getX()); v.push_back(n.getY()); v.push_back(n.getZ()); v.push_back(n.getX()); v.push_back(n.getY()); v.push_back(n.getZ()); v.push_back(n.getX()); v.push_back(n.getY()); v.push_back(n.getZ()); v.push_back(n.getX()); v.push_back(n.getY()); v.push_back(n.getZ()); v.push_back(n.getX()); v.push_back(n.getY()); v.push_back(n.getZ()); v.push_back(n.getX()); v.push_back(n.getY()); v.push_back(n.getZ()); v.push_back(n.getX()); v.push_back(n.getY()); v.push_back(n.getZ()); } } } } return v; }
/* \fn calculate the angle between this vector and the given vector along xy axes \param vec \return \li the angle */ real angle(const Vector& vec1, const Vector& vec2){ // acos (vec.x - x / distance) real distance = CapEngine::distance(vec1, vec2); real xDelta = vec1.getX() - vec2.getX(); real radAngle = acos(distance / xDelta); return RADTODEG(radAngle); }
int main() { srand(time(nullptr)); // Creating heap. ObjectHeap heap; // Creating value map. HumanoidValueMap map(&heap, 10, 10, 1); Vector coords = map.getBestPlace(); assert(coords.getX() == -1); assert(coords.getY() == -1); // Creating objects. Test t1(Vector(4.5, 4.5), 1, 0.5); heap.push(&t1); Test t2(Vector(3.1, 7.2), 1, 0.5); heap.push(&t2); // Reevaluating map. map.reevaluate(); coords = map.getBestPlace(); std::cout << map.print() << std::endl << coords.getX() << " " << coords.getY() << std::endl; return 0; }
/** * @brief GameEngine::KFactorOfNormal * @param vector one of the vectors which determines the direction of the vector in asked plane * @param normal normal Vector of the Plane * @return A factor which when multiplied with normal and then added with vector gives one of the Vector in the Plane */ float GameEngine::KFactorOfNormal(Vector vector, Vector normal){ float nx=normal.getX(); float ny=normal.getY(); float nz=normal.getZ(); return -(vector.getX()*nx + vector.getY()*ny + vector.getZ()*nz)/(nx*nx + nz*nz + ny*ny ); }
bool operator!=(const Vector& v1, const Vector& v2) { if (v1.getX() != v2.getX() || v1.getY() != v1.getY() || v1.getZ() != v2.getZ()) return true; else return false; }
bool operator==(const Vector& v1, const Vector& v2) { if (v1.getX() == v2.getX() && v1.getY() == v1.getY() && v1.getZ() == v2.getZ()) return true; else return false; }
Vector Vector::cross(const Vector& v) { return Vector( (comp[1]*v.getZ())-(comp[2]*v.getY()), (comp[2]*v.getX())-(comp[0]*v.getZ()), (comp[0]*v.getY())-(comp[1]*v.getX()) ); }
//vectorial product Vector Vector::operator*=( Vector vector) { return Vector( ( this->getZ() * vector.getY() - this->getY()*vector.getZ()), ( this->getX() * vector.getZ() - this->getZ()*vector.getX()), ( this->getY() * vector.getX() - this->getX()*vector.getY())); }
// get distance to line using vector product double Vector::getDistanceToLine(const Vector& line_start, const Vector& line_end) const { Vector a = *this - line_start; Vector b = line_end - line_start; return fabs(a.getX() * b.getY() - a.getY() * b.getX()) / line_start.getDistance(line_end); }
Vector<T> Vector<T>::cross(const Vector<T> &a, const Vector<T> &b) { Vector<T> product = Vector<T>(); product.setX(a.getY() * b.getZ() - b.getY() * a.getZ()); product.setY(a.getX() * b.getZ() - b.getX() * a.getZ()); product.setZ(a.getX() * b.getY() - b.getX() * a.getY()); return product; }
Vector Vector::cross(Vector vec1, Vector vec2) { Vector tmp; tmp.setX(vec1.getZ() * vec2.getY() - vec1.getY() * vec2.getZ()); tmp.setY(vec1.getX() * vec2.getZ() - vec1.getZ() * vec2.getX()); tmp.setZ(vec1.getY() * vec2.getX() - vec1.getX() * vec2.getY()); double l = 1; tmp.setL(l); return tmp; }
Tensor Vector::getRotationMatrix(double angle) { double cos = std::cos(angle); double sin = std::sin(angle); Vector a = getNormalized(); return Tensor( a.getX()*a.getX()*(1.0-cos)+cos, a.getY()*a.getX()*(1.0-cos)-a.getZ()*sin, a.getZ()*a.getX()*(1.0-cos)+a.getY()*sin, a.getX()*a.getY()*(1.0-cos)+a.getZ()*sin, a.getY()*a.getY()*(1.0-cos)+cos, a.getZ()*a.getY()*(1.0-cos)-a.getX()*sin, a.getX()*a.getZ()*(1.0-cos)-a.getY()*sin, a.getY()*a.getZ()*(1.0-cos)+a.getX()*sin, a.getZ()*a.getZ()*(1.0-cos)+cos ); }
Vector Vector::getRotation(Vector a, double angle) { double cos = std::cos(angle); double sin = std::sin(angle); a.normalize(); Tensor transformationMatrix = Tensor( a.getX()*a.getX()*(1.0-cos)+cos, a.getY()*a.getX()*(1.0-cos)-a.getZ()*sin, a.getZ()*a.getX()*(1.0-cos)+a.getY()*sin, a.getX()*a.getY()*(1.0-cos)+a.getZ()*sin, a.getY()*a.getY()*(1.0-cos)+cos, a.getZ()*a.getY()*(1.0-cos)-a.getX()*sin, a.getX()*a.getZ()*(1.0-cos)-a.getY()*sin, a.getY()*a.getZ()*(1.0-cos)+a.getX()*sin, a.getZ()*a.getZ()*(1.0-cos)+cos ); return (*this)*transformationMatrix; }
float Sphere::findIntersect(Ray ray) { Vector rayOrigin = ray.getOrigin(); float rayOriginX = rayOrigin.getX(); float rayOriginY = rayOrigin.getY(); float rayOriginZ = rayOrigin.getZ(); Vector rayDirection = ray.getDirection(); float rayDirectionX = rayDirection.getX(); float rayDirectionY = rayDirection.getY(); float rayDirectionZ = rayDirection.getZ(); Vector sphereCenter = center; float sphereCenterX = sphereCenter.getX(); float sphereCenterY = sphereCenter.getY(); float sphereCenterZ = sphereCenter.getZ(); //float a = 1; //normalized float b = (2 * (rayOriginX - sphereCenterX) * rayDirectionX) + (2 * (rayOriginY - sphereCenterY) * rayDirectionY) + (2 * (rayOriginZ - sphereCenterZ) * rayDirectionZ); float c = pow(rayOriginX - sphereCenterX, 2) + pow(rayOriginY - sphereCenterY, 2) + pow(rayOriginZ - sphereCenterZ, 2) - (radius*radius); float discriminant = b*b - 4*c; //if the ray intersections the sphere if (discriminant > 0) { //the first root float root1 = ((-1 * b - sqrt(discriminant))/2) - 0.000001; //if first root is the smallest positive root if (root1 > 0) { return root1; } //else the second root is the smallest positive root else { float root2 = ((sqrt(discriminant) - b)/2) - 0.000001; return root2; } } //else the ray missed the sphere else { return -1; } }
void LaserActor::writeT3D(std::ofstream& output, int indentLevel, NameFactory *nameFactory, Vector gridPosition, Vector gridTranslation) { if (mGrid != nullptr) { mLocation = (gridPosition + gridTranslation) * T3DExporter::DEMI_CUBE_SIZE * 2.0; mLocation = mLocation - Vector(0, 0, 3. * T3DExporter::DEMI_CUBE_SIZE / 4.); long predX = mGrid->get(gridPosition.getX() - 1, gridPosition.getY(), gridPosition.getZ()); long predY = mGrid->get(gridPosition.getX(), gridPosition.getY() - 1, gridPosition.getZ()); long predZ = mGrid->get(gridPosition.getX(), gridPosition.getY(), gridPosition.getZ() - 1); //long nextX = mGrid->get(gridPosition.getX() + 1, gridPosition.getY(), gridPosition.getZ()); //long nextY = mGrid->get(gridPosition.getX(), gridPosition.getY() + 1, gridPosition.getZ()); //long nextZ = mGrid->get(gridPosition.getX(), gridPosition.getY(), gridPosition.getZ() + 1); if (mGrid->get(gridPosition) == Grid::LASER_X_STICKED_CELL) { if (!Grid::isPhysicalCell(predX)) { mRotation = T3DExporter::FACING_X_VECTOR; } else { mRotation = T3DExporter::FACING_NEG_X_VECTOR; } } else if (mGrid->get(gridPosition) == Grid::LASER_Y_STICKED_CELL) { if (!Grid::isPhysicalCell(predY)) { mRotation = T3DExporter::FACING_Y_VECTOR; } else { mRotation = T3DExporter::FACING_NEG_Y_VECTOR; } } else { if (!Grid::isPhysicalCell(predZ)) { mRotation = T3DExporter::FACING_Z_VECTOR; } else { mRotation = T3DExporter::FACING_NEG_Z_VECTOR; } } double offset = T3DExporter::DEMI_CUBE_SIZE - 5; if (mRotation == T3DExporter::FACING_X_VECTOR) { mLocation = mLocation - Vector(offset, 0, 0); } else if (mRotation == T3DExporter::FACING_NEG_X_VECTOR) { mLocation = mLocation + Vector(offset, 0, 0); } else if (mRotation == T3DExporter::FACING_Y_VECTOR) { mLocation = mLocation - Vector(0, offset, 0); } else if (mRotation == T3DExporter::FACING_NEG_Y_VECTOR) { mLocation = mLocation + Vector(0, offset, 0); } else if (mRotation == T3DExporter::FACING_Z_VECTOR) { mLocation = mLocation - Vector(0, 0, offset); } else if (mRotation == T3DExporter::FACING_NEG_Z_VECTOR) { mLocation = mLocation + Vector(0, 0, offset); } } output << getT3D(indentLevel, nameFactory); }
void Particle::addPosition(Vector position) { this->graphicsItem->setPos( this->graphicsItem->x() + position.getX(), this->graphicsItem->y() + position.getY() ); }
void GoaliePen::decide() { Vector Me; vector <int> opps = pWorldData->mr_found_opponents(); Point ballPos = pWorldData->basedCenter(pWorldData->ball()); Point closestOppToBalll; if(opps.size() != 0) pWorldData->basedCenter(pWorldData->opponent(pWorldData->getNearstOppToPoint(ballPos))); Point me; me.x = Me.getX(); me.y = Me.getY(); if(pBasicPlayer->can_kick()) doClear(); else { if(me.getDistance(closestOppToBalll) > ballPos.getDistance(closestOppToBalll) && opps.size() != 0) doBlock(); else doGo(); } }
bool voxelAppartientCylindre(Point origine, Vector vecteur,double rayon, Voxel v){ Point limite(origine.getX()+vecteur.getX(), origine.getY()+vecteur.getY(),origine.getZ()+vecteur.getZ()); int distancePointProjete; int distanceOrigineProjete; for( int i = 0; i<8; i++ ){ Point projete(v.getSommet(i).projectOnLine(origine,limite)); distancePointProjete = sqrt( pow(v.getSommet(i).getX()-projete.getX(),2) + pow(v.getSommet(i).getY()-projete.getY(),2) + pow(v.getSommet(i).getZ()-projete.getZ(),2) ); distanceOrigineProjete = sqrt( pow(origine.getX()-projete.getX(),2) + pow(origine.getY()-projete.getY(),2) + pow(origine.getZ()-projete.getZ(),2) ); if( distancePointProjete > rayon || projete.getY() < origine.getY() || projete.getY() > limite.getY() ){ return false; } } return true; }
void FastWorldDrawer::configureCamera() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); Vector cameraPosition = manager->GetCameraModel()->GetPosition(); glTranslated(-cameraPosition.getX(), -cameraPosition.getY(), 0); }
/* Diese Methode Subtrahiert das Objekt und den vector sum */ Vector Vector::operator-=(Vector sum) { x -= sum.getX(); y -= sum.getY(); return *this; }
/* Diese Methode weist dem Objekt die Koordinaten von sum zu */ Vector Vector::operator=(Vector sum) { x = sum.getX(); y = sum.getY(); return *this; }
Vector normaleSommet(std::vector<Triangle> triangles,Point p){ std::vector<Triangle> listeTriangle; for(Triangle t : triangles){ if(t.getSommetA().compare(p) || t.getSommetC().compare(p) || t.getSommetC().compare(p) ){ listeTriangle.push_back(t); } } Vector vecteur(0,0,0); for(Triangle t : listeTriangle){ Vector vecteurTmp = normaleFace(t); vecteur.setX(vecteur.getX() + vecteurTmp.getX()); vecteur.setY(vecteur.getY() + vecteurTmp.getY()); vecteur.setZ(vecteur.getZ() + vecteurTmp.getZ()); } vecteur.setX(vecteur.getX() / listeTriangle.size()); vecteur.setY(vecteur.getY() / listeTriangle.size()); vecteur.setZ(vecteur.getZ() / listeTriangle.size()); vecteur.normalize(); return vecteur; }
Vector Vector::operator=(Vector vector){ this->x = vector.getX(); this->y = vector.getY(); this->z = vector.getZ(); return *this; }
void MyPolygon::transformPolygon(){ for(int i=0; i<1; i++){ //Vector v = AsSimilarAsPossible(vertices[i]); Vector v = AsRigidAsPossible(vertices[i]); vertices[i].move(v.getX(), v.getY()); } }
bool AABB::intersects(Ray ray, double &dist) { double distance[6] = {-1, -1, -1, -1, -1, -1}; distance[0] = (start.getX() - ray.origin.getX()) / ray.direction.getX(); distance[3] = (end.getX() - ray.origin.getX()) / ray.direction.getX(); distance[1] = (start.getY() - ray.origin.getY()) / ray.direction.getY(); distance[4] = (end.getY() - ray.origin.getY()) / ray.direction.getY(); distance[2] = (start.getZ() - ray.origin.getZ()) / ray.direction.getZ(); distance[5] = (end.getZ() - ray.origin.getZ()) / ray.direction.getZ(); int closestFace = -1; dist = 0; for (int i = 0; i < 6; i++) { Vector rayToBox = ray.origin + ray.direction * distance[i]; if ( (rayToBox.getX() > start.getX() - EPSILON) && (rayToBox.getX() < end.getX() + EPSILON) && (rayToBox.getY() > start.getY() - EPSILON) && (rayToBox.getY() < end.getY() + EPSILON) && (rayToBox.getZ() > start.getZ() - EPSILON) && (rayToBox.getZ() < end.getZ() + EPSILON) ) { if (((closestFace == -1) || distance[i] < dist) && distance[i] > EPSILON) { closestFace = i; dist = distance[i]; } } } return (closestFace != -1); }
void Camera::setUp(const Vector &up) { this->up.setX(up.getX()); this->up.setY(up.getY()); this->up.setZ(up.getZ()); this->up.normalize(); }