•      Sign In
• Create Account

# Orientation trouble

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

17 replies to this topic

### #1lucky6969b  Members   -  Reputation: 1077

Like
0Likes
Like

Posted 29 July 2011 - 05:11 AM

Hello guys,

The code snippet shown below is used for coordinating multiple objects to move around without bumping into each other.
You may assume the pathfinder will give a straight path from one point to another.
The problem I am having is the orientation of the objects. The outcome of the code below make the objects swing left and right randomly.
My intention is the object must follow the "direction" of the pathfinder.
I should highlight this line
float angle = atan2(m_velocity.z, -m_velocity.x) + D3DX_PI * 0.5f;


The output is out of expected.
What change(s) should I make in order to achieve the original intention?


#include "stdafx.h"
#include "CrowdEntity.h"
#include "Crowd.h"
#include "Pathfinder\Thread.h"

extern IDirect3DDevice9 *g_pDevice;
extern ID3DXEffect *g_pEffect;
HANDLE g_hMutex;

void CrowdEntity::PathFind(D3DXVECTOR3 s, D3DXVECTOR3 d,  std::vector<D3DXVECTOR3>& p)
{
static HANDLE hEvent;
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
g_hMutex = CreateMutex(NULL, FALSE, NULL);
D3DXVECTOR2 s1,d1;
s1 = D3DXVECTOR2(s.x, s.z);
d1 = D3DXVECTOR2(d.x, d.z);
PFThread thr(1, hEvent, s1, d1, 0.78, false);

thr.SetRunning(true);
thr.GetAStar()->SetClearance(5);
thr.run();
std::vector<D3DXVECTOR2> path;
path = thr.GetPath();

for (int i = path.size()-1; i > 0; i--)
{
D3DXVECTOR3 v;
v.x = path[i].x;
v.y = 0;
v.z = path[i].y;
p.push_back(v);
}

}

CrowdEntity::CrowdEntity(Crowd *pCrowd)
{

//if (cm_CBMesh == NULL)
//{
cm_CBMesh = new CMesh();
cm_CBMesh->Load("cb.dat");
//}

m_pCrowd = pCrowd;

m_position = GetRandomLocation();
m_goal = GetRandomLocation();

PathFind(m_position, m_goal, m_Path);

if (m_Path.size() > 0)
m_position = m_Path[0];

D3DXVec3Normalize(&m_velocity, &m_position);

m_count = 0;

//	m_pAnimController = sm_OperatorMesh->GetAnimController();

///	ID3DXAnimationSet *anim = NULL;
//	m_pAnimController->GetAnimationSet(0, &anim);
//	m_pAnimController->SetTrackAnimationSet(0, anim);
//	anim->Release();
}

CrowdEntity::~CrowdEntity()
{
if (m_pAnimController != NULL)
m_pAnimController->Release();
}

