Download

Rotate a mesh at a certain point

After many many hours of researching, I have learned… that I suck at matrices.

I’m trying to achieve this:
29f113505a90d0453b13140090dce4bc0e377ea1.png

Note that I don’t just want to do 90-degree rotations, it’s just paint wouldn’t let me rotate. :smiley:

Basically I have a mesh, and a vector symbolizing my magical point. I want to rotate the mesh as if the origin of the mesh is that point.
I have been searching high and low, found this, but even that doesn’t work.
And yes, do to how animations are done in my game, I can’t just attach the mesh to a point or something. :frowning:
Anyone know how to do this? I’m realllyyy stuck! XD

Thanks!!
-Josh Steinhauer

You could use a USceneComponent as your root component and attach the mesh to it.
Use AActor::AddActorLocalRotation or AActor::SetActorLocalRotation to rotate your mesh about the USceneComponent.
The only tricky part is setting the initial offset from your mesh to your root. I guess you could derive a blueprint just to see what offset values you need. Then go in code and set the initial position of the mesh.

Edit: It would be easier if you could move the pivot for your static mesh, but I’m not sure how to do that. If you figure out how to do that then just make the mesh your root component, the rotations would already be about the pivot.

Sadly this is not an option due to animations doing fancy stuff… It’s kinda hard to explain, but basically I can’t have a mesh attached, I have to do purely rotation. But thanks!

Does anyone know how to do the original question? It’s very mathy :confused:

What you have to do is translate so that Origin and Point are equal first, then rotate (around the origin), then translate back like so:
4cc4b7e9c68654b7e3d37e4157618650016dba7a.png

Yep, this is pretty much correct.

Remember, every rotation is a rotation around the origin. If you do a translation followed by a rotation, you’re going to be rotating the translated points around the origin. Sometimes, you want this (such as with planets orbiting the sun).

You can make sure that the static mesh you’re using is using an orientation which makes sense in a rotational sense. Make sure that the pivot point is centered on your mesh…

Hmmmm

Here’s the thing though. I’m looking to move a mesh component this way… but how does one move the mesh without moving the origin of the mesh thingy?

If this is not possible, the only possible alternative will probably be to compute the new position using fancy maths. :’’’(

Anyone know how to do this? :slight_smile: :smiley:

Uhm… you aren’t understanding what we mean by “origin”.

A mesh is just a list of vertices, denoted by an X, Y, and Z value. (Sometimes, a custom vertex structure will include other things, like normals, UV coordinates, etc).

Let’s say you have a very simple quad in 2D space, given by the following coordinates:

0,0
1,0
1,1
0,1

The “origin” is at (0,0). It always is. The center of the quad is at (0.5,0.5).

If you apply a rotation to this quad, it is going to pivot around the lower left corner located at (0,0). This is not a rotation around the center of the object.

If we want to rotate around the center of the quad, at (0.5, 0.5), then you first have to do a translation by (-0.5,-0.5), then apply a rotation.

Notice that we put the center of the quad at the origin.

Now, you may be frustrated and wondering, “Why does it work this way?”

Because mathematics. The same mathematical operation is applied to every single point on the mesh. If you do a 90 degree counter clockwise rotation on a unit vector of (1,0), the pivot point is always based off of the origin at (0,0) and the result will be (0,1) because… we’re doing (cos(90), sin(90))

Matrices are wonderful little things people invented which really help us compact all this stuff. A matrix will store all of the accrued rotations, translations, and scales you apply to a vertex, and the accrued values keep the order of your operations correct. This is mostly done on way back on the engine side, particularly with API’s like DirectX11 and OpenGL, but you can get a matrix “transform” within blueprints and apply it to an object.

Anyways, long story short, the origin is always at 0,0,0. It never moves. You always rotate around the origin. Usually we perform a translation on an object so that its on the origin, perform a rotation, then apply the inverse translation. It doesn’t matter if the object is a single point, a static mesh, a mesh component, etc.

Ok, I get it now, I think! Thanks!

However I do not understand how to do this in the context of the mesh component… could you perhaps provide a code example of how to do this?

Sorry!

I’ve dealt with this issue in a 2d environment. You want to do a “affine transformation” (Affine transformation - Wikipedia).
Basically, the solution is to skip all the hard math. Do what UnrealEverything said - move to the origin, rotate, move back. Saves you a lot of headaches, even if doesn’t sound like a “proper” way :slight_smile:

Could you perhaps write a few lines of code? I’ve already tried what I think your trying to say, and no dice. :confused: