x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How can I change the period of oscillation of an object?

Hi there, thanks for looking at my question!

I have been learning C++ for a month or so to try get a handle on it for general use but also to have a better understanding when I started learning Unreal.

I am in the first C++ unreal tutorial where toy make a floating Actor that bobs up and down and I understood it, but at the end it asks you to add 2 more axis of movement at different rates.

I thought I had the answer but I see now that I was just increasing the value of Sin by multiplying the delta time by a different amount for each direction, which increases the length the object moves, but it takes the same time in all directions, resulting in a vector motion. What I want is for it to take different amounts of time to complete a full cycle in each axis so that it looks like it is bobbing about in all directions.

 // Called every frame
 
 void AFloatingActor::Tick( float DeltaTime )
 {
     Super::Tick( DeltaTime );
 
     FVector NewLocation = GetActorLocation();
     float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
     NewLocation.Z += DeltaHeight * 20.0f;
     float DeltaDepth = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
     NewLocation.X += DeltaHeight * 10.0f;
     float DeltaWidth = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
     NewLocation.Y += DeltaHeight * 10.0f;
     RunningTime += DeltaTime;
     SetActorLocation(NewLocation);


So for example I want it to move up and down once fully every 3 seconds, back to front every 2.5 seconds and side to side every 4 seconds.

I hope that is sort of clear. Can anyone help?

Product Version: UE 4.12
Tags:
more ▼

asked Jul 12 '16 at 11:36 AM in C++ Programming

avatar image

Zarakk
1 1 2 3

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

It very easy if you think about it, you just scale the flow of time so for example:

 (FMath::Sin((RunningTime + DeltaTime)*2) - FMath::Sin(RunningTime*2))

Will move twice as fast :)

If you want to accurate cycle per sec you need also multiply frequency by PI*2 (*2 make it full cycle, look link)

https://betterexplained.com/wp-content/uploads/sine/sine-plot.gif

 (FMath::Sin((RunningTime + DeltaTime)*(TimeScale*(PI*2))) - FMath::Sin(RunningTime*(TimeScale*(PI*2)))

if you want sec per cycle you invert the scale (1/x)

 (FMath::Sin((RunningTime + DeltaTime)*((1/TimeScale)*(PI*2))) - FMath::Sin(RunningTime*((1/TimeScale)*(PI*2)))

But note as you use adding to position, change of frequency and aspecially amplitude can cause displacement of the actor, if you want it to be consistent use setting insted of adding, i think changing frequency sould not be a issue if you scale time just at delta time

more ▼

answered Jul 12 '16 at 04:28 PM

avatar image

Shadowriver
35.2k 921 166 1098

avatar image Zarakk Jul 12 '16 at 04:56 PM

Thank you so much! I knew it was something simple but my brain wasn't working. I was putting the modifier in the wrong place.

Thanks!

avatar image kenmorechalfant Feb 02 '18 at 04:25 AM

Sorry to resurrect this old post but I'm confused why the whole sin(RunningTime + DeltaTime) - sin(RunningTime) bit is necessary. If you just want something to oscillate can't you just do sin(RunningTime)?

avatar image Shadowriver Feb 02 '18 at 10:48 PM

You right he could just do RunningTime += DeltaTime; before the computations, he would have similar result, but maybe he wanted 0 value on first tick?

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question