Modding Help Update collisions.

Discussion in 'Starbound Modding' started by ImSoAwesome, Jul 15, 2017.

  1. ImSoAwesome

    ImSoAwesome Orbital Explorer

    Is there a way to update collisions on a animated object?
    Thanks.
     
  2. cpeosphoros

    cpeosphoros Orbital Explorer

    What do you mean by "update collisions"?
     
  3. bk3k

    bk3k Oxygen Tank

    Perhaps... you mean like doors do? If so, that's where you'd look. Doors will build a table of relative spaces obtained from object.spaces(), and build a table with a material(usually a metamaterial). But the doors allow you to define custom materialSpace setups if you want. Also you can manually define "spaces" within the "orientations" if you prefer as an alternative to "spaceScan" - which is an option that builds "spaces" automatically from the image, but that isn't always desirable. Maybe that's too much information about doors specifically.

    There are options for "collision" and "collisionSpaces" - perhaps that's what you're doing now? Those are pure JSON options that won't require a script, but can't adapt to changing states either. You'd need to do what doors do. Your animated object doesn't really NEED to store the spaces you'll be using technically. You could generate that in LUA if you really wanted, but it is more convenient to build them within your object.

    I'm missing a little info, like what sort of collision do you have in mind, what sort of script it currently has(if any). That in turn
    I'll give you this completely untested code. The odds of me having made a stupid error that I would catch by testing are only 76.2%. But you can't really expect me to make a test objects without even knowing what sort of object to build, do you?
    Code:
    local buildMaterialSpaces
    local changeState
    
    init = function()
      if storage.runOnce ~= true then
        storage.collisionStates = config.getParameter("collisionStates", { default = {} })
          --collisionStates would be lists of spaces under the animation state's nameSpace
        storage.materialSpaces = {}
        local stateMaterials = config.getParameter("stateMaterials". {})
    
      for state, _ in pairs(storage.collisionStates) do
        local spaces = storage.collisionStates[state]
        local mat = stateMaterials[state] or "metamaterial:objectsolid"
        storage.materialSpaces[state] = buildMaterialSpaces(spaces, mat)
      end
    
        local defaultState = config.getParameter("defaultState", "default")
        local currentState = config.getParameter("currentState", defaultState)
        local animationParts = config.getParameter("animationParts")
        local part
        for partName, __ in pairs(animationParts) do
          part = partName
          break
        end
        storage.mainPart = config.getParameter("mainObject", part)
        changeState(currentState, true)
        storage.runOnce = true
      else
        changeState(storage.currentState, false)
      end
    end
    
    
    buildMaterialSpaces = function(spaces, material)
      local retTable = {}
      for i, space in ipairs(spaces) do
        retTable[i] = {space, material}
      end
      return retTable
    end
    
    
    changeState = function(newState, arg)
      storage.currentState = newState
      object.setConfigParameter("currentState", newState)
      local collisionSpaces = storage.materialSpaces[newState]
      animator.setAnimationState(storage.mainPart, arg)
      object.setMaterialSpaces(storage.materialSpaces[newState])
    end
    
    Anywhere you see config.getParameter it is going to be looking for the first argument in your object. But don't bother with "currentState" as the script writes that and you probably have "animationParts" which it only checks when you don't define your default part(it uses the first).

    Example JSON would be

    Code:
      "collisionStates" : {
        "default" : [],
        "state1" : [
          [0, 2], [1, 2],
          [0, 1], [1, 1],
          [0, 0]. [1, 0]
        ],
        "state2" : [
          [0, 3], [1, 3],
          [0, 2], [1, 2],
          [0, 1], [1, 1],
          [0, 0]. [1, 0]
        ],
        "state3" : [
          [-1, 0], [0, 0]. [1, 0], [2, 0]
        ]
      },
    
      "stateMaterials" : {
        "default" : "metamaterial:empty",
        "state1" : "metamaterial:objectsolid",
        "state2" : "metamaterial:objectsolid",
        "state3" : "metamaterial:objectplatform"
      },
    
      "defaultState" : "state1",
      "mainObject" : "somePartName"
        //I have NO IDEA what this is for, so I don't even have a good example name. 
    Besides the fact I just sort of whipped this up without testing, I don't really understand what exactly you have in mind, or even what sort of object we're talking about. It would be better to be specialized a bit to match what you're doing. This is anything but specialized. Also unlike doors, this isn't currently supporting different collision types at different points. It could, but I don't really know what your needs are, so I didn't add make it that way.

    Also, if you don't define "mainObject", it will just use a part from "animationParts" which is hopefully the correct (only) part. If you are using more than one part, it is quite possible the script would need adjusted to consider the other parts and what they do. That's the importance of having all the information, and frankly your post was a bit lacking in details.

    edit:
    I really did make at least one stupid mistake, and have edited the code to fix it.

    edit 2:
    Noticed another stupid error where I want to treat a JSON object like an array. Corrected code, but that's not a great solution. You'd probably better defile "mainObject" if you do have more than one part.
     
    Last edited: Jul 15, 2017
    Cyel and cpeosphoros like this.
  4. ImSoAwesome

    ImSoAwesome Orbital Explorer

    No, something like "animationParts". I don't know how to control them in scripts. Like, one object is collision, and the other is static. I want the collision object be able to move around to specific positions while the static object is animating, and when it changes state, so does the collision. Any ideas?
    Thanks.
     
  5. bk3k

    bk3k Oxygen Tank

    Well... I suppose look at the bossdoor. I haven't done what you're talking about though.
     
  6. ImSoAwesome

    ImSoAwesome Orbital Explorer

    I actually had looked at "scanSpace", and it seems like thats what I want to change.
    Is there a way of constantly updating "scanSpace" through lua or json, so that on each frame, the collision updates?
    Thanks.

    UPDATE: "scanSpace" typo
    UPDATE 2: wait. i just incorrectly spelt it, thinking its incorrect.
     

Share This Page