Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Trouble converting negative integers to positive values <360

Engaged ,
Sep 07, 2023 Sep 07, 2023

Hello, I'm looking to create an expression that, when/if a value becomes a negative integer, 360 is added to it so that it becomes a positive integer <360. For example, if a certain value is -17 then the displayed value is 343. I'm beginning with this expression. It works fine, except when the slider goes into negative numbers, which, the way things are designed, it has to do sometimes. 

 

val1 = thisComp.layer("Controls").effect("Heading Slider")("Slider")/388.89 val2= val1*100 Math.round(val2)

 

The slider is set to values 1-1400, hence the /388.89. Much appreciate the help! I'm still an expression newbie.

TOPICS
Expressions
421
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 07, 2023 Sep 07, 2023

Try this:

val1 = thisComp.layer("Controls").effect("Heading Slider")("Slider")/388.89;
val2= Math.round(val1*100);
val2 < 0 ? val2 + 360 : val2
Translate
Community Expert ,
Sep 07, 2023 Sep 07, 2023

Try this:

val1 = thisComp.layer("Controls").effect("Heading Slider")("Slider")/388.89;
val2= Math.round(val1*100);
val2 < 0 ? val2 + 360 : val2
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2023 Sep 07, 2023

This is how I would approach the problem. I'm assuming you are trying to display heading information, so you need an expression that turns the rotation value into numbers between zero and 359 to give you a compass heading.

 

You can use an Angle Control instead of a Slider and this expression. 

 

v = effect("Angle Control")("Angle").value.toFixed();
rNum = Math.floor(v)/360;
n = Math.floor(rNum);
aFix = 360 * n;
a = v - aFix;
Math.abs(a)

 

 You could also use a slider, but if you used an angle control, you could visualize the compass heading. 

 

The rNum sets up a counter that counts the number of 360º rotations, and then n makes the number of rotations a whole number. If you multiply the number of rotations times 360 and subtract that number from the angle value (v), you get a number that varies between plus and minus 360. The Math.abs(a) function always keeps the value positive. 

 

When you get a handle on the basic Math operators in Javascript, sketching the problem out with a pencil and paper will give you the formula always to display a number between zero and 360. No if/else statements are required.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 15, 2023 Sep 15, 2023
LATEST

Oh man, thanks so much, both of you! I inherited the project, and the previous person had it set up with a slider. I didn't even realize angle control was a possibility. This is so very helpful. Thank you guys!

 

I really know very little about code or expressions, and it's fun to reverse engineer what you wrote to see how it all works. Much appreciated!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines