Jump to content
  • Advertisement
Sign in to follow this  
Elit3d

sfml distance between player and enemy

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

void Enemy::EnemyDetectPlayer(sf::Vector2f playerPos, sf::Vector2f enemyPos){
	float distance = (abs(sqrt(((playerPos.x - enemyPos.x) * (playerPos.x - enemyPos.x)) + ((playerPos.y - enemyPos.y) * (playerPos.y - enemyPos.y)))));

	if (distance < 200 && m_gunIsOut == true){
		//do something here
	}
}

So I currently have the distance between enemy and player, but the problem is, wherever the enemy is eg (500,50) this distance < 200 wont work correctly because the enemies x+y isn't 0, it would be 500,50. 

 

So my question is, how would I get the distance all around the enemy correctly so when the player is in distance, something will happen?

Share this post


Link to post
Share on other sites
Advertisement


wherever the enemy is eg (500,50) this distance < 200 wont work correctly because the enemies x+y isn't 0, it would be 500,50. 

The distance formula will work regardless of where the entities are.

Neither of the entities need to be at (0, 0).

 

Are you actually experiencing a problem with this code? If so, for which specific values (both player & enemy)?

 

Also, you can remove the abs() call completely. It's never ever going to do anything.

Later on, you can also remove the sqrt() call. You can use squared distances instead, and it will still work (but I would suggest you make sure your current code works correctly before doing this kind of change).

Share this post


Link to post
Share on other sites
Just skimmed through the code, but it looks like you're risking to take the square root of a negative number there. I suppose that's why you got that abs() call in there, bur you need to movre it inside the sqrt() call.
I would recommend something like this:
float xDist = abs(playerPos.x - enemyPos.x);
float yDist = abs(playerPos.y - enemyPos.y);
float distance = sqrt(xDist*xDist + yDist*yDist);

But if playerPos and enemyPos is vectors, you probably have a dist() function or something similar? Or (playerPos - enemyPos).length()?
Idk, I haven't used SMFL. The top solution should work just fine in any case.

EDIT:
I had forgotten basic math, so much of what I wrote is wrong. See below.

Share this post


Link to post
Share on other sites

Just skimmed through the code, but it looks like you're risking to take the square root of a negative number there.

That is false. Taking the square of any real number is a non-negative value.
 
float distx = player.x - enemt.y; // distx can be negative.
float disty = player.y - enemy.y; // disty can be negative.

float distx2 = distx * distx; // distx2 is ALWAYS non-negative (+2 * +2 gives +4, 0 * 0 gives 0, -3 * -3 gives +9)
float disty2 = disty * disty; // disty2 is ALWAYS non-negative

float dxy2 = distx2 + disty2; // dxy2 is ALWAYS non-negative
float dxy = sqrt(dxy2);

Share this post


Link to post
Share on other sites
It is normal math that the square of a real or integer number gives a non-negative value smile.png

The square root of negative numbers (but that's not happening in the problem of distance calculations!) is normally either a floating point exception or a Nan, I think you can set that behavior (but never tried it).
With a complex math library I'd indeed hope you get an imaginary number, but that is not 'standard' math (which assumes the floating point number domain).

Share this post


Link to post
Share on other sites

 

Just skimmed through the code, but it looks like you're risking to take the square root of a negative number there.

That is false. Taking the square of any real number is a non-negative value.
 
float distx = player.x - enemt.y; // distx can be negative.
float disty = player.y - enemy.y; // disty can be negative.

float distx2 = distx * distx; // distx2 is ALWAYS non-negative (+2 * +2 gives +4, 0 * 0 gives 0, -3 * -3 gives +9)
float disty2 = disty * disty; // disty2 is ALWAYS non-negative

float dxy2 = distx2 + disty2; // dxy2 is ALWAYS non-negative
float dxy = sqrt(dxy2);

 

This is really nicely written thank you, but my same issue still occurs. if i set my enemy to (200,50) and I go over the top of him, my distance will be 200, if I set him to (500,500) im getting in the 700's so how am I going to get the distance around the enemy if the values are totally different depending on where they are?

 

eg. If I am on the left of the enemy and I am < 200 away from enemy { do something }

If I am on the right of the enemy and I am < 200 away from enemy { do something }

If I am on the top of the enemy and I am < 200 away from enemy { do something }

If I am on the bottom of the enemy and I am < 200 away from enemy { do something }

Edited by Elit3d

Share this post


Link to post
Share on other sites

if i set my enemy to (200,50) and I go over the top of him, my distance will be 200

Then your bug isn't in the code you posted.

EIther your values going into the function are wrong (check in a debugger), or you're doing something wrong somewhere else.

 

It sounds like maybe you're always comparing to something that's at (0, 0). If the values change when you move the enemy, verify that your player position is correct (again, use a debugger).

Edited by Lactose!

Share this post


Link to post
Share on other sites

 


if i set my enemy to (200,50) and I go over the top of him, my distance will be 200

Then your bug isn't in the code you posted.

EIther your values going into the function are wrong (check in a debugger), or you're doing something wrong somewhere else.

 

It sounds like maybe you're always comparing to something that's at (0, 0). If the values change when you move the enemy, verify that your player position is correct (again, use a debugger).

 

 

I don't really know where to start looking. I am calling the function in my main update function like so:

 

enemy.EnemyDetectPlayer(player.playerPosition, enemy.enemyPosition);

 

and the position of each are vector2f

Share this post


Link to post
Share on other sites

 

 


if i set my enemy to (200,50) and I go over the top of him, my distance will be 200

Then your bug isn't in the code you posted.

EIther your values going into the function are wrong (check in a debugger), or you're doing something wrong somewhere else.

 

It sounds like maybe you're always comparing to something that's at (0, 0). If the values change when you move the enemy, verify that your player position is correct (again, use a debugger).

 

 

I don't really know where to start looking. I am calling the function in my main update function like so:

 

enemy.EnemyDetectPlayer(player.playerPosition, enemy.enemyPosition);

 

and the position of each are vector2f

 

That's why I said to use a debugger.

Set a breakpoint inside the function, and step through line by line, inspecting each variable to see what it is.

 

Debugging is a fundamental skill -- the sooner you learn it, the better.

 

EDIT: A helpful link. Clicky.

Edited by Lactose!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!