Public Group

# Syntax to say "Withing x units from?"

This topic is 4114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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 on other sites

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

##### 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)

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 101
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009662
• ### Who's Online (See full list)

There are no registered users currently online

×