About Store Forum Documentation Contact



Post Reply 
Placing object in world relative to position in object space
Author Message
JonathonA Offline
Member

Post: #1
Placing object in world relative to position in object space
Hello there, ran into an issue where I want to attach an object (in this case a sword) to a character models hand.

Now if you have read the above then you might suggest I do this:

Code:
if(C OrientP *point=cskel.findPoint("HandR"))
      {
         item  = &T.item(slots[SLOT_HAND_R]);
         Matrix  matrix=item->matrix(); // get current matrix

         item->matrix(Matrix().setPosDir(point->pos, point->dir, point->cross())); // set new matrix
         item->update();

         // immediately overwrite item members after calling 'update'
         GetVel(item->vel, item->ang_vel, matrix, item->matrix()); // calculate velocities between old and new matrixes
      }

Which okay it does sort of work but my problem is that this sword has a point on its handle called "grip" and I want the sword to be positioned so that the handle is in the characters hand (or in other words, I want to position the sword at the "HandR" point but relative to the "grip" point on the sword).

I am not sure how to accomplish this; I basically want to position an item relative to a point within its local space?
(This post was last modified: 03-20-2012 01:47 PM by JonathonA.)
03-18-2012 01:04 AM
Visit this user's website Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #2
RE: Attaching an object to a model
Okay so I have been playing around with this; Here is my first attempt which hasn't worked, so if anyone has suggestions about where I am going wrong I'd appreciate it.

