• Advertisement
Sign in to follow this  

Transform Matrix and Pivot

This topic is 769 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Community,

 

i have a big Problem, i want to rotate a cube around the Y axis and after it, to move it on the X axis, but the cube still move on the X axis and not in the angle, i had rotated it befor and i don't know what i made wrong, maybe you guys can help me and find a solution?

 

Here's my code:

 

            Matrix scale = Matrix.Scaling(this.scale);

            Matrix rotation = Matrix.RotationYawPitchRoll(Mathematics.DegreesToRadians(rotate.Y),
                Mathematics.DegreesToRadians(rotate.X),
                Mathematics.DegreesToRadians(rotate.Z));

            this.side = Vector3.TransformNormal(Vector3.UnitX, rotation);
            this.up = Vector3.TransformNormal(Vector3.UnitY, rotation);
            this.look = Vector3.TransformNormal(Vector3.UnitZ, rotation);

            Matrix rotation = Matrix.LookAtLH(this.translate, this.pivot.Look, this.pivot.Up);

            rotation.M13 = -rotation.M13;
            rotation.M31 = -rotation.M31;

            Matrix translate = Matrix.Translation(this.translate);

            this.world = (scale * rotation * translate);

 

Greets

Edited by HaloMasterChief

Share this post


Link to post
Share on other sites
Advertisement


and after it, to move it on the X axis, but the cube still move on the X axis and not in the angle, i had rotated it befor

 

What does this mean? "You want to move it on the x axis, but it still moves on the x axis." -> I don't understand.

 

How is this.translate calculated in your code?

 

Are you trying to move the object along the X-axis in world space or object space?

Share this post


Link to post
Share on other sites

Hello phil_t,

 

i meant, i want to move the cube on the rotated X axis, not on the origin X axis of the World.

 

As example what i mean:

rotatedaxis.jpg

 

And translate, rotate and scale are all Vector3's

 

Greets

Share this post


Link to post
Share on other sites

Then instead of scale -> rotate -> translate, use scale -> translate -> rotate:

this.world = (scale * translate * rotation);

Share this post


Link to post
Share on other sites

Hello phil_t,

 

work's almost perfect, the cube move in the right rotated axis directions, but the only problem is, that the cube rotated around the middle of the scene (0, 0, 0) and not at his self!?

 

I had try it with:

 

Matrix rotation = Matrix.LookAtLH(this.translate, this.pivot.Look, this.pivot.Up);

 

or

 

Matrix rotation = Matrix.LookAtLH(this.translate, this.translate+ this.pivot.Look, this.pivot.Up);

 

But this dosen't work!?

 

            Matrix scale = Matrix.Scaling(this.scale);

            Matrix rotation = Matrix.RotationYawPitchRoll(Mathematics.DegreesToRadians(rotate.Y),
                Mathematics.DegreesToRadians(rotate.X),
                Mathematics.DegreesToRadians(rotate.Z));

            this.side = Vector3.TransformNormal(Vector3.UnitX, rotation);
            this.up = Vector3.TransformNormal(Vector3.UnitY, rotation);
            this.look = Vector3.TransformNormal(Vector3.UnitZ, rotation);

            Matrix rotation = Matrix.LookAtLH(Vector3.Zero, this.pivot.Look, this.pivot.Up);

            rotation.M13 = -rotation.M13;
            rotation.M31 = -rotation.M31;

            Matrix translate = Matrix.Translation(this.translate);

            this.world = (scale * translate * rotation );

 

Greets

Share this post


Link to post
Share on other sites


but the only problem is, that the cube rotated around the middle of the scene (0, 0, 0) and not at his self!?

 

that's cause you translate then rotate.

 

to rotate around local y , then translate along local x:

 

start in model space at location 0,0,0 with rotation 0,0,0.  rotate around y.   now calculate the direction of travel. you want to move down local x, which is the direction yr + pi/2   IE the direction you're facing + 90 degrees.

 

to move:

new x = old x + sin(direction of travel) * speed

new z = old z + cos(direction of travel) * speed

 

 

 

 

Share this post


Link to post
Share on other sites

work's almost perfect, the cube move in the right rotated axis directions, but the only problem is, that the cube rotated around the middle of the scene (0, 0, 0) and not at his self!?

 

Well if you're just placing this cube and that's it, then the end result would be the same.

 

It sounds like you want to move the cube in the direction after it's already been positioned/rotated, right? Sorry, I didn't understand that at first.

 

Ok, so go back to what you had before, where world is:

world = scale * rotate * translate;

That's the correct way to assembly your world matrix (scale, rotate, translate).

 

Now at this point, if you want to move the object along it's local x-axis, each frame you'll want to take the x-axis direction vector and transform it by your rotation matrix:

Vector3 direction = Vector3.TransformNormal(Vector3.UnitX, rotation);

"direction" now points along the local x axis of the rotated cube. So offset your translation in that direction by the desired amount:

this.translate += direction * amountToMove;

and finally calculate your new world matrix:

Matrix translate = Matrix.Translation(this.translate);
this.world = (scale * rotation * translate);
Edited by phil_t

Share this post


Link to post
Share on other sites

Hello phil_t,

 

with the X axis i meant only a example, i want to create a Transform Tool that can place a 3D Object in a Scene where i want, so in the end, I want to move and rotate it on all 3 axis, not only on one axis, how is there the solution?

 

Greets

Share this post


Link to post
Share on other sites

Edit: This is my Code I have:

            Matrix scale = Matrix.Scaling(this.scale);

            Matrix rotate = Matrix.RotationYawPitchRoll(Mathematics.DegreesToRadians(this.rotate.Y),
                    Mathematics.DegreesToRadians(this.rotate.X),
                    Mathematics.DegreesToRadians(this.rotate.Z));

            this.localSide = Vector3.TransformNormal(Vector3.UnitX, rotate);
            this.localUp = Vector3.TransformNormal(Vector3.UnitY, rotate);
            this.localLook = Vector3.TransformNormal(Vector3.UnitZ, rotate);

            Matrix translate;

            if (transformType == TransformType.Local)
            {
                this.translate += this.localSide * this.translate.X;
                //this.translate += this.pivot.Up * this.translate.Y;
                //this.translate += this.pivot.Look * this.translate.Z;

                translate = Matrix.Translation(this.translate);
            }
            else
            {
                translate = Matrix.Translation(this.translate);
            }

            this.world = (scale * rotate * translate);
Edited by HaloMasterChief

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement