Rotate component in it's local space toward a point in world

Hi.
I apologize for the hackneyed question, but I couldn’t find the answer on the forum.
I’m making a tank.
Simple hierarchy: Body → tower → cannon.
A little stuck on the aiming system.

I’m doing a linetrace from the screen to the world. A hit position appears in the world. I need the tower to rotate to this point around its local Z axis, and the gun along Y;

A static turret is very easy to implement, since I can use world coordinates, but here I need to rotate this components relative to the body of the tank, since when moving on inclined surfaces, the rotation is incorrect if I use world coordinates.

If possible, an example in C++ would be very helpful, since I’m not very good at blueprints…

I’m assuming you would use the same setup in CPP as BP, which would be ‘look at rotation’. But for this to work, your gun needs to point along the local X axis.

Yes, I see that “FindLookAtRotation” simply subtracts the “start” and “target” vectors to find the direction.
What should I do with this direction then?)

That’s the direction you point the turret, but only Z

( sorry for the BP, I don’t ‘do’ CPP :slight_smile: )

But like I say, this node will point the forward vector of the component at the location, so you need the pivot to be setup correctly.

In C++ it looks something like this, but unfortunately it doesn’t work. The turret rotates to the desired point only if the tank itself is pointed towards the world X axis. When the tank rotates 90 degrees, the turret also looks to the target direction in the direction, but with + or - 90 degrees and so on…(

Notice a rotator is returned from the node. Does it really just subtract one vector from the other?

( I don’t have the source )

Yes, indeed… It returns “FRotationMatrix”) I’ll see what i can do with this matrix)

1 Like

Gimme a minute… :slight_smile:

So I have a target actor in the world, and this code

lookat

Yes, as I already said, what you show is very easy to do, since these are two actors, and they are both located in world coordinates. The only thing is that I need to rotate not a separate actor, but a component that is lower in the hierarchy of the main actor itself… The hierarchy is like this:

Tank
|
Tower - Rotate along local Z towards the target
|
Cannon - Rotate along local Y towards the target

The tank also rotates when it moves…)
This method does not work in the local coordinates of the component…(

I am rotating a component… I am pointing the component at a world location.

tank

But an actor is not a component…
You can clearly see SetActorRotation() in your screenshot)
Or am I missing something?)

Will the result be the same even if you rotate around various axes the tank itself?)

By the way. Please tell me how you make such small videos? What soft you are using for this?) It would probably be very cool and simple if I could do the same to show what exactly I’m doing)

1 Like

You’re right, my CPP example only contains actors, sorry. I’ll see if I can find something more relevant… But it will be ‘set relative rotation’ on the component.

I use

1 Like

Now I’ll try to record a video and show what exactly I mean :slight_smile:

1 Like

Sorry, I can’t load the video… The Internet connection is interrupted all the time.

Alas, “SetRelativeRotation” preserves rotation in other axes but sets the Z rotation in the target direction incorrectly, and “SetWorldRotation” rotates in the target direction correctly, but does not prevent rotation in other axes…(

Rotate component in it’s local space toward a point in world

See if this is what you’re looking for:

You must transform direction to / from local space. Ignore the interps and clamps, ofc. It’s a bit dated, today I’d at least slerp directions.

2 Likes

Well, this will take some time)
I’ll be sure to answer as soon as I get something done)
Thank you in advance. From local to world and back looks very logical in this situation. I hope it will work :slight_smile:

Perfectly works)
Just awesome! This is the very first time that someone was really able to help me)
Eternally grateful to you)
The code looks like this) I hope this will help someone else too)

1 Like

Neater than BP spaghetti, for sure!

1 Like