void CrowdEntity::Update(float deltaTime)
{
const float ENTITY_INFLUENCE_RADIUS = 100.0f;
const float NEIGHBOR_REPULSION = 50.0f;
const float ENTITY_SPEED = 200.0f;
const float ENTITY_SIZE = 100.0f;

//Force towards goal

if (m_count < m_Path.size())
{
// m_position derived from getRandomPosition();
// now is dependent on m_Path[m_count];
m_position = m_Path[m_count];
D3DXVECTOR3 forceToGoal = m_goal - m_position;

//Has goal been reached?
if(D3DXVec3Length(&forceToGoal) < ENTITY_INFLUENCE_RADIUS)
{
//Pick a new random goal
m_goal = GetRandomLocation();
PathFind(m_position, m_goal, m_Path);
m_count = 0;
m_position = m_Path[0];
}
D3DXVec3Normalize(&forceToGoal, &forceToGoal);

//Get neighbors
vector<CrowdEntity*> neighbors;
m_pCrowd->GetNeighbors(this, ENTITY_INFLUENCE_RADIUS, neighbors);

//Avoid bumping into close neighbors
D3DXVECTOR3 forceAvoidNeighbors(0.0f, 0.0f, 0.0f);
for(int i=0; i<(int)neighbors.size(); i++)
{
D3DXVECTOR3 toNeighbor = neighbors[i]->m_position - m_position;
float distToNeighbor = D3DXVec3Length(&toNeighbor);

toNeighbor.y = 0.0f;
float force = 1.0f - (distToNeighbor /  ENTITY_INFLUENCE_RADIUS);
forceAvoidNeighbors += -toNeighbor * NEIGHBOR_REPULSION *  force;

//Force move intersecting entities
if(distToNeighbor < ENTITY_SIZE)
{
D3DXVECTOR3 center = (m_position +  neighbors[i]->m_position) * 0.5f;
D3DXVECTOR3 dir = center - m_position;
D3DXVec3Normalize(&dir, &dir);

//Force move both entities
m_position = center - dir * ENTITY_SIZE * 0.5f;
neighbors[i]->m_position = center + dir * ENTITY_SIZE *  0.5f;
}
}

//Sum up forces
D3DXVECTOR3 acc = forceToGoal + forceAvoidNeighbors;
D3DXVec3Normalize(&acc, &acc);

//Update velocity & position
m_velocity += acc * deltaTime;
D3DXVec3Normalize(&m_velocity, &m_velocity);
m_position += m_velocity * ENTITY_SPEED * deltaTime;
m_count++;
}
//Update animation
//	m_pAnimController->AdvanceTime(deltaTime, NULL);
}

void CrowdEntity::Render()
{
D3DXMATRIX world, pos, rot;

//Position
//D3DXMatrixTranslation(&pos, m_position.x, m_position.y,  m_position.z);

D3DXMatrixTransformation(&pos, NULL, NULL,  &D3DXVECTOR3(0.5,0.5,0.5), NULL, NULL, &m_position);

//Orientation
float angle = atan2(m_velocity.z, -m_velocity.x) + D3DX_PI * 0.5f;

D3DXMatrixRotationY(&rot, angle);

//Set pose and render
world = rot * pos;

g_pDevice->SetTransform(D3DTS_WORLD, &world);
cm_CBMesh->Render();

}

D3DXVECTOR3 CrowdEntity::GetRandomLocation()
{
return D3DXVECTOR3(((rand()%1000 / 10.0f)) * 20.0f - 10.0f,
0.0f,
((rand()%1000 / 10.0f)) * 20.0f - 10.0f);//  /1000.0f
}


### #2DJTN  Members   -  Reputation: 207

Like
1Likes
Like

Posted 29 July 2011 - 09:58 AM

Look at your World Transforms. If you want the objects to face a spacific object you'll need to handle that by setting up a new "LookatLH" Matrix to replace your current roataion transforms.

### #3lucky6969b  Members   -  Reputation: 1077

Like
0Likes
Like

Posted 30 July 2011 - 12:56 AM

DJTN, thank you for your response...
Actually, I don't need the NPC to face the player. I just want it to face the direction it's moving in.
I am just a bit slow at this. Why do we get an angle from this calculation?
atan2f(m_velocity.z, -m_velocity.x) + D3DX_PI * 0.5f

because I was copying this code from a book....
Are there any illustrations for this kind of angle-to-tangent relationship stuff?
Another problem being is the objects are constantly spinning about the y-axis

BTW: Anyways that I can post animated gif to this forum?

Thanks
Jack

### #4haegarr  Crossbones+   -  Reputation: 7042

Like
1Likes
Like

Posted 30 July 2011 - 02:15 AM

The tangent function computes the length ratio of the opposite to the adjacent in a right-angled triangle, based on the angle between the 2 said legs (see e.g. this wikipedia article). The arc tangent (or atan for short) is the inverse function to the tangent: It gives you the angle between the 2 legs of a right-angled triangle where the lengths are known.

Because the argument to atan is a ratio, e.g. a/b, you have to ensure that b isn't null (division by zero) and you loose information (i.e. a/b is the same as -a/-b, and -a/b is the same as a/-b). That restricts the usability of atan. Hence atan2 was invented (see e.g. this wikipedia article), which is in fact a "macro function" that is able to deal with b == 0 and the signs, just because a and b are given separately.

So, atan2 gives you an angle, and you add an angle (D3DX_PI) weighted by half. In summary you have an angle.

### #5lucky6969b  Members   -  Reputation: 1077

Like
0Likes
Like

Posted 30 July 2011 - 02:34 AM

So atan2f is the inverse of tangent. When I do a D3DXMatrixRotationY(m_velocity.z, -m_velocity.x), I look down from the top and calculate the angle between z and negative x.
Okay, I can understand this.
However, I am still clueless about why the statement D3DXMatrixRotationY(...) makes the objects spin non-stop
When I don't add this statement m_position = m_Path[m_count]; The orientation is correct. In effect, when there is no pathfinder.
Could you please check my code in the 1st post? I just couldn't contact the author of the book.
http://m.UploadEdit.com/b96/75626232.gif
Thanks
Jack

### #6lucky6969b  Members   -  Reputation: 1077

Like
0Likes
Like

Posted 30 July 2011 - 04:22 AM

For example, if the velocity vector has magnitude z => -0.1 x=>0.1
tan-1(-0.1/0.1) = -0.7615
So the angle is -0.7615 * 180 / PI => -43 degrees
which is correct.
How come the objects in the scene are spinning. pretty hairy?

ori: 265.330902 degrees
ori: 264.489746 degrees
ori: 263.595551 degrees
ori: 263.595551 degrees
ori: 262.760132 degrees
ori: 262.760132 degrees
ori: 261.872437 degrees
ori: 260.988373 degrees
ori: 260.988373 degrees
ori: 260.162933 degrees
ori: 259.286499 degrees
ori: 259.286499 degrees
ori: 258.468475 degrees
ori: 257.600311 degrees
ori: 257.600311 degrees
ori: 256.736694 degrees
ori: 256.736694 degrees
ori: 255.931183 degrees
ori: 255.076813 degrees
ori: 255.076813 degrees
ori: 254.280273 degrees
ori: 253.435776 degrees
ori: 253.435776 degrees
ori: 252.596603 degrees
ori: 252.596603 degrees
ori: 251.814682 degrees
ori: 250.986252 degrees
ori: 250.986252 degrees
ori: 250.214569 degrees
ori: 249.397324 degrees
ori: 249.397324 degrees
ori: 248.586029 degrees
ori: 247.830795 degrees
ori: 247.830795 degrees
ori: 247.031387 degrees
ori: 247.031387 degrees
ori: 246.287445 degrees
ori: 245.500290 degrees
ori: 245.500290 degrees
ori: 244.719635 degrees
ori: 243.993576 degrees
ori: 243.993576 degrees
ori: 243.225693 degrees
ori: 243.225693 degrees
ori: 242.511719 degrees
ori: 241.756958 degrees
ori: 241.756958 degrees
ori: 241.009064 degrees
ori: 240.313965 degrees
ori: 240.313965 degrees
ori: 239.579498 degrees
ori: 239.579498 degrees
ori: 238.897125 degrees
ori: 238.176331 degrees
ori: 238.176331 degrees
ori: 237.462646 degrees
ori: 236.799835 degrees
ori: 236.799835 degrees
ori: 236.100037 degrees
ori: 236.100037 degrees
ori: 235.450302 degrees
ori: 234.764496 degrees
ori: 234.764496 degrees
ori: 234.085938 degrees
ori: 233.456192 degrees
ori: 233.456192 degrees
ori: 232.791733 degrees
ori: 232.175201 degrees
ori: 232.175201 degrees
ori: 231.524872 degrees
ori: 231.524872 degrees
ori: 230.881851 degrees

The orientation seems to be changing all the time

Thanks

### #7haegarr  Crossbones+   -  Reputation: 7042

Like
0Likes
Like

Posted 30 July 2011 - 05:04 AM

Although don't knowing the book, I would say there are some strange issues within the OP's code snippet. Maybe I'm wrong, but ...

