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"

Programming Guide Begginer question

Hello guys,

I'm totally new to UE4 and just started at the programming section. So in the first guide there's the following code:

 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;       //Scale our height by a factor of 20
     RunningTime += DeltaTime;
     SetActorLocation(NewLocation);
 };

Now what I can't really understand is DeltaHeight calculation. What is that value for and how can it affect the actor's position?. I understand everything else like what a Tick is, etc., just can't get how does the time calculation from line 2 affect the actor position to be used in the line under it.

Also, in which moment it decides to move up and to move down? When and how does that change occur?

Thanks.

Product Version: UE 4.18
Tags:
more ▼

asked Feb 04 '18 at 10:34 AM in C++ Programming

avatar image

Kyrax80
22 1 1 4

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

1 answer: sort voted first
more ▼

answered Feb 04 '18 at 11:35 AM

avatar image

Scienziatogm
349 44 40 50

avatar image Kyrax80 Feb 04 '18 at 11:49 AM

already did, but that doesn't answer my question of what DeltaHeight affects the position of the actor and how it decides to make the actor go up and down

avatar image Scienziatogm Feb 04 '18 at 12:12 PM

Ok, I'll try to explain that, Sin is a trigonometric function which goes from -1 to 1, depending on the value of the angle. 0 degrees corresponds to 0, while 90 degrees corresponds to 1, from 180 to 360 sin is negative and goes from 0 at 180 degrees to 1 at 270 degrees. The input values are not degrees, but differences of time. DeltaTime as you can see on docs is the time elapsed during last frame modified by the time dilation, while RunningTime is a counter which increments since the game started. So DeltaHeight can go from -1 to 1 because it's a difference between two Sin functions. When we calculated the DeltaHeight, we can increment the Z location of the actor, but since DeltaHeight goes from -1 to 1, we need to scale it to have our desired moving rate. Multiplying it by 20 sets our scale to 20 units, and multiplying the 20 scale to a negative number decreases the value of the new Z location instead of increase that.

avatar image Kyrax80 Feb 04 '18 at 12:37 PM

Okay thanks, some questions (because I really suck at maths)... Now how does it decide when to go up or down? I assume it goes down when Delta Height is negative and up when it's positive. But I tried changing the scale from 20 to 100 and it moves faster but also makes a longer distance until it changes to up or down. Why is that and how could it be changed?

avatar image Scienziatogm Feb 04 '18 at 12:47 PM

Ok, Sin is a periodic function, so when the angle is 360, the function is repeating. For example the Sin of 540 is the Sin of 180. So the up and down function is periodic like the Sin function. If you want to adjust the speed of the movement and the amount of movement, you should use a different system, for example I would use the FInterpTo function (https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Math/FMath/FInterpTo/index.html).

avatar image Kyrax80 Feb 04 '18 at 12:49 PM

Okay thank you, it's now clear :)

avatar image Kaneki Lim Dec 06 '18 at 06:06 PM

Hi, I hope u still here. What do u mean "from 180 to 360 sin is negative and goes from 0 at 180 degrees to 1 at 270 degrees"? Shouldn't it be -1 at 270 degrees or it's just a typo? Also I cannot understand this "So DeltaHeight can go from -1 to 1 because it's a difference between two Sin functions". I understand the sin is a trigonometric function which goes from -1 to 1, so the value of both FMath::Sin(RunningTime + DeltaTime) and also FMath::Sin(RunningTime) will within the range of -1 to 1 right? Then supposing that Sin(RunningTime + DeltaTime) is 1 and Sin(RunningTime) is -1, then we will get a 1-(-1) = 2 ?

(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