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);
}
}
}
}
//}
}
colliding with objects help
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.
it seems that if i press down and right or left the player goes straight through the enemy
wow, that looks like you totally went overkill on something simple. Maybe try something like this...?
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){ // 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.x-16 && player->x+16 < enemies.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.x - player->x; int dy = enemies.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++;}
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:
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? ;)
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? ;)
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement