Sign in to follow this  
nano511

Player getting hurt problem

Recommended Posts

nano511    103
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
[code]

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;
}
}
}
[/code]

In game.cpp
[code]

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

}
[/code]

Share this post


Link to post
Share on other sites
speciesUnknown    527
[quote name='nano511' timestamp='1312509811' post='4844827']
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
[code]

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;
}
}
}
[/code]

In game.cpp
[code]

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

}
[/code]
[/quote]

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
nano511    103
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
deftware    1778
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
nano511    103
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
BrianJensen    343
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. [url="http://www.gamedev.net/topic/607767-smooth-2d-collision-detection/page__view__findpost__p__4844943"]http://www.gamedev.n...ost__p__4844943[/url]

Share this post


Link to post
Share on other sites
jnmacd    197
[quote name='nano511' timestamp='1312509811' post='4844827']
[code]

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

You are not affecting health.

Share this post


Link to post
Share on other sites
BrianJensen    343
[quote name='jnmacd' timestamp='1312546562' post='4844969']
[quote name='nano511' timestamp='1312509811' post='4844827']
[code]

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

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
sjaakiejj    130
[quote name='0Circle0' timestamp='1312550689' post='4844997']
[quote name='jnmacd' timestamp='1312546562' post='4844969']
[quote name='nano511' timestamp='1312509811' post='4844827']
[code]

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

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
nano511    103
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
way2lazy2care    790
[quote name='nano511' timestamp='1312554128' post='4845020']
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?
[/quote]

You are painting over rust with this solution tbh. It's better to establish what the real problem is and then fix it the right way.

I'm guessing the problem is that you aren't pushing the player far enough away so it just keeps colliding. Fix that problem first. Ideally I would say do what speciesunknown said.

Share this post


Link to post
Share on other sites
Wooh    1088
In many older games when you get hit you are pushed away and you start to blink for a short time. As long as you are blinking you will not get hurt. Have you considered this approach?

Share this post


Link to post
Share on other sites
nano511    103
Yes thats what im asking about Wooh

E: To whoever said painting over rust: I have it so that if there is collision xVel -= 70, but he still doesnt fly backwards.

Share this post


Link to post
Share on other sites
way2lazy2care    790
[quote name='nano511' timestamp='1312555187' post='4845029']
Yes thats what im asking about Wooh

E: To whoever said painting over rust: I have it so that if there is collision xVel -= 70, but he still doesnt fly backwards.
[/quote]

use your debugger. You are probably setting his velocity again somewhere else before he flies backwards.

Share this post


Link to post
Share on other sites
nano511    103
Okay he is flung back but i still need to do the blinky thing because if he's hurt then tries to walk through the monster weird things happen. So when he's hurt he should be able to walk through monsters for a while

Share this post


Link to post
Share on other sites
sjaakiejj    130
[quote name='nano511' timestamp='1312558401' post='4845053']
Okay he is flung back but i still need to do the blinky thing because if he's hurt then tries to walk through the monster weird things happen. So when he's hurt he should be able to walk through monsters for a while
[/quote]

First off, what type of "Weird Things" happen? If weird things happen they generally indicate a bug, and ignoring it by covering it up with different functionality is going to get back to you.

Regarding the blinky thing, you have to give the character State, and introduce a "HurtState", in which the character ignores collisions with monsters.

Share this post


Link to post
Share on other sites
nano511    103
Could you go into more detail about this character state? And by weird things i mean if you try to run through the monster you keep getting flung back and eventually caught in the corner and you die.

Share this post


Link to post
Share on other sites
skuzzbag    120
State at its simplest would be a boolean var named isHurt and set to false.

Then after the collision make isHurt=true for however long you need.

Then as mentioned while isHurt=true no collision detection takes place.

Share this post


Link to post
Share on other sites
nano511    103
Thats what i want to do. I have that all set up, but im confused on "for however long you need". Say i wanted to stay in hurt state for 2 seconds. How would i do that?

Share this post


Link to post
Share on other sites
nano511    103
Ahh nvm. I guess ill learn how to use one of the ones you linked. iNudge doesnt let you save music, and although there is almost no way to sound bad in iNudge, you can make anything other then happy music. Im trying to make a more dark piece.

Share this post


Link to post
Share on other sites
TTT_Dutch    240
[quote name='nano511' timestamp='1312513148' post='4844838']
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?
[/quote]

Thats what I thought when I had to learn about vectors, but its just a line lol

Share this post


Link to post
Share on other sites
nano511    103
[quote name='TTT_Dutch' timestamp='1312576660' post='4845206']
[quote name='nano511' timestamp='1312513148' post='4844838']
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?
[/quote]

Thats what I thought when I had to learn about vectors, but its just a line lol
[/quote]

Yeah but i prefer using code that i understand lol

btw player can still leave the screen :(

Share this post


Link to post
Share on other sites
sjaakiejj    130
[quote name='nano511' timestamp='1312563467' post='4845084']
Could you go into more detail about this character state? And by weird things i mean if you try to run through the monster you keep getting flung back and eventually caught in the corner and you die.
[/quote]

The implementation of Character State really depends on how you handle your code. Generally the thing you'd do is to implement some sort of a state manager, and a class called something in the lines of "CharacterState". CharacterState would be an interface class, of which the specific states would inherit the methods such as "Enter", "Exit" and "Update", again, depending on your implementation. These methods in turn would handle input, collisions and events. So when your character is in the "HealthyState", collision detection and damage is the way you've currently implemented. When your character is in the state "HurtState", it would not handle any enemy collisions and the draw method would have your character blink instead. The update method of "HurtState" could then simply tell the manager to change the state again after a given amount of time, e.g. you have a variable "ElapsedTime" which is set to 0 on enter, and just starts counting until it has reached 2 seconds or however long you want it to be. Then it calls "StateManager::ChangeState()" to change the state back to "HealthyState".


You can add as many states as you like, and it's generally the way that games show the condition in which the character is in (E.g. low HP means your character has wounds and ripped clothes, high HP means he looks clean without any damage). It's really the same as normal State Management, but then on a micro level.


Regarding vectors; Learn them. They're quite easy to understand, and it's going to help you a lot in game development.

Share this post


Link to post
Share on other sites

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