• Advertisement

[Vector Distance]Trying to get the distance between two vectors and get the enemy to shoot when i'm in range but I can't get it through. Help please

Recommended Posts

	float CalculateDistance(D3DXVECTOR3 enemyPos, D3DXVECTOR3 playerPos)
	{
		float length;
		float distanceX;
		float distanceZ;

		distanceX = enemyPos.x - playerPos.x;
		distanceZ = enemyPos.z - playerPos.z;

		length = sqrt((distanceX*distanceX) + (distanceZ * distanceZ));

		return length;
	}

//And then in my Update


	D3DXVECTOR3 distance = D3DXVECTOR3(10.0f, 0.0f, 10.0f);

	if (distance.x || distance.z <= CalculateDistance(D3DXVECTOR3(position.x, 0.0f, position.z), D3DXVECTOR3(CGame::GetPlayer()->GetPos().x, 0.0f, CGame::GetPlayer()->GetPos().z)))
	{
		int Random = rand() % 50;

		if (Random == 0)
		{
			D3DXVECTOR3 towardsPlayer = CGame::GetPlayer()->GetPos() - position;
			Bullet::CreateBullet((D3DXVECTOR3(position.x, position.y - 5.0f, position.z)),
				D3DXVECTOR3(10.0f, 10.0f, 0.0f),
				25.0f,
				towardsPlayer,
				30,
				BULLET_TYPE_ENEMY);
		}
	}

I've tried doing this. The enemy continues to shoot even if I'm farther than distance.x and distance.z. Can someone tell me what's wrong?

Edited by Smarkus

Share this post


Link to post
Share on other sites
Advertisement

When something goes wrong, please include what goes wrong. Save us all some time by not having to guess.

 

That said, your if check looks very suspect. if (distance.x || ...) -- this will most likely always return true. In this part of the if, you are only checking whether distance.x evaluates to true (which I think is the same as distance.x != 0.0f)

 

I think the if check can be changed to something like this (you might have to cast things or convert to the correct type...):

if (CalculateDistance(position, CGame::GetPlayer()->GetPos() <= someValue)

 

Share this post


Link to post
Share on other sites
14 minutes ago, Lactose said:

When something goes wrong, please include what goes wrong. Save us all some time by not having to guess.

 

That said, your if check looks very suspect. if (distance.x || ...) -- this will most likely always return true. In this part of the if, you are only checking whether distance.x evaluates to true (which I think is the same as distance.x != 0.0f)

 

I think the if check can be changed to something like this (you might have to cast things or convert to the correct type...):


if (CalculateDistance(position, CGame::GetPlayer()->GetPos() <= someValue)

 

Thanks for the reply. What's wrong with my code is that the enemy continues to shoot even if i'm farther away than distance.x and distance.z which is 10.0f each.

So the someValue that you suggested is in my case is the range in which the enemy shoots which in my case is  distance.x and distance.z. 

Edited by Smarkus

Share this post


Link to post
Share on other sites

The likely problem is that you misunderstood what the operator || does. It takes booleans on both sides and returns true if either one is true (it also has short-circuiting semantics, but that's not relevant here).

So `distance.x' is converted to a boolean, which means your condition will trigger whenever distance.x is not zero. Probably not what you want.

 

Edited by alvaro

Share this post


Link to post
Share on other sites
1 minute ago, alvaro said:

The likely problem is that you misunderstood what the operator || does. It takes booleans on both sides and returns true if either one is true (it also has short-circuiting semantics, but that's not relevant here).

So `distance.x' is interpreted as a boolean, which means your condition will trigger whenever distance.x is not zero. Probably not what you want.

 

Got it. So what I was thinking when I made this vector was that this would be the distance which is the range in which the enemy would shoot and if I'm out of the distance range then the enemy won't shoot at the player. How else do you suggest me to implement that?

Share this post


Link to post
Share on other sites

if( dist.x  <= YourConditionHere || dist.y <= YourConditionHere)

You are doing the equivalent of:

if(dist.x)

which, if dist.x is != zero...which it always is...it will always enter the conditional.

 

You might've been able to track this down via debugging as well, or logging.  Those are both good tricks to have in your skillset. 

 

 

Though bigger picture, distance is a single float and not a vector, so I'm not sure why you're checking .x or .z and not just distance.

Edited by ferrous

Share this post


Link to post
Share on other sites
1 hour ago, Smarkus said:

Got it. So what I was thinking when I made this vector was that this would be the distance which is the range in which the enemy would shoot and if I'm out of the distance range then the enemy won't shoot at the player. How else do you suggest me to implement that?

You really should be able to figure this one out yourself. Measure the distance (a single number, as ferrous points out), then check against your threshold.

Share this post


Link to post
Share on other sites

if your max shoot vector is 10,0,10, calculate that as a scalar length of distance value and compare to the distance between the player and enemy.


 

float maxDistance = sqrt((distance.x*distance.x) + (distance.z*distance.z));
if (maxDistance >= CalculateDistance(D3DXVECTOR3(position.x, 0.0f, position.z), D3DXVECTOR3(CGame::GetPlayer()->GetPos().x, 0.0f, CGame::GetPlayer()->GetPos().z)))

{

}

 

you can also optimize the whole thing if you don't use sqrt() at all, in this instance it wont' affect the outcome.

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


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By _RoboCat_
      Hi,
      Can anyone point me into good direction how to resolve this?
      I have flat mesh made from many quads (size 1x1 each) each split into 2 triangles. (made procedural)
      What i want to achieve is : "merge" small quads into bigger ones (show on picture 01), English is not my mother language and my search got no result... maybe i just form question wrong.
      i have array[][] where i store "map" information, for now i'm looking for blobs of same value in it -> and then for each position i create 1 quad. and on end create mesh from all.
      is there any good algorithm for creating mesh between random points on same plane? less triangles better. Or "de-tesselate" this to bigger/less triangles/quads?
      Also i would like to find "edges" and create "faces" between edge points (picture 02 shows what i want to achieve).
      No need for whole code, just if someone can point me in good direction would be nice.
      Thanks


    • By Karol Plewa
      Hi, 
       
      I am working on a project where I'm trying to use Forward Plus Rendering on point lights. I have a simple reflective scene with many point lights moving around it. I am using effects file (.fx) to keep my shaders in one place. I am having a problem with Compute Shader code. I cannot get it to work properly and calculate the tiles and lighting properly. 
       
      Is there anyone that is wishing to help me set up my compute shader?
      Thank you in advance for any replies and interest!
    • By PhillipHamlyn
      Hi
      I have a procedurally generated tiled landscape, and want to apply 'regional' information to the tiles at runtime; so Forests, Roads - pretty much anything that could be defined as a 'region'. Up until now I've done this by creating a mesh defining the 'region' on the CPU and interrogating that mesh during the landscape tile generation; I then add regional information to the landscape tile via a series of Vertex boolean properties. For each landscape tile vertex I do a ray-mesh intersect into the 'region' mesh and get some value from that mesh.

      For example my landscape vertex could be;
      struct Vtx { Vector3 Position; bool IsForest; bool IsRoad; bool IsRiver; } I would then have a region mesh defining a forest, another defining rivers etc. When generating my landscape veretexes I do an intersect check on the various 'region' meshes to see what kind of landscape that vertex falls within.

      My ray-mesh intersect code isn't particularly fast, and there may be many 'region' meshes to interrogate, and I want to see if I can move this work onto the GPU, so that when I create a set of tile vertexes I can call a compute/other shader and pass the region mesh to it, and interrogate that mesh inside the shader. The output would be a buffer where all the landscape vertex boolean values have been filled in.

      The way I see this being done is to pass in two RWStucturedBuffer to a compute shader, one containing the landscape vertexes, and the other containing some definition of the region mesh, (possibly the region might consist of two buffers containing a set of positions and indexes). The compute shader would do a ray-mesh intersect check on each landscape vertex and would set the boolean flags on a corresponding output buffer.

      In theory this is a parallelisable operation (no one landscape vertex relies on another for its values) but I've not seen any examples of a ray-mesh intersect being done in a compute shader; so I'm wondering if my approach is wrong, and the reason I've not seen any examples, is because no-one does it that way. If anyone can comment on;
      Is this a really bad idea ? If no-one does it that way, does everyone use a Texture to define this kind of 'region' information ? If so - given I've only got a small number of possible types of region, what Texture Format would be appropriate, as 32bits seems really wasteful. Is there a common other approach to adding information to a basic height-mapped tile system that would perform well for runtime generated tiles ? Thanks
      Phillip
    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
  • Advertisement