Ejemplo n.º 1
0
void Player::updateY(const std::chrono::milliseconds elapsed_time,
                     const Map& map,
                     ParticleTools& particle_tools)
{
    // Update velocity
    const units::Acceleration gravity = is_jump_active_ && velocity_.y < 0
        ? kJumpGravity
        : kGravity;
    velocity_.y = std::min(velocity_.y + gravity * elapsed_time.count(),
                kMaxSpeedY);
    // Calculate delta
    const units::Game delta = velocity_.y * elapsed_time.count();
    if (delta > 0.0) {
        // Check collision in the direction of delta
        CollisionInfo info = getWallCollisionInfo(map, bottomCollision(delta));
        // React to collision
        if (info.collided) {
            pos_.y = units::tileToGame(info.row) - kCollisionYBottom;
            velocity_.y = 0.0;
            is_on_ground_ = true;
        } else {
            pos_.y += delta;
            is_on_ground_ = false;
        }
        // Check collision in the direction opposite to delta
        info = getWallCollisionInfo(map, topCollision(0));
        if (info.collided) {
            pos_.y = units::tileToGame(info.row) + kCollisionYHeight;
            createHeadBumpParticle(particle_tools);
        }
    } else {
        // Check collision in the direction of delta
        CollisionInfo info = getWallCollisionInfo(map, topCollision(delta));
        // React to collision
        if (info.collided) {
            pos_.y = units::tileToGame(info.row) + kCollisionYHeight;
            createHeadBumpParticle(particle_tools);
            velocity_.y = 0.0;
        } else {
            pos_.y += delta;
            is_on_ground_ = false;
        }
        // Check collision in the direction opposite to delta
        info = getWallCollisionInfo(map, bottomCollision(0));
        if (info.collided) {
            pos_.y = units::tileToGame(info.row) - kCollisionYBottom;
            is_on_ground_ = true;
        }
    }
}
Ejemplo n.º 2
0
ball_t moveBall(ball_t ball, paddle_t paddle) {
	// if the ball hits the left or right edge, reverse the x velocity
	if(leftCollision(ball, paddle) || rightCollision(ball)) {
		ball.velocity.x *= -1;
	}

	// if the ball hits the top or bottom edge, reverse the y velocity
	if(topCollision(ball) || bottomCollision(ball)) {
		ball.velocity.y *= -1;
	}

	outOfBounds(ball);		// check if the ball goes behind the paddle

	// makes the ball move
	ball.position.x += ball.velocity.x;
	ball.position.y += ball.velocity.y;

	return ball;
}
Ejemplo n.º 3
0
void Megaman::stop(double wallX, double wallY, double wallWidth, double wallHeight)
{
	// Case: Below surface
	if ((spriteData.x + spriteData.width > wallX) && (spriteData.x < wallX + wallWidth) && spriteData.y >= wallY + wallHeight - 10)
	{
		bottomCollision(wallY, wallHeight);
	}
	// Case: Above surface
	else if (((spriteData.x + spriteData.width > wallX && spriteData.x < wallX + wallWidth) && spriteData.y + spriteData.height <= wallY + 20) && velocity.y >= 0)
	{
		topCollision(wallY);
	}
	// Case: Left of surface
	else if (spriteData.x + spriteData.width >= wallX && spriteData.x + spriteData.width < wallX + wallWidth && spriteData.y < wallY + wallHeight)
	{
		leftCollision(wallX);
	}
	// Case: Right of surface
	else if (spriteData.x < wallX + wallWidth && spriteData.x + spriteData.width > wallX  && spriteData.y < wallY + wallHeight)
	{
		rightCollision(wallX, wallWidth);
	}
}
Ejemplo n.º 4
0
//=============================================================================
// stop
// Collision detection between Mega Man and solid surfaces
//=============================================================================
void Megaman::stop(std::vector<VECTOR2> collisionVector, std::vector<RECT> tileCoordinates)
{
	if (collisionVector.size() == 1)
	{
		stop(tileCoordinates[0].left, tileCoordinates[0].top, tileCoordinates[0].right - tileCoordinates[0].left, tileCoordinates[0].bottom - tileCoordinates[0].top);
	}
	else
	{
		while (collisionVector.empty() == false)
		{
			VECTOR2 tempCV = collisionVector[0];
			collisionVector.erase(collisionVector.begin());

			RECT tempTC = tileCoordinates[0];
			tileCoordinates.erase(tileCoordinates.begin());

			bool blocksInLine = false;
			bool onTop = false;
			bool onBottom = false;
			bool onLeft = false;
			bool onRight = false;

			for (int i = 0; i < collisionVector.size(); i++)
			{
 				if (!collisionVector.empty() && tempTC.top == tileCoordinates[i].top)
				{
					collisionVector.erase(collisionVector.begin() + i);
					tileCoordinates.erase(tileCoordinates.begin() + i);
					i--;
					blocksInLine = true;
				}
			}
			// If blocks lined up on the same y-coordinate (top y)
			if (blocksInLine) 
			{
				//top/bottom collision
				if (tempCV.y > 0)
				{
					topCollision(tempTC.top);
				}
				else
				{
					bottomCollision(tempTC.top, tempTC.bottom - tempTC.top);
				}
			}
			else
			{
				for (int i = 0; i < collisionVector.size(); i++)
				{
					if (!collisionVector.empty() && tempTC.left == tileCoordinates[i].left)
					{
						collisionVector.erase(collisionVector.begin() + i);
						tileCoordinates.erase(tileCoordinates.begin() + i);
						i--;
						blocksInLine = true;
					}
				}

				// If blocks lined up on the same x-coordinate (left x)
				if (blocksInLine)
				{
					//left/right collision
					if (tempCV.x > 0)
					{
						leftCollision(tempTC.left);
					}
					else
					{
						rightCollision(tempTC.left, tempTC.right - tempTC.left);
					}
				}
				else
				{
					stop(tempTC.left, tempTC.top, tempTC.right - tempTC.left, tempTC.bottom - tempTC.top);
				}
			}
			if (collisionVector.size() == 1 || collisionVector.empty())
			{
				if (!collisionVector.empty())
				{
					stop(tileCoordinates[0].left, tileCoordinates[0].top, tileCoordinates[0].right - tileCoordinates[0].left, tileCoordinates[0].bottom - tileCoordinates[0].top);
					collisionVector.erase(collisionVector.begin());
					tileCoordinates.erase(tileCoordinates.begin());
				}
			}
		}
	}
}