# Calculate distance between 2 translations/Models

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

## Recommended Posts

This is my first post here, lets see how this commnity's support is :). I am working on a simple AI, the characters will walk and turn if they are stuck because of a wall. I therefore want to do a periodicly check like very 0.5 seconds to compare the current translation/location with a location X seconds ago. I want to rotate the character if the difference is too small. I therefore use this c++ code:
D3DXVECTOR3 AI_stuckCheck; //Scheduled stuck check
int AInextMove; //Next rotation/pause/etc

static unsigned long stuckUpdate = timeGetTime();
if( stuckUpdate + 500 < timeGetTime()){
if(AI_stuckCheck==GetTranslation()){
}
AI_stuckCheck=GetTranslation();
stuckUpdate = timeGetTime();
}

However this does not seem to do what i want. Btw: is it correct to use the GetTranslation? I could also use GetWorldMatrix but thats a D3DXMATRIX ofcourse. How to calculate the differencebetween the 2 location vectors to determine when i need to rotate a character ?

##### Share on other sites
What I have done is use D3DXVec3Length to compute the length of the target position (what you want to be close to you) minus the current position (of course, where you currently are).

##### Share on other sites
Hm thats close, but not yet a solution if im right:

A
|
|
|____B

Vector a and B are both 3 units long, but are not near at all (looking from the heads).
I think in my project the actual distance would be the length between the 2 heads.
But i cant use somethinglike pythagoras as the directions vary.

##### Share on other sites
Using pythagoras would be fine, it works irregardless of which direction you're facing.

dist = sqrt((x1-x2)^2 + (y1-y2)^2)

##### Share on other sites
Pythagors needs 1 90 degrees corner, which in this example wehave not (always) got.

##### Share on other sites
Quote:
 Original post by Anonymous PosterPythagors needs 1 90 degrees corner, which in this example wehave not (always) got.

That 90 degree corner doesn't have to be aligned to any axes though, so that 90 degrees can be rotated around the origin and will still work (e.g. you can orient the paper any way you want when you draw that right angled triangle - as long as the angle in the triangle you draw is right angled.)

I suspect the word Pythagoras, and the way it's taught in school may be getting in the way here. Think of it a different (but really the same) way: Two positions A and B can be thought of as two vectors from the origin to those positions. C=B-A gets you a vector whose direction points from A to B and whose length is the distance between A and B. The dot-product of a vector with itself (d=Cx*Cx+Cy*Cy+Cz*Cz) gives you the squared length of that vector. The square root gives you the real length of that vector from the squared length and thus the distance between A and B.

[Edited by - S1CA on December 16, 2006 6:09:53 AM]

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631353
• Total Posts
2999488
×