double Vector2::AngleBetween(Vector2 v) {
    if (this->Magnitude() == 0 || v.Magnitude() == 0) {
        return 0;
    }
    else {
        double dotProd = this->Dot(v);
        dotProd /= this->Magnitude() * v.Magnitude();
        return acos(dotProd);
    }
}
Beispiel #2
0
bool Intersect(const Circle& c, const Line& l)
{
	Vector2 startToCenter = c.center - l.from;
	Vector2 startToEnd = l.to - l.from;
	float len = startToEnd.Magnitude();
	Vector2 dir = startToEnd / len;

	// Find the closest point to the line segment
	float projection = Dot(startToCenter, dir);
	Vector2 closestPoint;
	if (projection > len)
	{
		closestPoint = l.to;
	}
	else if (projection < 0.0f)
	{
		closestPoint = l.from;
	}
	else
	{
		closestPoint = l.from + (dir * projection);
	}

	// Check if the closest point is within the circle
	Vector2 closestToCenter = c.center - closestPoint;
	if (closestToCenter.MagnitudeSqr() > c.radius * c.radius)
	{
		return false;
	}
	return true;
}
Beispiel #3
0
float Vector2::AngleBetween(Vector2 v) const {
	if (this->Magnitude() == 0 || v.Magnitude() == 0) {
		return 0;
	}

	Vector2 left = this->Normalize();
	Vector2 right = v.Normalize();

	if (left == right) {
		return 0;
	}

	float dot = left.Dot(right);

	// Floating points check
	if (dot > 1.0f) {
		dot = 1.0f;
	}
	else if (dot < -1.0f) {
		dot = -1.0f;
	}

	float rot = acos(dot);

	// http://stackoverflow.com/questions/11022446/direction-of-shortest-rotation-between-two-vectors
	// Use cross vector3 to determine direction
	Vector3 cross = Vector3(left.x, left.y).Cross(Vector3(right.x, right.y));
	if (cross.z > 0) {
		return -rot;
	}
	else {
		return rot;
	}
}
void Vector2::RotateAround(Vector2 origin, double rotation) {
    Vector2 fromMid = *this - origin;
    Vector2 unitVec(1, 0);
    double angleFrom0 = fromMid.AngleBetween(unitVec);
    if (fromMid.y < 0) {
        angleFrom0 *= -1;
    }
    angleFrom0 += rotation;
    Vector2 endMove(cos(angleFrom0), sin(angleFrom0));
    endMove *= fromMid.Magnitude();
    endMove = origin + endMove;

    *this = endMove;
}
Beispiel #5
0
Vector2 Vector2::RotateAround(Vector2 origin, float rotation) const {
	if (rotation == 0) {
		return *this;
	}

	Vector2 fromMid = *this - origin;
	Vector2 unitVec(1, 0);
	float angleFrom0 = fromMid.AngleBetween(unitVec);
	angleFrom0 -= rotation;
	Vector2 endMove(cos(angleFrom0), sin(angleFrom0));
	endMove *= fromMid.Magnitude();
	endMove = origin + endMove;

	return endMove;
}
void GameManager::MouseMotion(int x, int y)
{
    y = glutGet(GLUT_WINDOW_HEIGHT) - y;

    Vector2 dirVector = Vector2(x,y) - Vector2(GameConfig::getInstance().ScreenWidth/2, GameConfig::getInstance().ScreenHeight/2);

    float dirVectorMag = sqrtf((dirVector.x * dirVector.x) + (dirVector.y * dirVector.y));

    mNormalizedDir = dirVector/dirVectorMag;

    Vector2 shipUp(0,1);

    float dotProduct = (dirVector.x * shipUp.x) + (dirVector.y * shipUp.y);

    float cosAngle = dotProduct / (shipUp.Magnitude() * dirVector.Magnitude()) ;

    float angle = acos(cosAngle) * 180.f / PI;

    mPlayerShip->SetDirectionAngle( x > GameConfig::getInstance().ScreenWidth/2.f ? -angle : angle);
}
Beispiel #7
0
/* AngleBetween */
float Vector2::AngleBetween(Vector2& vec) {
	return acos(DotProduct(vec)/(Magnitude() * vec.Magnitude()));
}
Beispiel #8
0
 public: static Vector2 Normalize (Vector2 &A) {
     return A *= float
     (float (1) /
     A.Magnitude());
 }
Beispiel #9
0
		float Vector2::CompProj(const Vector2& argVec1, const Vector2& argVec2)
		{
			return Vector2::Dot(argVec1, argVec2) / argVec1.Magnitude();			
		};
Beispiel #10
0
		Vector2 Vector2::Normalize(const Vector2& argSource)
		{
			return argSource / argSource.Magnitude();
		};	
Beispiel #11
0
 double Magnitude( Vector2 const& i_vector )
 {
     return i_vector.Magnitude();
 }