Jump to content
  • Advertisement
jeffkiwi

2d collisions cannot "slide" on texture when vertical collision occurs

This topic is 382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So in each frame I record the current coordinates - if there's a collision it resets the x and y. This mean if you held "right" while holding "up" you could not slide "up" along the tile because of the way the resetting works. I changed the code so it updates the X and Y separately, but now I still have an issue where my character cannot slide along a texture if it is collides with the top or bottom of the texture (side collisions work okay and the character can "slide" up or down the side of a texture).

Edit: I added 3 tags to this post but only c++ appears :(

int keyInputX(SDL_Rect playerarea, Input input) {
	input.update();
	int playerSpeed;
	playerSpeed = 4;
	if (input.keyDown(SDL_SCANCODE_RIGHT)) playerarea.x += playerSpeed;
	if (input.keyDown(SDL_SCANCODE_LEFT)) playerarea.x -= playerSpeed;

	return playerarea.x;
}

int keyInputY(SDL_Rect playerarea, Input input) {
	input.update();
	int playerSpeed;
	playerSpeed = 4;
	if (input.keyDown(SDL_SCANCODE_UP)) playerarea.y -= playerSpeed; // key input
	if (input.keyDown(SDL_SCANCODE_DOWN)) playerarea.y += playerSpeed;

	return playerarea.y;
}

void CheckPlayerCollision(SDL_Rect & player, std::list<Tile> & TileList, SDL_Rect & oldLocation) { 
	for (Tile& mytile : TileList) { // this is the collision code for the rock tiles
		if (player.x + 40 > mytile.location.x && player.x - 100 < mytile.location.x) {
			if (player.y + 100 > mytile.location.y && player.y - 76 < mytile.location.y) { // nest this for speed
				player.x = oldLocation.x;
				player.y = oldLocation.y;
			}
		}
	}
}

main loop below:

			originalLocation.x = area.x;
			originalLocation.y = area.y;

			area.x = keyInputX(area, input); // update player location X
			CheckPlayerCollision(area, TileList, originalLocation);
			area.y = keyInputY(area, input); // update player location  Y
			CheckPlayerCollision(area, TileList, originalLocation);

 

Edited by jeffkiwi

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!