Help wanted with colliders/collision SFML C++

Hi,

I'm pretty new to programming and am working on an entry assignement for the game development school I hope to attend this year. I need to develop a working 2d game in C++ and I'm using SFML.

At the moment I'm stuck at working with collisions and the tilemap. So far I have a movable player object, a tilemap that loads correctly and a collision map that seems to be loading correctly. When a collision is detected, I move the player object a few pixels in the opposite direction of where the player object was moving. I'm using the width of the player object and the tile on the map that he is colliding with, and use subtraction and the like to calculate the amount of pixels to move. My goal is to stop the player object when it runs into another (solid) object. When entering the collider I want to move it out of the collider, before drawing the frame. The result is unfortunatly somewhat unexpected....

When the player object hits another object (in my code the player starts by falling down onto a solid object, being the ground) it keeps 'jumping' up and down for a smal amount of pixels, just above the collider. I can keep moving right and left but the player object keeps jumping up and down...

I think there may be a problem with my code I use for this, but I fail to understand what I could be doing wrong:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  	if (p1.right < tileLeft || p1.left > tileRight || p1.top > tileBottom || p1.bottom < tileTop)
		{
		}
	else
		{
			if (p1.bottom > tileTop && p1.bottom > tileBottom)
				{
					p1.rect.move(0, (-(p1.bottom - tileTop)));
					p1.update();
				}
			else if (p1.top < tileBottom && p1.top < tileTop)
				{
					p1.rect.move(0, (-(tileBottom - p1.top)));
					p1.update();
				}
			else if (p1.right > tileLeft && p1.right > tileRight)
				{
					p1.rect.move(-(p1.right - tileLeft), 0);
					p1.update();
				}
			else if (p1.left < tileRight && p1.left < tileLeft)
				{
					p1.rect.move((tileRight - p1.left), 0);
					p1.update();
				}
		}


Can someone point me in the right direction? Being a solution or a suggestion on how to debug this.. I'm really stuck here. Any help is greatly appreciated!


Kind regards, Jonah

PS. Here's a link to the entire code I have so far: https://gitlab.com/jonah_rutten/BOOST.git
PPS. Please forgive me if I'm not addresseing the issue correctly or if I'm not using the correct lingo.. I'm pretty new to all this and still have much to learn!
It is unnecessary to compare both sides of the tile in each if statement. If p1.bottom > tileBottom is true you know for sure that p1.bottom > tileTop must be true so no need to check it.

If my thinking is correct p1.bottom > tileBottom means that the player is hitting the tile from below. That means you should move the player down (tileBottom - p1.top) pixels instead, doesn't it? I'm not sure but I think so.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (p1.bottom > tileBottom)
{
	p1.rect.move(0, (tileBottom - p1.top));
	p1.update();
}
else if (p1.top < tileTop)
{
	p1.rect.move(0, (tileTop - p1.bottom));
	p1.update();
}
else if (p1.right > tileRight)
{
	p1.rect.move((tileRight - p1.left), 0);
	p1.update();
}
else if (p1.left < tileLeft)
{
	p1.rect.move((tileLeft - p1.right), 0);
	p1.update();
}
Topic archived. No new replies allowed.