how do I detect what element is touching

  • zBuilder
    27th Dec 2013 Member 0 Permalink

    how do I detect which element is touching a lua created element and how can I change the properties of the particle that colided?

  • bimmo_devices
    27th Dec 2013 Member 0 Permalink

    I have a very simple (possibly naive) solution to checking particles touching a particle: if each particle can retrieve it's coordinates tuple (x,y), and a particle can be found at a position given by a similar tuple, you could probe the surrounding pixels of each particle of your detector particle and find the particles around it. For example, for each detector particle at (x,y), find particles (x+1, y), (x-1, y), (x,y+1) and (x,y-1) and change their types. If I am not mistaken, this is a Von Newman Cellular Automaton neighbourhood. This seems like a real lag machine if you ask me; is there a better implementation?

  • jacob1
    27th Dec 2013 Developer 0 Permalink
    http://boxmein.x10.mx/tptelements/lua-reference.html#simulation

    See sim.partNeighbors (an alias of sim.partNeighbours, both are the same thing). You can iterate through a list of all nearby particles quickly. I have never seen anyone use it mostly because it's slightly more advanced and the function is undocumented ...

    I think it's something like:

    for i in sim.partNeighbors(x, y, 1 (maybe 2 or 3?)) do
    i is the particle id, use it however
    end

    The "type" argument will limit it to only elements of a certain type and is optional.
  • boxmein
    27th Dec 2013 Former Staff 0 Permalink
  • mniip
    27th Dec 2013 Developer 2 Permalink
    Edited 2 times by mniip. Last: 28th Dec 2013
  • zBuilder
    27th Dec 2013 Member 0 Permalink

    ok, found out this does sort of what I want; I'll just have to run it once per every direction; though that might get slow.

     

    if sim.partProperty(sim.partID(x, y+1),  sim.FIELD_TYPE)==elements.DEFAULT_PT_GLAS then
    sim.partProperty(sim.partID(x, y+1), sim.FIELD_TYPE , elements.DEFAULT_PT_METL)
    end

    Edited 2 times by zBuilder. Last: 28th Dec 2013
  • jacob1
    28th Dec 2013 Developer 0 Permalink
    @mniip (View Post)
    I guess I don't know the functions very well either, lol. The one you posted was the one I was thinking of.

    @boxmein (View Post)
    I tried that, but it didn't work because you used the "sim" shortcut instead of "simulation" which I was trying. http://boxmein.x10.mx/tptelements/lua-reference.html#simulation.partNeighbors didn't work.

    @zBuilder (View Post)
    if you can use sim.neighbors that would work a lot better and be less laggy.

    for i in sim.neighbors(x, y, 1, 1, tpt.el.glas.id) do
    tpt.set_property("type", i, tpt.el.metl.id)
    end


    Also I prefer the old api since more people know it and it's much cleaner ... but really doesn't matter. Also I tried this and it doesn't completely work for some reason ... weird. It changed the ones on the corners though.
    Edited once by jacob1. Last: 28th Dec 2013
  • zBuilder
    28th Dec 2013 Member 0 Permalink

    jacob1:

    @mniip (View Post)
    I guess I don't know the functions very well either, lol. The one you posted was the one I was thinking of.

    @boxmein (View Post)
    I tried that, but it didn't work because you used the "sim" shortcut instead of "simulation" which I was trying. http://boxmein.x10.mx/tptelements/lua-reference.html#simulation.partNeighbors didn't work.

    @zBuilder (View Post)
    if you can use sim.neighbors that would work a lot better and be less laggy.

    for i in sim.neighbors(x, y, 1, 1, tpt.el.glas.id) do
    tpt.set_property("type", i, tpt.el.metl.id)
    end


    Also I prefer the old api since more people know it and it's much cleaner ... but really doesn't matter. Also I tried this and it doesn't completely work for some reason ... weird. It changed the ones on the corners though.

     

    when I use partneigbours it fails because it is "calling a table value", whatever that means. though your arguments look different then the last set I saw so I'll give it a shot! :)

    edit: sorry, that one throws an error instead as well :(

    Edited once by zBuilder. Last: 28th Dec 2013
  • jacob1
    29th Dec 2013 Developer 0 Permalink
    @zBuilder (View Post)
    I was wrong, it's just sim.neighbors, not sim.partNeighbors. One returns an iterator, the other just a table.