[Solved]get new node position depending on distance

Problems building or running the engine, queries about how to use features etc.
Post Reply
Maya
Halfling
Posts: 71
Joined: Fri Oct 11, 2013 8:50 pm

[Solved]get new node position depending on distance

Post by Maya »

Hi,
given :
node1 in Pos1
node2 in Pos2
node3 in Pos3

Code: Select all


float distan1 =node1->getPosition().distance(node3->getPosition());
float distan2=node1->getPosition().distance(node2->getPosition());	
i want to get new node1 position to get distan1 equal to distan2
Last edited by Maya on Tue Feb 18, 2014 8:41 pm, edited 1 time in total.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: get new node position depending on distance

Post by c6burns »

Get a node1's position in nodeX's space (node 2 or node 3, whichever you are doing). Then normalize that and multiply by new distance. I'm pretty crap at math, but that should do it.
Maya
Halfling
Posts: 71
Joined: Fri Oct 11, 2013 8:50 pm

Re: get new node position depending on distance

Post by Maya »

Sorry but i did not understand <hat you want to say here
Get a node1's position in nodeX's space (node 2 or node 3, whichever you are doing).
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: get new node position depending on distance

Post by c6burns »

Oh nevermind, I see. You want the distances to be equal, not to use one distance to set the other. I misunderstood :)
Maya
Halfling
Posts: 71
Joined: Fri Oct 11, 2013 8:50 pm

Re: get new node position depending on distance

Post by Maya »

i make like that

Code: Select all

distance=distan1-distan2
Ogre::Vector3 newpos=Ogre::Vector3(node1.getPosition().x+distance,node1.getPosition().y,node1.getPosition().z)
when i recalculate the new distance bteween the new position of te node1 and node3 i get
that=51.4679
where the other distance =51.4302

they are not equal
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: get new node position depending on distance

Post by Kojack »

One method is constraint relaxation (often used in verlet based physics).

Code: Select all

void constrain_3d_fixed_dist(Vector3 &pos1, Vector3 &pos2, float desired_dist, float compensate1, float compensate2)
   {
      Vector3 delta = pos2 - pos1;
      float deltalength = (float)sqrt(delta.x*delta.x+delta.y*delta.y+delta.z*delta.z);
      if(deltalength > 0)
      {
         float diff = (deltalength - desired_dist) / deltalength;
         pos1 += delta * compensate1 * diff;
         pos2 -= delta * compensate2 * diff;
      }
   }
This function moves points around to maintain a fixed distance.
To move both positions freely, you would use compensate1=0.5 and compensate2=0.5 (each position moves half the error distance).
To move only the first position, use compensate1=1.0 and compensate2=0. This makes the second position locked.

In your situation there are two constraints: distance of pos1 to pos2 and distance of pos1 to pos3. Fixing one can break the other. The idea of constraint relaxation is that if you perform all the constraints several times in a row, it will eventually relax into the correct position.

So you would do something like this:

Code: Select all

float distan1 =node1->getPosition().distance(node3->getPosition());
float distan2=node1->getPosition().distance(node2->getPosition());   
Vector3 pos1 = node1.getPosition();
Vector3 pos2 = node2.getPosition();
Vector3 pos3 = node3.getPosition();
for(int i=0;i<10;i++)
{
   constrain_3d_fixed_dist(pos1, pos2, distanc2, 1.0, 0);
   constrain_3d_fixed_dist(pos1, pos3, distanc2, 1.0, 0);
}
node1.setPosition(pos1);
distanc2 is used in both lines because you said you wanted distanc1 to become equal to distanc2.

This is the technique used in the Hitman series of games to do ragdoll physics skeletons, cloth simulation and plants. I think skeletons used a loop of around 10 and plants used 3 (smaller loop makes the constraints more flexible and stretchy).
Maya
Halfling
Posts: 71
Joined: Fri Oct 11, 2013 8:50 pm

Re: get new node position depending on distance

Post by Maya »

Hi,
it's tre that it give me the same distance BUt the direction is not acepted
i will explain
when i move the node1 by the error distance , i want to move it by direction toward node3

for exemple when we reach somthing we move the wrist by distance twoard the target, (it's just exemple not my case)
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: get new node position depending on distance

Post by tod »

I think you should use square distance if you only compare distances. It's faster.
Maya
Halfling
Posts: 71
Joined: Fri Oct 11, 2013 8:50 pm

Re: get new node position depending on distance

Post by Maya »

BUt i want to use the exact distance , i don't want just compare the dsiatnces but i will use them
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: get new node position depending on distance

Post by tod »

I don't really see why you need the exact distance. But then, I don't really understand what you want to do.
Maya
Halfling
Posts: 71
Joined: Fri Oct 11, 2013 8:50 pm

Re: get new node position depending on distance

Post by Maya »

Ok
maybe i did not well explain the problem
that's figure exmplain what i have
error_dist.jpg
error_dist.jpg (10.32 KiB) Viewed 594 times

i want to move node1 by the distance "error" in the direction of node3 where
before :
distance node1-node2=dist1
distance node1-node3=dist2
AFter the setting of the node1 position i must have
distance node1-node2 =dist3;
distance node1-node3=dist1;
somthing like that
error dis2.png
error dis2.png (3.85 KiB) Viewed 593 times
you can see that i moved node1 by the distance "error"
the new distance between node1(in the new position) and node3 == the distance between node1 and node2 before the setting position (dist1)
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: get new node position depending on distance

Post by tod »

You basically need a position on the line between Node1 and Node3 that is also in the middle of the segment.
I would guess something like this should get you that vector:
Node2 = Node1 + (Node3-Node1)/2

EDIT:
Sorry, I moved node 2.
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: get new node position depending on distance

Post by tod »

Are all the nodes on a line?
Maya
Halfling
Posts: 71
Joined: Fri Oct 11, 2013 8:50 pm

Re: get new node position depending on distance

Post by Maya »

BUT not exactelly in the middle
it's just exmple
what i want is new node1 position that will be in the line of node1-node3
and by distanc from node3 eqaul to dist1
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: get new node position depending on distance

Post by tod »

So you basically have a sphere with the center at node3 and the radius equal with the amplitude of the vector node3-node2.
Then you got a line defined by node1 and node3.

So if you solve the equations of the sphere and the line, you should get the desired position.
Maya
Halfling
Posts: 71
Joined: Fri Oct 11, 2013 8:50 pm

Re: get new node position depending on distance

Post by Maya »

HI,
is't the unique solution ?
we can't use
new pos=Pos1-Pos3?

EDIT
it work when i make like that
new pos=pos1+(pos3-pos2)
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: get new node position depending on distance

Post by tod »

Man, you should really learn to explain to us what you want. I can't understand it even now that you solved it.
Anyway, mark the thread as solved (Put [Solved] a the beginning of the title).
Post Reply