1. Is PFThread really a concurrently running piece of program? IMHO such stuff is difficult enough to be used by experienced programmers only. BTW, what purpose has the mutex? It seems not to be used.

2. What is the path good for? It is computed every time a new goal is picked, i.e. it lasts for some time. In CrowdEntity::Update the current position is picked as a pre-computed sample on the path in this line
m_position = m_Path[m_count];

and later the path index is incremented in this line
m_count++;

without altering the path in-between. That means that each iteration through the update function will pick a pre-computed position, doesn't it?

But in-between those lines, the real position is computed from the picked position plus an offset towards the goal (notice: towards the goal and not towards the next path segment) plus an offset computed to eventually avoid other objects.

So, IMHO, the position of the object at the end of frame n-1 (i.e. when the object is rendered) and the position of the same object at the beginning of frame n (i.e. when its next position is computed due to movement) are not the same! That means that the object are jumping a bit from frame to frame.

Because the orientation depends on the movement, but the movement is not exactly related to position changes (see above), the orientation may not look at expected. Moreover, because the movement is computed towards the goal and considering the obstacle avoidance, you won't see it point along the path anyway.

I would try to simplify things. E.g. restrict the world to a single objects, comment out the obstacle avoidance stuff, print out the path and step through the code using a debugger. During that, check whether the computation of the position and later the orientation matches the expectations.

### #8haegarr  Crossbones+   -  Reputation: 7042

Like
0Likes
Like

Posted 30 July 2011 - 05:08 AM

ori: 265.330902 degrees
ori: 264.489746 degrees
ori: 263.595551 degrees
ori: 263.595551 degrees
ori: 262.760132 degrees
ori: 262.760132 degrees
...

The orientation seems to be changing all the time

Yes, but it changes gradually only. And there is no hint at the rate of change given here. And at least we don't know the path. Assume that the path is not straight but a curve, so something like the shown angles are to be expected. And last but not least, we don't know something about obstacles in the world.

### #9lucky6969b  Members   -  Reputation: 1077

Like
0Likes
Like

Posted 30 July 2011 - 06:15 AM

Hi,
According to your advices, I modified my coding a bit. This time, I compared 2 samples in the path to calculate the direction. the object is very unstable (swaying) although the direction looks fine
void CrowdEntity::Update(float deltaTime)
{
const float ENTITY_INFLUENCE_RADIUS = 100.0f;
const float NEIGHBOR_REPULSION = 50.0f;
const float ENTITY_SPEED = 200.0f;
const float ENTITY_SIZE = 100.0f;

//Force towards goal

if (m_count < m_Path.size()-2)
{
// m_position derived from getRandomPosition();
// now is dependent on m_Path[m_count];
m_position = m_Path[m_count];
//D3DXVECTOR3 pos1 = m_Path[m_count];
D3DXVECTOR3 posInc = m_Path[m_count+1];
D3DXVECTOR3 forceToGoal = posInc - m_position;

//Has goal been reached?
if(D3DXVec3Length(&forceToGoal) < ENTITY_INFLUENCE_RADIUS)
{
//Pick a new random goal
m_goal = GetRandomLocation();
PathFind(m_position, m_goal, m_Path);
m_count = 0;
m_position = m_Path[0];
}
D3DXVec3Normalize(&forceToGoal, &forceToGoal);

//Get neighbors
vector<CrowdEntity*> neighbors;
m_pCrowd->GetNeighbors(this, ENTITY_INFLUENCE_RADIUS, neighbors);

//Avoid bumping into close neighbors
D3DXVECTOR3 forceAvoidNeighbors(0.0f, 0.0f, 0.0f);
for(int i=0; i<(int)neighbors.size(); i++)
{
D3DXVECTOR3 toNeighbor = neighbors[i]->m_position - m_position;
float distToNeighbor = D3DXVec3Length(&toNeighbor);

toNeighbor.y = 0.0f;
float force = 1.0f - (distToNeighbor / ENTITY_INFLUENCE_RADIUS);
forceAvoidNeighbors += -toNeighbor * NEIGHBOR_REPULSION * force;

//Force move intersecting entities
if(distToNeighbor < ENTITY_SIZE)
{
D3DXVECTOR3 center = (m_position + neighbors[i]->m_position) * 0.5f;
D3DXVECTOR3 dir = center - m_position;
D3DXVec3Normalize(&dir, &dir);

//Force move both entities
m_position = center - dir * ENTITY_SIZE * 0.5f;
neighbors[i]->m_position = center + dir * ENTITY_SIZE * 0.5f;
}
}

//Sum up forces
D3DXVECTOR3 acc = forceToGoal + forceAvoidNeighbors;
D3DXVec3Normalize(&acc, &acc);

//Update velocity & position
m_velocity += acc * deltaTime;
D3DXVec3Normalize(&m_velocity, &m_velocity);
m_position += m_velocity * ENTITY_SPEED * deltaTime;
m_count++;
}

}


Do I still miss something?
http://m.UploadEdit....b96/5397764.gif
Another variation..
http://m.UploadEdit.com/b96/27894784.gif
Thanks
Jack

### #10haegarr  Crossbones+   -  Reputation: 7042

Like
0Likes
Like

Posted 30 July 2011 - 07:03 AM

Hi,
According to your advices, I modified my coding a bit. This time, I compared 2 samples in the path to calculate the direction. the object is very unstable (swaying) although the direction looks fine
...
Do I still miss something?

Yes: You still mix up a path and the actual movement. IMHO the entire stuff dealing with the path is not only not necessary but harmful. Why do you pre-compute a path and then try to drive the object physically?

### #11lucky6969b  Members   -  Reputation: 1077

Like
0Likes
Like

Posted 30 July 2011 - 07:20 AM

So I am stuck
Are there any alternatives to driving the object physically with a path?
Thanks
Jack

### #12haegarr  Crossbones+   -  Reputation: 7042

Like
0Likes
Like

Posted 30 July 2011 - 07:34 AM

But you understand what problem I see within that approach? Maybe I'm missing something, but until now I'm quite sure.

What happens if you try something like the following:
void CrowdEntity::Update(float deltaTime)
{
const float ENTITY_INFLUENCE_RADIUS = 100.0f;
const float NEIGHBOR_REPULSION = 50.0f;
const float ENTITY_SPEED = 200.0f;
const float ENTITY_SIZE = 100.0f;

D3DXVECTOR3 forceToGoal = m_goal - m_position;

//Has goal been reached?
if(D3DXVec3Length(&forceToGoal) < ENTITY_INFLUENCE_RADIUS)
{
//Pick a new random goal
m_goal = GetRandomLocation();
forceToGoal = m_goal - m_position;
}
D3DXVec3Normalize(&forceToGoal, &forceToGoal);

//Get neighbors
vector<CrowdEntity*> neighbors;
m_pCrowd->GetNeighbors(this, ENTITY_INFLUENCE_RADIUS, neighbors);

//Avoid bumping into close neighbors
D3DXVECTOR3 forceAvoidNeighbors(0.0f, 0.0f, 0.0f);
for(int i=0; i<(int)neighbors.size(); i++)
{
D3DXVECTOR3 toNeighbor = neighbors[i]->m_position - m_position;
float distToNeighbor = D3DXVec3Length(&toNeighbor);

toNeighbor.y = 0.0f;
float force = 1.0f - (distToNeighbor / ENTITY_INFLUENCE_RADIUS);
forceAvoidNeighbors += -toNeighbor * NEIGHBOR_REPULSION * force;

//Force move intersecting entities
if(distToNeighbor < ENTITY_SIZE)
{
D3DXVECTOR3 center = (m_position + neighbors[i]->m_position) * 0.5f;
D3DXVECTOR3 dir = center - m_position;
D3DXVec3Normalize(&dir, &dir);

//Force move both entities
m_position = center - dir * ENTITY_SIZE * 0.5f;
neighbors[i]->m_position = center + dir * ENTITY_SIZE * 0.5f;
}
}

//Sum up forces
D3DXVECTOR3 acc = forceToGoal + forceAvoidNeighbors;
D3DXVec3Normalize(&acc, &acc);

//Update velocity & position
m_velocity += acc * deltaTime;
D3DXVec3Normalize(&m_velocity, &m_velocity);
m_position += m_velocity * ENTITY_SPEED * deltaTime;
}


Please tell us the sense of PathFind(). What is it computing, and what is the purpose of the resulting path?

EDIT: direction of forceToGoal negated, and re-computation of that value on new goal inserted.

Edited by haegarr, 30 July 2011 - 07:37 AM.

### #13lucky6969b  Members   -  Reputation: 1077

Like
0Likes
Like

Posted 30 July 2011 - 07:41 AM

Wow, yeah. This is the original program...
The pathfind() function just computes a straight path with no obstacles for the time being.
I need a pre-computed path because I don't want the objects to wander freely....
Do you want me to send the whole stuff to you to have a look at it? It's very small
Thanks
Jack

### #14haegarr  Crossbones+   -  Reputation: 7042

Like
0Likes
Like

Posted 30 July 2011 - 07:51 AM

Wow, yeah. This is the original program...

Ohh. Okay. So you already knew that version ;) However, does it show the same problem? Probably not, so the problem is really in the mix-up as said!?

The pathfind() function just computes a straight path with no obstacles for the time being.
I need a pre-computed path because I don't want the objects to wander freely....

Well, they never wander freely because they wander towards a goal. Do you want them to reach the goal not on a straight line (when ignoring the avoidances here) but more-or-less along a path prescription? So you can give them a curved way, for example? Is that the intention?

Do you want me to send the whole stuff to you to have a look at it? It's very small

Not for the moment, because I still haven't understood the whys and whats, and I probably won't see this in the code.

### #15lucky6969b  Members   -  Reputation: 1077

Like
0Likes
Like

Posted 30 July 2011 - 07:53 AM

Can the objects dodge static obstacles with this coding? I don't need a pathfinder?
But i need a precise path. because it's a computational project...
The path comprises of straight and curved portions
Thanks
Jack

### #16haegarr  Crossbones+   -  Reputation: 7042

Like
0Likes
Like

Posted 30 July 2011 - 08:52 AM

Can the objects dodge static obstacles with this coding? I don't need a pathfinder?

The objects can avoid dynamic obstacles (just the other moving objects) and hence I'd say, yes, they can avoid static obstacles, too. Maybe it requires some tweaking though.

The implemented method is this: The object tries to go step straight into the direction of the goal. But there is an obstacle in the way. If the obstacle is far enough then the objects does the step as-is. If it is too close then the object gets a pulse into a direction to avoid a collision.

But i need a precise path. because it's a computational project...
The path comprises of straight and curved portions

If the paths of all moving objects are pre-defined, then don't use dynamic avoidance and physical motion. Use a timeline animation instead, so you'll have all under control. Run the animation until you see a collision, then tweak the animation curves as needed.

### #17lucky6969b  Members   -  Reputation: 1077

Like
0Likes
Like

Posted 01 August 2011 - 04:52 AM

Excuse me for my dumbness. But what is a timeline animation specifically? And also, Are there any good web references for me to take a look at up to this point? And of course, source code Thanks Jack

### #18haegarr  Crossbones+   -  Reputation: 7042

Like
0Likes
Like

Posted 03 August 2011 - 02:36 AM

With timeline animation I mean that the placements of all objects are pre-defined, manually tweaked, driven by a timeline.

Should the environment be dynamic? Should the situation be evaluated each frame, and the movement adapted accordingly? I.e. the movement is computed on-the-fly? Hence the path will look zigzag like but smoothed because sudden changes in velocity cannot occur. Then there is no sense in computing a path in advance. An addition may be that the path an agent can choose is constraint by the environment.

Or else ... Should the path be computed in advance? So that it is defined, all situations in the environment predetermined? No surprising encounters? Then a hand tweaked timeline animation can be used, yielding in a totally deliberate movement.

Your OP contains a mix of both. It is just that mix that makes your code snippet not work. Hence my question: What is the goal? Why do you think you need both approaches together?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS