Sign in to follow this  

Object Map Collision

This topic is 1599 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

Hello, I have been working on a collision system with an object map and I cannot get it to work. I go through it over and over, and I draw it out and It looks like it should work. Here is what I got:

 

Get the current object if it is in the screen:


for(auto iter = m_ObjMap.begin(); iter != m_ObjMap.end(); ++iter){

	//if the tile is within the screen render
	if((iter->getHitbox().sfPosition.x + iter->getHitbox().sfSize.x > width) &&
		(iter->getHitbox().sfPosition.x < width + 400) &&
		(iter->getHitbox().sfPosition.y + iter->getHitbox().sfSize.y > height) &&
		(iter->getHitbox().sfPosition.y < height + 300)){

			player.checkCollision(*iter);
	}
}

Collision code:

void PlayerEntity::checkCollision(Object obj){

	//object attributes
	float posX = obj.getHitbox().sfPosition.x;
	float posY = obj.getHitbox().sfPosition.y;
	float sizeX = obj.getHitbox().sfSize.x;
	float sizeY = obj.getHitbox().sfSize.y;
	
	//top of player to bottom of object
	if(m_sfPosition.y >= posY + sizeY) m_bCollisionUp = true;
	else m_bCollisionUp = false;

	//bottom of player to top of object
	if(m_sfPosition.y + m_sfSize.y <= posY) m_bCollisionDown = true;
	else m_bCollisionDown = false;

	//left of player to right of object
	if(m_sfPosition.x <= posX + sizeX) m_bCollisionLeft = true;
	else m_bCollisionLeft = false;

	//right of player to left of object
	if(m_sfPosition.x + m_sfSize.x >= posX) m_bCollisionRight = true;
	else m_bCollisionRight = false;
}

I got this and then If there is collision then it will print to the console at which side. I am not moving, and I am in the air, and it says I get down and left collision. In the constructor, all the collision Boolean variables are set to false. Thanks for any help.

Edited by Crusable

Share this post


Link to post
Share on other sites

Is the down direction same as the positive y direction? If so, then

if(m_sfPosition.y >= posY + sizeY) m_bCollisionUp = true;
if(m_sfPosition.y + m_sfSize.y <= posY) m_bCollisionDown = true;

should be

if(m_sfPosition.y <= posY + sizeY) m_bCollisionUp = true;
if(m_sfPosition.y + m_sfSize.y >= posY) m_bCollisionDown = true;

 

Also, try using exclusive comparisons instead of inclusive ones (> and < instead of >= and <=) and see if that helps.

Share this post


Link to post
Share on other sites

This topic is 1599 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this