Update position from within update loop

  • MarkHuss
    20th May 2019 Member 0 Permalink

    I am trying to make TPT render particles position from different formulas. For example, make particles follow a sine function, or a circle, to eventually use fractal functions.

     

    I tried to loop x and y through a formula in the update function, but I found the following problem: whatever loop is in the update function, only the very end result gets rendered. 

     

    I tried nesting functions in whatever logic I could think of (having the update function within the loop function, having two udate functions, etc...), none of them working.

     

    I also tried using the event.register(tick), but I end up with the same problem: only the end result of the update function gets rendered.

     

    Any ideas, suggestions?

     

    Marc

    Edited 3 times by MarkHuss. Last: 20th May 2019
  • jacob1
    20th May 2019 Developer 0 Permalink
    Can you post your code? It might help us help you solve the problem.

    Are you trying to make a function move position every frame to a new position in the sine wave? If so, you can't do a for loop like you are doing, because that loop won't ever return control to powder toy until it finishes, when it's at the end of the sine wave. You'll need to store in the particle properties what position in the wave it is at. You can set properties like this: sim.partProperty(0, "tmp", 123). That sets particle 0's tmp to 123. There's enough properties that you can store x/y and angle in properties to control how far it is in the sine.

    Or alternatively are you trying to have one particle render a "sine" graphic? If so, you should use a graphics function. But it won't be normal at all, you'll have to do something special and use some graphics functions to draw to the screen. (hmm, just noticed there is no drawPixel, odd, but you can use drawRect to draw a pixel too)
  • MarkHuss
    20th May 2019 Member 0 Permalink

    Hi jacob,

     

    I tried the partProperty.tmp route, but I can ony paint on existing particles. I really need it to be a single particle that moves along a procedural path. I am now testing with simple sine or circle formulas.

     

    I have not explored the graphics function route yet.

     

    However I managed to grossly approximate what I want by using arrays to "port" position data across update function loops.

     

    --edit: corrected the code. Now works.

    For now, it is really cluncky and only draws only half a circle, but it is a proof of concept.

     

    I would be happy if you would take a look.

     

     

    --Initialize environnment variables
    incr_array={}
    increment=0.25
    r=80 --radius
    --
    for a =-r, r, increment do
    incr_array[#incr_array+1]=a
    end
    --
    b=1 --Array pointer to be used in update loop
    x_container={} --Array to store x values
    y_container={} --Array to store y values
    c=1 -- Array pointer to manage stored x and y values
    d=0 -- Second hemisphere trigger
    bb= (2 * r) / increment -- incr_array backwards pointer for second hemisphere
    --
    --Main update loop
    local function M_OS_Update(i, x, y, ss, nt)
    r=80
    x_container[c]=x
    y_container[c]=y
    --
    if bb==0 then -- Switch from second hemisphere to the first
    d=0 bb=(2 * r) / increment
    --print("if bb==0", "r=", r, increment, "bb=", bb, "incr_array[bb]=", incr_array[bb], "b=", b)
    end
    if incr_array[b]== nil then --Second hemisphere trigger
    b=1 d=1
    --print("if incr_array[b]== nil", "r=", r, increment, "bb=", bb, "incr_array[bb]=", incr_array[bb], "b=", b)
    end
    --
    if d==0 then
    --print("if d==0", "r=", r, increment, "bb=", bb, "incr_array[b]=", incr_array[b], "b=", b)
    xpos = x_container[1] + incr_array[b]
    ypos = y_container[1] + math.sqrt(math.abs(r^2 - (xpos-x_container[1])^2))
    sim.partProperty(i, sim.FIELD_X, xpos)
    sim.partProperty(i, sim.FIELD_Y, ypos)
    b=b+1
    end
    if d==1 then
    --print("if d==1", "r=", r, increment, "bb=", bb, "incr_array[bb]=", incr_array[bb], "b=", b)
    xpos = x_container[1] + incr_array[bb]
    ypos = y_container[1] - math.sqrt(math.abs(r^2 - (xpos-x_container[1])^2))
    sim.partProperty(i, sim.FIELD_X, xpos)
    sim.partProperty(i, sim.FIELD_Y, ypos)
    bb=bb-1
    end
    c=c+1
    end
    elements.property(elem.MARKHUSS_PT_M_OS, "Update", M_OS_Update)

    Edited once by MarkHuss. Last: 22nd May 2019