Sign in to follow this  
Mike2H

Calculate distance between 2 translations/Models

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()){
			AddRotation(D3DXVECTOR3( 0.0f, -1.5f, 0.0f ));
		}
	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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Pythagors needs 1 90 degrees corner, which in this example wehave not (always) got.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Pythagors 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]

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

Sign in to follow this