Rolling a Cube

Hello,

I am trying to roll a cube in Unreal but I am not able to do it. For visualization purpose I am creating something similar to this : https://www.youtube.com/watch?v=3nFpUDAzysY

For this I attached a URotatingMovementComponent and tried to offset the pivot in order to rotate it.
I have attached the code that does it. But it does not work.

Rotating the cube when “Up” Key is pressed

The timer functions which stops the rotation after 1 second
TimerFunction

The component that I have attached that makes the rotation possible

Currently it looks like this
The red spheres are the pivot point

ezgif.com-video-to-gif-converted

Hi BlackDash69,

output

You’re super close.

The issue is that you’re setting the pivot in world space, but the URotatingMovementComponent PivotTranslation works in local space.

All you need to do is:

  1. To your MovementClass.h file, add this include:
    #include "Kismet/KismetMathLibrary.h"

  2. To Your RotateUp() function, switch the first two lines to be:

FVector worldLocation = FVector(50.f, 0, -50.f) + GetActorLocation();
RotationDoer->PivotTranslation = UKismetMathLibrary::InverseTransformLocation(GetTransform(), worldLocation);

What this code is doing is taking the world space location - the actor’s location plus the offset you want for the pivot - and using InverseTransformLocation to convert it into the cube’s local space that the URotatingMovementComponent is expecting.

If world/local space is new to you, this is a pretty good explainer video.


Hope this helps, and good luck on your project!

3 Likes

Thanks a lot I will try it out

Just a small question
How are you stopping the cube from rolling?
Are you using a timer?
Or is it something else?