Sign in to follow this  

colliding with objects help

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

hi what i am trying to do is is get my player to stop if above an enemy and pressing down and below a sprite prssing up. So if player is above an enemy as is pressing down and is within range then he must stop and if the player is belo an enemy and is pressing up and is within range play mus stop, i did this but not sure it is correct.

void CollisionManager::playerWithEnemies(Player *player, Enemy *enemy)
{
	//for(int i = 0; i < NUM_BBOX; i++)
	//{
		// body bounding box
		if(collide.PPSpriteCollision(&player->getBbox(0)->getSprite(), &enemy->getBbox(0)->getSprite()) 
			&& player->isAlive() && enemy->isAlive())
		{
			// fix this part needs amendment
			//if(diffZ(player->getPosZ(), enemy->getPosZ()))
			//{
				// player conditions
				// player above enemy sprite
				if(player->getPosZ() < (enemy->getPosZ() + 80))
				{
					//player going down
					if(player->getDirZ() > 1)player->move(0, -player->getSpeed());
				}
				// player below enemy sprite
				if(player->getPosZ() > (enemy->getPosZ() - 80))
				{
					//player going up
					if(player->getDirZ() < -1)player->move(0, player->getSpee());
				}
				
				// enemy conditions
				if(enemy->getPosX() < (player->getPosX() - X_HIT_RANGE))
				{
					if(enemy->getDirX() > 1)
					{
						enemy->move(-enemy->getSpeed(), 0);
					}
				}
			//}

			if(diffZ(player->getPosZ(), enemy->getPosZ()))
			{
				// player conditions
				// behind p2 sprite
				if(player->getPosX() > (enemy->getPosX() + X_HIT_RANGE))
				{
					if(player->getDirX() < -1)
					{
						player->move(+player->getSpeed(), 0);
						enemy->move(-enemy->getSpeed(), 0);
					}
				}
				// infront of p2 sprite
				else if(player->getPosX() < (enemy->getPosX() - X_HIT_RANGE))
				{
					if(player->getDirX() > 1)
					{
						player->move(-player->getSpeed(), 0);
					}
				}
			}
		} 
	//}	
}


it seems that if i press down and right or left the player goes straight through the enemy

Share this post


Link to post
Share on other sites
wow, that looks like you totally went overkill on something simple. Maybe try something like this...?


void MovePlayer(Player *player)
{
// do player input and move player
if(KEY_DOWN("d"))
player->x++;


for(int i=0;i<MAX_ENEMIES;i++)
{
//check right side
if(player->x+16 > enemies[i].x-16 && player->x+16 < enemies[i].x+16)
player->x--; //move back
}
}





theres another way to do collsion detection...its kinda cleaner imo. You get the actual distance from the enemy using the distance formula.


void MovePlayer(Player *player)
{
for(int i=0; i<MAX_ENEMIES; i++)
{
int dx = enemies[i].x - player->x;
int dy = enemies[i].y - player->y;

int distance = sqrt(dx*dx + dy*dy);

if( distance < 32 )
return;
}


// there isnt a monster near...its ok to move
if(KEY_DOWN("d"))
player->x++;
}


Share this post


Link to post
Share on other sites
NegativeGeForce: your distance formula only works for circles. Using axis aligned bounding box collision code is probably faster since it doesn't require square root calculations, and provides early-out conditions.

So, to demonstrate:


struct Box
{
int top, bottom, left, right;
}

bool Collision(Box& a, Box& b)
{
// if box a is completely to the left, or to the right, of box b, then they don't collide
if(a.right <= b.left || a.left >= b.right)
return false;
// if box a is completely above, or beneath box b, then they don't collide
else if(a.bottom >= b.top || a.top <= b.bottom)
return false;
// otherwise, they do intersect
else
return true;
}




That's as simple as it gets. For movement, you may want to check how deep they intersect, to correctly move one of the boxes back to a non-collision state. The code for that shouldn't be too hard to figure out.

As for making a character shake, if it's purely for the visual effect, why not add a shakex and shakey variabele to it's display location, and modify that one when you want him to shake? Pretty straightforward stuff, right? ;)

Share this post


Link to post
Share on other sites
I was just demoing 2 different ways to do collisions because im not sure what kind of game this is. From my experience the only way you can get a "slide" from moving along a collision object is to check each side at a time and align the player to the surface.

Just keep in mind you dont need crazy class design to figure out that 2 things crossed paths, lol. KISS, u know what that means :D.

Share this post


Link to post
Share on other sites

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