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:
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.gitPPS. 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!