Double pendulum as modulation in Kontakt KSP

Jamielufc11
Jamielufc11 Member Posts: 3 Newcomer
edited October 2024 in Scripting Workshop

Hi, I'm trying to use a double pendulum to modulate parameters within KSP

I've given it a go but my code is not giving me results. I was hoping to see some sort of wavy modulation

See code attached, any help would be gratefully appreciated, thanks in advance.

on init

declare ui_knob $Volume (0,1000000,1000000)



declare ~L1
~L1 := 100.00
declare ~L2
~L2 := 100.00

declare ~M1
~M1 := 40.00
declare ~M2
~M2 := 40.00

declare ~A1
~A1 := 90.00
declare ~A2
~A2 := 0.00

declare ~A1V

declare ~A2V

declare ~A1A

declare ~A2A

~A1V := ~A1A
~A2V := ~A2A
~A1 := ~A1V
~A2 := ~A2V

declare ~G
~G := 9.81

declare ~num1
declare ~num2
declare ~num3
declare ~num4
declare ~den

declare $test_int
end on

function pendulum

~A1V := ~A1A

~A2V := ~A2A

~A1 := ~A1V

~A2 := ~A2V

~num1 := -~G * (2.0 * ~M1 + ~M2) * sin(~A1)
~num2 := -~M2 * ~G * sin(~A1 - 2.0 * ~A2)
~num3 := -2.0 * sin(~A1 - ~A2) * ~M2
~num4 := ~A2V * ~A2V * ~L2 + ~A1V * ~A1V * ~L1 * cos(~A1 - ~A2)
~den := ~L1 * (2.0 * ~M1 + ~M2 - ~M2 * cos(2.0 * ~A1 - 2.0 * ~A2))
~A1A := (~num1 + ~num2 + ~num3 * ~num4) / ~den

~num1 := 2.0 * sin(~A1 - ~A2)
~num2 := ~A1V * ~A1V * ~L1 * (~M1 + ~M2)
~num3 := ~G * (~M1 + ~M2) * cos(~A1)
~num4 := ~A2V * ~A2V * ~L2 * ~M2 * cos(~A1 - ~A2)
~den := ~L2 * (2.0 * ~M1 + ~M2 - ~M2 * cos(2.0 * ~A1 - 2.0 * ~A2))
~A2A := (~num1 * (~num2 + ~num3 + ~num4)) / ~den

$test_int := real_to_int(~A2A)
message($test_int)

{~A1V := abs(~A1V)}

end function

on ui_control ($Volume)
set_engine_par($ENGINE_PAR_VOLUME,$test_int,-1,-1,-1)

end on

Comments

  • theodorech
    theodorech Member Posts: 72 Member
    edited June 2024

    Well written code! Here are a few tips:

    1. you need to set a listener in order to trigger somehow the movement
    2. you need to call the pendulum function within the LCB
    3. you should increment (time steps) the values which take care of the pendulum motion (velocity and angle) so it happens automatically (ie ~A1V := ~A1V + ~A1A * 0.1 and ~A1 := ~A1 + ~A1V * 0.1)
    4. AFAIK trigonometric functions should utilize radians and not degrees. It'd be better to use NI_MATH_PI and convert to radians: declare ~A1 := 90.0 * NI_MATH_PI / 180.0

  • Jamielufc11
    Jamielufc11 Member Posts: 3 Newcomer

    Thats great thank you! Ill give all that a shot.

  • Jamielufc11
    Jamielufc11 Member Posts: 3 Newcomer

    Hi theodorech

    Thanks for your help

    I Have given what you suggested a go and Ive made progress but I still not able to get the parameter to modulate. It just returns the value to 0.

    I think it may be to do with the time steps you suggested, I'm not fully understanding what to do.

    Could you please provide further help.

    Thank you again.

    See my update code bellow:

    on init

    make_perfview
    set_script_title("Pendulum")
    set_ui_height_px(125)

    declare ui_button $Button
    $Button := 0

    set_listener($NI_SIGNAL_TIMER_MS,100000)

    declare ~L1
    ~L1 := 100.00
    declare ~L2
    ~L2 := 100.00

    declare ~M1
    ~M1 := 40.00
    declare ~M2
    ~M2 := 40.00

    declare ~A1
    ~A1 := 90.0 * ~NI_MATH_PI / 180.0
    declare ~A2
    ~A2 := 90.0 * ~NI_MATH_PI / 180.0

    declare ~A1V

    declare ~A2V

    declare ~A1A

    declare ~A2A

    ~A1V := ~A1A
    ~A2V := ~A2A
    ~A1 := ~A1V
    ~A2 := ~A2V

    declare ~G
    ~G := 9.81

    declare ~num1
    declare ~num2
    declare ~num3
    declare ~num4
    declare ~den

    declare $test_int

    end on

    function func_pendulum

    ~A1V := ~A1A

    ~A2V := ~A2A

    ~A1 := ~A1V

    ~A2 := ~A2V

    ~num1 := -~G * (2.0 * ~M1 + ~M2) * sin(~A1)
    ~num2 := -~M2 * ~G * sin(~A1 - 2.0 * ~A2)
    ~num3 := -2.0 * sin(~A1 - ~A2) * ~M2
    ~num4 := ~A2V * ~A2V * ~L2 + ~A1V * ~A1V * ~L1 * cos(~A1 - ~A2)
    ~den := ~L1 * (2.0 * ~M1 + ~M2 - ~M2 * cos(2.0 * ~A1 - 2.0 * ~A2))
    ~A1A := (~num1 + ~num2 + ~num3 * ~num4) / ~den

    ~num1 := 2.0 * sin(~A1 - ~A2)
    ~num2 := ~A1V * ~A1V * ~L1 * (~M1 + ~M2)
    ~num3 := ~G * (~M1 + ~M2) * cos(~A1)
    ~num4 := ~A2V * ~A2V * ~L2 * ~M2 * cos(~A1 - ~A2)
    ~den := ~L2 * (2.0 * ~M1 + ~M2 - ~M2 * cos(2.0 * ~A1 - 2.0 * ~A2))
    ~A2A := (~num1 * (~num2 + ~num3 + ~num4)) / ~den

    ~A1V := ~A1V + ~A1A * 0.1
    ~A1 := ~A1 + ~A1V * 0.1

    $test_int := real_to_int(~A2A)


    {~A1V := abs(~A1V)}

    end function

    on listener
      
    

    if ($NI_SIGNAL_TYPE = $NI_SIGNAL_TIMER_MS)

    call func_pendulum

    end if

    end on

    on ui_control ($Button)
    if ($Button = 1)

    set_engine_par($ENGINE_PAR_VOLUME,$test_int,-1,-1,-1)


    end if


    end on

This discussion has been closed.
Back To Top