• Advertisement
Sign in to follow this  

Syntax to say "Withing x units from?"

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

Is there an easy way, in C++, to say "if player A is within x units from player B"? Or do you have to just do all the math? The reason I ask is because when I move my players, and they get to their destination, sometimes its not the exact same place(off a .5 unit or something), so they bounce back and forth trying to get to it. But if I say, destinationx is within 1 unit of playerx, then its ok..... Jeff

Share this post


Link to post
Share on other sites
Advertisement
There is no such predefined syntax in C++.

The real power of C++ is to let you, the programmer, create your own "syntax" (actually, objects and functions) for operations that you need to do on a regular basis.

So, the syntax would be:

if (Distance(A,B) < x) {
// It's within x units
} else {
// It's not
}


Just write a function Distance which returns the distance between two players.

Share this post


Link to post
Share on other sites
You'll have to do the math. (And then hopefully stick it in a function of your own.) C++ has no notion of players or distance between them. After all, what kind of distance would it use? Euclidean? Manhattan? Minkowski?

Share this post


Link to post
Share on other sites

bool at_destination( Player p1, Player p2 ) {
return distance( p1.position, p2.position ) &lt; x;
}

Share this post


Link to post
Share on other sites
Thats the formula to calculate the distance (P.Theorem )
:


pseudo code:

(X1 - X2 ) + (Y1 - Y2 )
//dont use pow() ...it will slow down your game ;/
return sqrt (xResult*xResult + yResult * yResult)


Share this post


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

  • Advertisement