Using the above code I:
1. set the item position based on the "HandR" point which is already transformed to world space.
2. got the "grip" point (which is still in local space) and transform that to world space using the item matrix (since it has been set to world space by this point).
3. Get the displacement vector (We'll call it: disVec) between the transformed "grip" point and the item position.
4. Set the item position again by using item->pos + disVec.

That hasn't worked for me?
03-18-2012 12:23 PM
Visit this user's website Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #3
RE: Attaching an object to a model
Hmm easy enough to resolve this using the Editor and making sure the handle of item is centred at the point of origin (0,0,0) in its local space but still struggling to achieve this in code.

I have tried to offset it in its local space before I position it using the code above but no success with that either.
03-18-2012 01:35 PM
Visit this user's website Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #4
RE: Attaching an object to a model
This is getting a little bit messy now; Bottom line is I am trying to position an item in the players hand (in world space) relative to a Point on the item (in item space) and NOT relative to the items origin (or identity matrix) which is what happens at the moment.
03-19-2012 01:57 AM
Visit this user's website Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #5
RE: Attaching an object to a model
Okay so here is some really sketchy code (3D math is not my expert field so apologies for this) but basically I end up right back at the beginning and it just positions it according to the center of the weapon (in its local space)?

Code:
C OrientP *point=cskel.findPoint("HandL");

         item  = &T.item(slots[SLOT_HAND_L]);
         Matrix  matrix=item->matrix(); // get current matrix

         OrientP p = *item->mesh->skeleton()->findPoint("grip"); // get "grip" point; it is in local space so we are gonna have to transform it

         p *= cskel.findBone("Bip01_L_Hand"->matrix(); // transform the "grip" point using the hand matrix since that is in world space

         Vec d = point->pos - p.pos; // get the displacement from the "HandL" point to the "grip" point to determine how far we have to offset the weapon
        
         p.pos += d; // displace the item position so the the handle of the weapon sits in the players hand

         item->matrix(Matrix().setPosDir(p.pos, point->dir, point->perp));
03-19-2012 07:37 PM
Visit this user's website Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #6
RE: Attaching an object to a model
Can somebody tell me if it would be better to offset the weapon in its local space before I set its position in the world at least? Every time I have tried offsetting it in the local space, when I come to placing it in the world it disappears.
03-20-2012 08:25 AM
Visit this user's website Find all posts by this user Quote this message in a reply
EthanC Offline
Member

Post: #7
RE: Attaching an object to a model
As much as I'm against the practice of "bumping" things on internet posts, watching Jon have entire conversations with himself on these support forums is no fun either.

The support started out so well a few months ago. Where are all those helpful people now?
03-20-2012 08:49 AM
Find all posts by this user Quote this message in a reply
Skykill Offline
Member

Post: #8
RE: Attaching an object to a model
(03-20-2012 08:49 AM)EthanC Wrote:  As much as I'm against the practice of "bumping" things on internet posts, watching Jon have entire conversations with himself on these support forums is no fun either.

The support started out so well a few months ago. Where are all those helpful people now?
Work/School? smile. It's work for me.
03-20-2012 09:53 AM
Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #9
RE: Attaching an object to a model
(03-20-2012 09:53 AM)Skykill Wrote:  
(03-20-2012 08:49 AM)EthanC Wrote:  As much as I'm against the practice of "bumping" things on internet posts, watching Jon have entire conversations with himself on these support forums is no fun either.

The support started out so well a few months ago. Where are all those helpful people now?
Work/School? smile. It's work for me.

Understood, just surprised the forum went so quiet over the last few days and hope we haven't offended anyone; Moving back to my self-convo wink...

Matrix().setPosDir(...) I am assuming is the source of most of my troubles with all this. The item is placed in the world based on the items center or identity at the moment but as you all know I want it placed based on a point within the items space.

Am I right in thinking that Matrix() initializes the matrix to an identity? If so, then it is just positioning the item based on that which is why the center of my item is ending up in the hand?

If that is right, then if I create a Matrix(), offset it (move it) to the "grip" position then call setPosDir(...) with the HandR or HandL point position/direction for it to be placed in the world?
(This post was last modified: 03-20-2012 12:26 PM by JonathonA.)
03-20-2012 12:24 PM
Visit this user's website Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #10
RE: Placing object in world relative to position in object space
As I mentioned before I apologise for the multiple posts, but I am just trying to show that I am working on this and not expecting it to be written for me or anything like that. I'll sort one or two images out later on after I try one or two other things that have crossed my mind and go from there. Both your suggestions I have tried though but thank you for mentioning them. smile
(This post was last modified: 03-20-2012 02:41 PM by JonathonA.)
03-20-2012 02:33 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Abril Offline
Member

Post: #11
RE: Attaching an object to a model
(03-18-2012 12:23 PM)JonathonA Wrote:  Okay so I have been playing around with this; Here is my first attempt which hasn't worked, so if anyone has suggestions about where I am going wrong I'd appreciate it.

Using the above code I:
1. set the item position based on the "HandR" point which is already transformed to world space.
2. got the "grip" point (which is still in local space) and transform that to world space using the item matrix (since it has been set to world space by this point).
3. Get the displacement vector (We'll call it: disVec) between the transformed "grip" point and the item position.
4. Set the item position again by using item->pos + disVec.

That hasn't worked for me?

1) get the original offset within the item in local space (easier to do and can be done only once and stored in the Item)
2) at runtime, transofmr the item offset by the item matrix, getting the displacement
3) apply the displacement to a copy of the Item matrix
4) place the Item with this new matrix

In pseuodocode would look like this (this doesnt compile - psuedocode to show you the operations):

Matrix mat1=pItem->getmatrix;
Vec vDisp=pItem.vOffset.mul(mat1);
Matrix mat2=mat1.move(vDisp);
pItem->setmatrix(mat2)

This should be enough to get you guys on the right track...
smile
03-20-2012 03:50 PM
Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #12
RE: Attaching an object to a model
(03-20-2012 03:50 PM)Abril Wrote:  
(03-18-2012 12:23 PM)JonathonA Wrote:  Okay so I have been playing around with this; Here is my first attempt which hasn't worked, so if anyone has suggestions about where I am going wrong I'd appreciate it.

Using the above code I:
1. set the item position based on the "HandR" point which is already transformed to world space.
2. got the "grip" point (which is still in local space) and transform that to world space using the item matrix (since it has been set to world space by this point).
3. Get the displacement vector (We'll call it: disVec) between the transformed "grip" point and the item position.
4. Set the item position again by using item->pos + disVec.

That hasn't worked for me?

1) get the original offset within the item in local space (easier to do and can be done only once and stored in the Item)
2) at runtime, transofmr the item offset by the item matrix, getting the displacement
3) apply the displacement to a copy of the Item matrix
4) place the Item with this new matrix

