Пример #1
0
// Calculate the projection of a polygon on an axis
// and returns it as a [min, max] interval
void ProjectTriangle2D(Vector2f axis, Vector2f *triangle, float &min, float &max) {
	float dotProduct = axis.Dot(triangle[0]);
	min = dotProduct;
	max = dotProduct;
	for (int i = 0; i < 3; i++)
	{
		float d = triangle[i].Dot(axis);
		if (d < min)
			min = d;
		else if(d > max)
			max = d;
	}
}
Пример #2
0
void Player::Update(float elapsed)
{
    //Add friction into the acceleration.

    //If the player isn't accelerating, slow down his velocity completely.
    if (Acceleration.x == 0.0f && Acceleration.y == 0.0f)
    {
        if (Velocity.x != 0.0f || Velocity.y != 0.0f)
        {
            Acceleration = -Velocity * LevelConstants::Instance.PlayerFriction;
        }
    }
    else
    {
        //Split the acceleration and velocity into "forward" and "side" components.
        Vector2f forward = LookDir.XY().Normalized(),
                 side = Vector2f(forward.y, forward.x),
                 forwardA = forward * forward.Dot(Acceleration),
                 forwardV = forward * forward.Dot(Velocity),
                 sideA = side * side.Dot(Acceleration),
                 sideV = side * side.Dot(Velocity);

        //Add friction to any velocity components that go against the acceleration.
        if (forwardA.Dot(forwardV) < 0.0f && (abs(forwardV.x) > 0.00001f || abs(forwardV.y) > 0.00001f))
        {
            Acceleration -= forwardV.Normalized() * LevelConstants::Instance.PlayerFriction;
        }
        if (sideA.Dot(sideV) < 0.0f && (abs(sideV.x) > 0.00001f || abs(sideV.y) > 0.00001f))
        {
            Acceleration -= sideV.Normalized() * LevelConstants::Instance.PlayerFriction;
        }
    }


    //Update velocity and acceleration.
    Velocity += Acceleration * elapsed;
    Acceleration = Vector2f();

    //Constrain the velocity.
    float speedSqr = Velocity.LengthSquared();
    if (speedSqr > LevelConstants::Instance.PlayerMaxSpeed * LevelConstants::Instance.PlayerMaxSpeed)
    {
        Velocity = (Velocity / sqrtf(speedSqr)) * LevelConstants::Instance.PlayerMaxSpeed;
    }

    //Update position.
    TryMove(elapsed);


    //Handle weapons.
    if (Fire)
    {
        if (!firedLastFrame)
        {
            firedLastFrame = true;
            GetCurrentWeapon()->StartFire();
        }
        Fire = false;
    }
    else if (firedLastFrame)
    {
        GetCurrentWeapon()->StopFire();
        firedLastFrame = false;
    }
    GetCurrentWeapon()->Update(elapsed);
}