Jump to content
  • Advertisement
Sign in to follow this  
nano511

Player getting hurt problem

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

The problem is he isnt getting hurt XD. I want him to get hurt and be thrown back when a zombie touches him. Instead, the zombie touches him and he sometimes is thrown back, and like %10 chance of getting hurt.

Here is my code to hurt the player

In Player.cpp


void Player::IsHit( int monsterXPos, Direction mosterdir )
{
if( mosterdir == LEFT )
{
if( monsterXPos + 20 == xPos )
{
health -= 1;
if( health < 0 )
health = 0;

xVel -= 40;
yVel -= 5;
}
}
else
{
if( monsterXPos == xPos )
{
health - 1;
if( health < 0 )
health = 0;
xVel += 40;
yVel -= 5;
}
}
}


In game.cpp


void Game::Logic()
{

player->xPos += player->xVel;
...

monster->Move( player->xPos ); player->IsHit( monster->xPos, monster->direction );
player->IsHit( monster->xPos, monster->direction );
...

monster->xPos += monster->xVel

}

Share this post


Link to post
Share on other sites
Advertisement

The problem is he isnt getting hurt XD. I want him to get hurt and be thrown back when a zombie touches him. Instead, the zombie touches him and he sometimes is thrown back, and like %10 chance of getting hurt.

Here is my code to hurt the player

In Player.cpp


void Player::IsHit( int monsterXPos, Direction mosterdir )
{
if( mosterdir == LEFT )
{
if( monsterXPos + 20 == xPos )
{
health -= 1;
if( health < 0 )
health = 0;

xVel -= 40;
yVel -= 5;
}
}
else
{
if( monsterXPos == xPos )
{
health - 1;
if( health < 0 )
health = 0;
xVel += 40;
yVel -= 5;
}
}
}


In game.cpp


void Game::Logic()
{

player->xPos += player->xVel;
...

monster->Move( player->xPos ); player->IsHit( monster->xPos, monster->direction );
player->IsHit( monster->xPos, monster->direction );
...

monster->xPos += monster->xVel

}



I would simplify this process - use the raw distance from the centre point of the zombie and the centre point of the character as a crude test, get this working, and then refine it later to perform per-pixel tests. In the first iteration, your logic looks something like this:

if ( (zombie.position - player.position).length() < player.radius)
{
player->IsHit(zombie);
player->ApplyImpulse();
}

for this to work, you need to be able to do vector math operations - thats what the (zombie->position - player->position).length() stuff is. Are you using any kind of vector math library?

If not, you can instead use pythagorus to work out the distance between the centre points of each creature.

Share this post


Link to post
Share on other sites
Woah Im not even in highschool yet! Here's what i had in mind

If monster xPos and player xPos are the same, lower player health and fling him back depending on the direction he was hit from.

Can you help me with that?

Share this post


Link to post
Share on other sites
I think the issue you are experiencing lies in the fact that you are doing an equal comparison instead of a less or greater than comparison. This forces your code to only perform the velocity and damage effect when the monster is either exactly at the player's position in X (in the monsterdir == RIGHT case) or exactly 20 units beyond the player's position (in the monsterdir == LEFT case).

What exactly is the monsterdirection variable representing? Direction of monster movement I imagine, but a better idea would just be to check if the player and monster are overlapping in any way, and then push the player according to his position minus the monster's position. So if monster is at 50, player is at 75, well 75-50=25, the player should be on the right-side of the monster, a negative result means simply the opposite, and that's how you'd know which direction to push the player.. The damage code itself is the same in either case, it's only the movement code that has to be isolated by monster/player relative position cases.

Share this post


Link to post
Share on other sites
The direction enum was originally used for drawing the sprite in the right direction, but i thought i could use it here to make the player fly in the opposite direction the zombie came at him. I think i get what you mean and ill try to fix it tommorow because its late here.

But one last thing, if monster is at 50 and player is at 75, and then player's new position is at 25, wouldnt that make him on the left of the monster since 25 < 50?

Share this post


Link to post
Share on other sites
75 - 50 = 25 yes, that is the math, but you don't test that value against the players position to decide if the monster is on the left or right of the player. Instead you are getting that value to only see if it is positive or negative.

If you are at 75 and the monster is at 50 that means you are on the right of the monster because the resulted math is positive; however lets do the reverse math
Monster at 75 player at 50.

50 - 75 = -25

The result is negative and therefor the assumption can accurately be made that the monster is to the left of the apparent player.

Negative numbers would be on left and positive numbers on right; however if you check the monster against the player then the reverse can be said, but I would always check the player against the monster. The same goes for Top to Bottom checks, where negative numbers are above(on Top of) and positive numbers are below( or Under).

Also, use >= or <= instead of == in your collision detections. Example if the Player(32 pixels wide and tall) and Zombie at same dimensions and the player is at 50, 50 and the zombie at 75, 75. Assuming you are using the TOP LEFT pixel in the image as point of reference then the players MAX X would be 82 and MAX Y would be 82, the monsters MAX X would be 108 and MAX Y would be 108 as you can see the players MAX X and MAX Y are within the bounds of the Zombie and a collision has occurred, but they are not == to each other; however the collision detection you are doing should have detected the collision at any point the zombie was even 1 pixel in the same space occupied by the player rather than ever having allowed the collision to get that close as no two objects can ever share the exact same space without a collision having occurred.

check this post which I made that would be easier than me just applying that same example to this topic. http://www.gamedev.n...ost__p__4844943

Share this post


Link to post
Share on other sites



else
{
if( monsterXPos == xPos )
{
health - 1;
if( health < 0 )
health = 0;
xVel += 40;
yVel -= 5;
}
}



You are not affecting health.

Share this post


Link to post
Share on other sites

[quote name='nano511' timestamp='1312509811' post='4844827']


else
{
if( monsterXPos == xPos )
{
health - 1;
if( health < 0 )
health = 0;
xVel += 40;
yVel -= 5;
}
}



You are not affecting health.
[/quote]

Probably hand typed it out as they forgot the = sign. If not, that would have supplied a compile time error if I remember correctly.

Share this post


Link to post
Share on other sites

[quote name='jnmacd' timestamp='1312546562' post='4844969']
[quote name='nano511' timestamp='1312509811' post='4844827']


else
{
if( monsterXPos == xPos )
{
health - 1;
if( health < 0 )
health = 0;
xVel += 40;
yVel -= 5;
}
}



You are not affecting health.
[/quote]

Probably hand typed it out as they forgot the = sign. If not, that would have supplied a compile time error if I remember correctly.
[/quote]

I don't believe that that would give you a compile time error. It would subtract 1 from health and dispose of the result. In other words, it just wouldn't do anything.

Saying something like:
if( health < 0)
xVel;


Would compile without problems too, though it's completely useless. I'm thinking that jnmacd is right, and that this is the error that's affecting the OPs code.

Share this post


Link to post
Share on other sites
OK the player Gets hurt now. But Too fast lol. i changed the code to have a surrounding if( !hurt ) loop. In if loops it theres collision i set gurt to true, but where would i set it to false? Would i haev to make a timer?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!