In pseuodocode would look like this (this doesnt compile - psuedocode to show you the operations):

Matrix mat1=pItem->getmatrix;
Vec vDisp=pItem.vOffset.mul(mat1);
Matrix mat2=mat1.move(vDisp);
pItem->setmatrix(mat2)

This should be enough to get you guys on the right track...
smile

Thanks for the suggestions Abril, definitely puts me on the right track and answers one or two questions I have smile
03-20-2012 04:30 PM
Visit this user's website Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #13
RE: Placing object in world relative to position in object space
Fairly long post; if you don't want to read it please just skip to the bottom smile

   

Okay so the picture you see above results from this code (only included the parts that operate on item position) and you can see the problem (now I have finally posted a picture!):

Code:
void Inventory::update(...)
{
      if(C OrientP *point=cskel.findPoint("HandL"))
      {
          item  = &T.item(slots[SLOT_HAND_L]);
          Matrix  matrix=item->matrix(); // get current matrix
                  Matrix& finalPos = Matrix().setPosDir(point->pos, point->dir);
                  item->matrix(finalPos);
      }
}

So at this point when I thought about getting the offset in local space and then Abril suggested it I started to try and implement Abrils suggestion. I added this line to Item::create to store the displacement from the center of the weapon (where the hand is currently holding it to the "grip") in local space:

Code:
void Item::create(NetItem& src)
{
gripOffset = mesh->skeleton()->getPoint("grip").pos - mesh->box.center();
}

Next step was to transform that offset and move the item to that position which I tried to do like this:

Code:
void Inventory::update(...)
{
      if(C OrientP *point=cskel.findPoint("HandL"))
      {
          item  = &T.item(slots[SLOT_HAND_L]);
          Matrix  matrix = item->matrix(); // get current matrix
                  Vec displacement = item->gripOffset * matrix;
                  Matrix& finalPos = Matrix().setPosDir(point->pos, point->dir);
                  finalPos.move(displacement);
                  item->matrix(finalPos);
      }
}


Loaded up the client and uh oh, look whats happened:

   

So I had a look at the vectors/matrixes at runtime and the displacement vector components were getting bigger and bigger. Then I figured out why; item position is being set to point->pos ( .setPosDir(point->pos...) ), then moved by displacement ( finalPos.move(displacement) ) every frame. Then in the next call to Inventory::update the item matrix pos has now changed...but then it applies the same calculation again and so on and basically it gets larger and larger so that is completely wrong.

So first step I removed:
Code:
Vec displacement = item->gripOffset * matrix;
and replaced it with:
Code:
Vec displacement = item->gripOffset;

So here is what it looked like now:

   

So at least the weapon is there now but obviously it isn't correct since displacement vec hasn't been transformed to world space. So I replaced:
Code:
Vec displacement = item->gripOffset;
with
Code:
Vec displacement = item->gripOffset * Matrix().setPosDir(point->pos, point->dir, point->cross());
and changed this line
Code:
Matrix& finalPos = Matrix().setPosDir(point->pos, point->dir);
to
Code:
Matrix& finalPos = Matrix().setPosDir(d, point->dir);
and now I am so much closer, but we are gripping the wrong end of the item now:

   

Only reason that could happen is if the displacement vector was in the wrong direction, and of course it was because I was doing gripPosition - weaponCenter so all I had to do was then flip it around weaponCenter - gripPosition and here we have it:

   

A massive massive massive thank you to Abril and Aceio76, really appreciate the help on this smile
03-20-2012 07:10 PM
Visit this user's website Find all posts by this user Quote this message in a reply
evldmn Offline
Member

Post: #14
RE: Placing object in world relative to position in object space
forget about working and stuff, this guy and his sword look awesome, wow...
03-21-2012 06:59 AM
Find all posts by this user Quote this message in a reply
JonathonA Offline
Member

Post: #15
RE: Placing object in world relative to position in object space
smile glad you like him, he is one of the several races in the game; plenty more on its way but I'll leave that up to the PM, EthanC to show off wink
03-21-2012 08:50 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply