GLdouble dotProd(const Vec &one, const Vec &two){ GLdouble x = (one.getX() * two.getX()); GLdouble y = (one.getY() * two.getY()); GLdouble z = (one.getZ() * two.getZ()); return (x + y + z); }
/* Multiplication vector and matrix. (vec * *this) * ARGUMENTS: * - vector to multiply from right * Vec &v; * RETURNS: * - result vector * Vec; */ Vec Matrix::MultiplyVectorFromLeft( Vec &v ) { Vec res = Vec(v.getX() * M[0][0] + v.getY() * M[0][1] + v.getZ() * M[0][2] + M[0][3], v.getX() * M[1][0] + v.getY() * M[1][1] + v.getZ() * M[1][2] + M[1][3], v.getX() * M[2][0] + v.getY() * M[2][2] + v.getZ() * M[2][2] + M[2][3]); return res; } /* End of 'operator*' function */
/* Multiplication vector and matrix. (*this * v) * ARGUMENTS: * - vector to multiply from right * Vec v; * RETURNS: * - result vector * Vec; */ Vec Matrix::operator*( Vec &v ) { Vec res = Vec(v.getX() * M[0][0] + v.getY() * M[1][0] + v.getZ() * M[2][0] + M[3][0], v.getX() * M[0][1] + v.getY() * M[1][1] + v.getZ() * M[2][1] + M[3][1], v.getX() * M[0][2] + v.getY() * M[1][2] + v.getZ() * M[2][2] + M[3][2]); return res; } /* End of 'operator*' function */
Vec crossProd(const Vec &one, const Vec &two){ GLdouble cp[3]; cp[0] = one.getY() * two.getZ() - two.getY() * one.getZ(); cp[1] = one.getZ() * two.getX() - two.getZ() * one.getX(); cp[2] = one.getX() * two.getY() - two.getX() * one.getY(); Vec result(cp[0], cp[1], cp[2]); return result; }
void KdTree::searchInternal(KdNode* node,const BBox& bb,int d,List<VecItem*>& items){ if( node ){ Vec pn = node->item->getPosition(); if( (d==0 && bb.getMinX()<pn.getX()) || bb.getMinY()<pn.getY() ){ searchInternal(node->left,bb,(d+1)%m_k,items); } if( bb.contains(pn) ){ items.append( node->item ); } if( (d==0 && pn.getX()<=bb.getMaxX()) || pn.getY()<=bb.getMaxY() ){ searchInternal(node->right,bb,(d+1)%m_k,items); } } }
void KdTree::searchInternal(KdNode* node,const Vec& p,double range,int d,List<VecItem*>& items){ if( node ){ Vec pn = node->item->getPosition(); if( (d==0 && (p.getX()-range)<pn.getX()) || (p.getY()-range)<pn.getY() ){ searchInternal(node->left,p,range,(d+1)%m_k,items); } if( p.distanceToLessThan(pn,range) ){ items.append( node->item ); } if( (d==0 && pn.getX()<=(p.getX()+range)) || pn.getY()<=(p.getY()+range) ){ searchInternal(node->right,p,range,(d+1)%m_k,items); } } }
//This function is stupid, I know. //This is NOT a comparison of vector lengths! //It needs to look this way for 'maps' to work... bool Vec::operator<(const Vec vecA) const { Vec tmp = vecA; Vec tmp2 = *this; if (tmp2.getX() < tmp.getX()) { return true; } else if((tmp2.getX() == tmp.getX()) && (tmp2.getY() < tmp.getY())){ return true; } else if((tmp2.getX() == tmp.getX()) && (tmp2.getY() == tmp.getY()) && (tmp2.getZ() < tmp.getZ())){ return true; } else return false; }
Vec Vec::operator=(Vec vecA){ x = vecA.getX(); y = vecA.getY(); z = vecA.getZ(); return *this; }
// Reference: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=30 GLint testIntPlane(const Plane &pl, const Vec &pos, const Vec &dir, GLdouble &lamda, const GLdouble offset){ if ( (fabs(pl.position.getX() - pos.getX()) > offset) || (fabs(pl.position.getZ() - pos.getZ()) > offset)) { return 0; } /* Dot Product between the plane normal and ray direction */ GLdouble theDotProd = dotProd(dir, pl.normal); GLdouble lam; // Determine If Ray Parallel To Plane if ((theDotProd < ZERO) && (theDotProd > -ZERO)) return 0; /* Find the distance to the collision point */ lam = dotProd(pl.position-pos, pl.normal); lam /= theDotProd; if (lam < -ZERO) // Test If Collision Behind Start return 0; lamda=lam; return 1; }
bool Vec::operator==(const Vec vecA) const{ Vec tmp = vecA; if (x==tmp.getX() && y==tmp.getY() && z==tmp.getZ()) { return true; } else return false; }
bool Vec::operator!=(const Vec vecA) const{ Vec tmp = vecA; if (x!=tmp.getX() || y!=tmp.getY() || z!=tmp.getZ()) { return true; } else return false; }
Lighting::Lighting(Vec pos, Vec dir, char color, GLfloat spot, GLfloat intense, int lightNum, GLfloat colour[]) { this->white[0] = 1.0; this->white[1] = 1.0; this->white[2] = 1.0; this->white[3] = 1.0; this->red[0] = 1.0; this->red[1] = 0.0; this->red[2] = 0.0; this->red[3] = 1.0; this->green[0] = 0.0; this->green[1] = 1.0; this->green[2] = 0.0; this->green[3] = 1.0; this->blue[0] = 0.0; this->blue[1] = 0.0; this->blue[2] = 1.0; this->blue[3] = 1.0; this->other[0] = colour[0]; this->other[1] = colour[1]; this->other[2] = colour[2]; this->other[3] = colour[3]; this->position[0] = pos.getX(); this->position[1] = pos.getY(); this->position[2] = pos.getZ(); this->position[3] = 1.0; this->direction[0] = dir.getX(); this->direction[1] = dir.getY(); this->direction[2] = dir.getZ(); this->fSpotLight = spot; this->spotIntensity = intense; this->init(color, lightNum); }
/* Create rotation Matrix. * ARGUMENTS: * - rotation angle in degrees * float angle; * - rotation vector * Vec radVec; * RETURNS: * - rotation matrix * Matrix ; */ Matrix CreateRotation( float angle, Vec &radVec ) { Matrix rot; /**/float si = sin(D2R(angle)), co = cos(D2R(angle)), len, radX = radVec.getX(), radY = radVec.getY(), radZ = radVec.getZ(); len = !radVec; if (len == 0) len = 1; radX /= len; radY /= len; radZ /= len; rot.SetElement(0, 0, co + radX * radX * (1 - co)); rot.SetElement(0, 1, radX * radY * (1 - co) - radZ * si); rot.SetElement(0, 2, radX * radZ * (1 - co) + radY * si); rot.SetElement(1, 0, radX * radY * (1 - co) + radZ * si); rot.SetElement(1, 1, co + radY * radY * (1 - co)); rot.SetElement(1, 2, radZ * radY * (1 - co) - radX * si); rot.SetElement(2, 0, radX * radZ * (1 - co) - radY * si); rot.SetElement(2, 1, radZ * radY * (1 - co) + radX * si); rot.SetElement(2, 2, co + radZ * radZ * (1 - co));/* float si = sin(D2R(angle / 2)), co = cos(D2R(angle / 2)), len, // ÏÎËÎÂÈÍÍÛÉ ÓÃÎË!!! radX = radVec.getX(), radY = radVec.getY(), radZ = radVec.getZ(); len = !radVec; if (len == 0) len = 1; radX *= si / len; radY *= si / len; radZ *= si / len; rot.SetElement(0, 0, 1 - 2 * (radY * radY + radZ * radZ)); rot.SetElement(0, 1, 2 * radX * radY - 2 * co * radZ); rot.SetElement(0, 2, 2 * co * radY + 2 * radX * radZ); rot.SetElement(1, 0, 2 * radX * radY + 2 * co * radZ); rot.SetElement(1, 1, 1 - 2 * (radX * radX + radZ * radZ)); rot.SetElement(1, 2, 2 * radY * radZ - 2 * co * radX); rot.SetElement(2, 0, 2 * radX * radZ - 2 * co * radY); rot.SetElement(2, 1, 2 * co * radX + 2 * radY * radZ); rot.SetElement(2, 2, 1 - 2 * (radX * radX + radY * radY));/**/ return rot; } /* End of 'CreateRotation' function */
void Trogdor::randomDirection(int randomInt, Vec &enemyPosition) { if (this->facing == NORTH || this->facing == SOUTH) { if (randomInt % 10000 == 0) { if (this->position.getX() < enemyPosition.getX()) this->facing = EAST; else if (this->position.getX() > enemyPosition.getX()) this->facing = WEST; } } else //other direction { if (randomInt % 10000 == 0) { if (this->position.getZ() < enemyPosition.getZ()) this->facing = SOUTH; else if (this->position.getZ() > enemyPosition.getZ()) this->facing = NORTH; } } }
GLdouble testDist(const Vec &p1, const Vec &p2){ GLdouble x = p1.getX() - p2.getX(); GLdouble z = p1.getZ() - p2.getZ(); return sqrt(pow(x,2) + pow(z,2)); }
void Trogdor::ChooseDirection(GLint nTile, GLint sTile, GLint eTile, GLint wTile, Vec &enemyPosition) { if (this->facing == NORTH || this->facing == SOUTH) { if (wTile == 0 && this->position.getX() > enemyPosition.getX()) //westTile is not a wall and enemy is more west than trogdor { this->facing = WEST; //go west } else if (eTile == 0 && this->position.getX() < enemyPosition.getX()) //eastTile not a wall and enemy is more east than trogdor { this->facing = EAST; // go east } else if (nTile == 0 && this->position.getZ() > enemyPosition.getZ()) //northTile is not a wall and enemy is above trogdor { this->facing = NORTH; // go north } else if (sTile == 0 && this->position.getZ() < enemyPosition.getZ()) //southTile is not a wall and enemy is lower than trogdor { this->facing = SOUTH; //go south } else if (wTile == 0 && this->position.getX() > enemyPosition.getX()) //westTile is not a wall and enemy is more west than trogdor { this->facing = WEST; //go west } else if (eTile == 0) //no other conditions work try an available direction... { this->facing = EAST; } else if (nTile == 0) { this->facing = NORTH; } else if (sTile == 0) { this->facing = SOUTH; } } else //other two directions { if (nTile == 0 && this->position.getZ() > enemyPosition.getZ()) //northTile is not a wall and enemy is above trogdor { this->facing = NORTH; // go north } else if (sTile == 0 && this->position.getZ() < enemyPosition.getZ()) //southTile is not a wall and enemy is lower than trogdor { this->facing = SOUTH; //go south } else if (wTile == 0 && this->position.getX() > enemyPosition.getX()) //westTile is not a wall and enemy is more west than trogdor { this->facing = WEST; //go west } else if (eTile == 0 && this->position.getX() < enemyPosition.getX()) //eastTile not a wall and enemy is more east than trogdor { this->facing = EAST; // go east } else if (nTile == 0) //no other conditions work try an available direction... { this->facing = NORTH; } else if (sTile == 0) { this->facing = SOUTH; } else if (wTile == 0) { this->facing = WEST; } else if (eTile == 0) { this->facing = EAST; } } }
void Vec::operator= ( Vec other ) { this->x = other.getX(); this->y = other.getY(); this->z = other.getZ(); }
void Vec::set( const Vec &other ) { this->x = other.getX(); this->y = other.getY(); this->z = other.getZ(); }
void Vec::operator-= ( Vec other ) { this->x -= other.getX(); this->y -= other.getY(); this->z -= other.getZ(); }
Vec Vec::operator- ( const Vec &other ) const { Vec result(this->x - other.getX(), this->y - other.getY(), this->z - other.getZ()); return result; }
void Vec::operator+= ( Vec other ) { this->x += other.getX(); this->y += other.getY(); this->z += other.getZ(); }
Vec Vec::operator+ ( const Vec &other ) const { Vec result(this->x + other.getX(), this->y + other.getY(), this->z + other.getZ()); return result; }
Vec Vec::operator+(Vec vecA){ Vec newVec(vecA.getX()+this->getX(),vecA.getY()+this->getY(),vecA.getZ()+this->getZ()); return newVec; }
Vec Vec::operator-(Vec vecA){ Vec newVec(this->getX()-vecA.getX(),this->getY()-vecA.getY(),this->getZ()-vecA.getZ()); return newVec; }
GLdouble Vec::dot( const Vec &other ) const { return (getX() * other.getX() + getY() * other.getY() + getZ() * other.getZ()); }