www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: Marlon on January 26, 2012, 01:43:13 pm

Title: world position of a child object
Post by: Marlon on January 26, 2012, 01:43:13 pm
Hi there!

I have a parent object with world position (ie. 1,0,0) and a child object with relative position to the parent object (ie. 2,0,0). This means the world position of the child object in this case would become (3,0,0), because it's relative to the parent object's space. I would like to retrieve the world position (ie. 3,0,0) of the child.
Currently the child only knows it's relative position.
How can I achieve this? What functions I have to use to calculate it?

Thanks in advance!
Marlon
Title: Re: world position of a child object
Post by: Vi_O on January 26, 2012, 02:08:22 pm
did you try Object.getWorldTransformation() ?
Title: Re: world position of a child object
Post by: Marlon on January 26, 2012, 02:24:01 pm
did you try Object.getWorldTransformation() ?

The problem here is that I have calculated the center positions of the childs mesh of each keyframe and saved them into a ArrayList of SimpleVectors.
So what I have is the object and a SimpleVector for each keyframe (relative position in object world space).
I need to know what calculation I have to use to get a SimpleVector in world space.
Title: Re: world position of a child object
Post by: EgonOlsen on January 26, 2012, 02:40:08 pm
Just multiply the SimpleVector with the matrix returned from Object3D.getWorldTransformation().
Title: Re: world position of a child object
Post by: Marlon on January 26, 2012, 08:12:50 pm
Just multiply the SimpleVector with the matrix returned from Object3D.getWorldTransformation().

Mhhh.. in my case this doesn't seem to work.

In practise I want to add Particles to an item, the character of my game is wearing in his hand. To be even more concrete I want to make a torch which is emitting fire particles.
Because the character (and so the item) is moving while attacking or walking I need to look about the center point of the items mesh at each keyframe.
So I made a list (you already gave me some helpful tipps, egon) and added those center points to it.
With getCenterPosition() I receive the actual position.
Unfortunatly this position is in object space and I need the coordinates in world space.

Here is my code:
Code: [Select]
float cx=0, cy=0, cz=0;
SimpleVector tmpVec=obj3D.getCenterPosition();
if(tmpVec!=null) {
tmpVec.matMul(obj3D.getWorldTransformation());
cx=tmpVec.x*obj3D.getScale();
cy=tmpVec.y*obj3D.getScale();
cz=tmpVec.z*obj3D.getScale();
}
particlePosition = new SimpleVector(obj3D.getTranslation().x+cx, obj3D.getTranslation().y+cy, obj3D.getTranslation().z+cz);

Although mathematic was my intensive course at shool the third dimension is always bringing confusion to my brain.  :o
It would be great if somebody could help me.
Title: Re: world position of a child object
Post by: EgonOlsen on January 26, 2012, 09:06:22 pm
This should be sufficient:

Code: [Select]
float cx=0, cy=0, cz=0;
SimpleVector tmpVec=obj3D.getCenterPosition();
if(tmpVec!=null) {
tmpVec.matMul(obj3D.getWorldTransformation());
}
particlePosition = new SimpleVector(tmpVec);

The method returns the world transformation(!)...it should include all rotations/translations/scalings.
Title: Re: world position of a child object
Post by: Marlon on January 26, 2012, 09:13:56 pm
I already tested this, but there are no particles.

When I use my code (except tmpVec.matMul(obj3D.getWorldTransformation()) particles are shown (always at 12 o clock).
So when I rotate the character with 180 degree, they are exactly on the wrong position ^^. Not easy...

I am going to debug this thing and write here later...
Thanks!
Title: Re: world position of a child object
Post by: EgonOlsen on January 26, 2012, 09:16:09 pm
Maybe because you are working directly on the instances? Have you tried to use the copy of the center position vector instead?
Title: Re: world position of a child object
Post by: Marlon on January 26, 2012, 09:23:47 pm
Maybe because you are working directly on the instances? Have you tried to use the copy of the center position vector instead?

Arghhh.. i just found it out while debugging! Yes, now it works! Damnnn, I forgot that tempVec is an instance which is saved in the mentioned ArrayList.

The effect now looks great! I can't wait for releasing this! :)

THANKS!!!
Marlon