#using update to track pointer

1 messages · Page 1 of 1 (latest)

tired shuttle
#

Interesting! I have been working on this very thing!

#
--  Hugh Everett
-- Basically emulates pickUp and Drop actions under player control.
--  Here I use scriptingButton 1 as my trigger.  press once to pick up, press again to set down.
--
--    fancy ideas:  increment lift while button is down.  "Manual lift"
--                  stronger feedback to player so s/he can tell whether the object has been grabbed
--                  configure variable rotation values
--                  Use any handy event for the trigger.  right click menu, external button, attached button
--                        all are fair game
--                  Improve stability by locking or turning off physics

following = nil
followRot = nil
offset = Vector(0, 0, 0)

function follow(who)
    followRot = self.getRotation()
    followRot.y = math.floor(followRot.y / 36 + 0.5) * 36
    following = who
    offset = self.getPosition() - Player[who].getPointerPosition()
end

function onFixedUpdate()
    if following ~= nil then
        self.setPosition(Player[following].getPointerPosition() + offset)
        self.setRotation(followRot)
    end
end

function onScriptingButtonDown(which, who) -- cannot use onPickUp or onDrop
    if which == 1 then
        local obj = Player[who].getHoverObject()
        if obj == self then
            if following == nil then
                follow(who)
            else
                following = nil
                -- Wait.stop(waitId)
            end
        end
    end
end```
#

I used onFixedUpdate() because update results in too much instability. You are essentially working against Unity here. I think I can cure that by locking the object while it is "held". Further testing needed. Strange that @undone dirge's code and mine are so similar despite the face that neither of us knew the other was coding it!

#

My code also intends to enforce a 36 degree rotational increment to support my penrose tile mod.

undone dirge
#

haha this is just demo trash I wrote in seconds 😛

#

this trick is somewhat old though, used it for custom cursors years ago

tired shuttle
#

Figured.

tired shuttle
#
function onLoad()
    following = nil
    followRot = nil
    offset = Vector(0, 0, 0)
    self.UI.setXmlTable({
        {   tag = "Button",
            attributes = { width = 60, height = 60, colors = "#000000|#888888|#ffffff|#00000000",
                position = "0 0 -15", text = "O", textColor = "#aabbcc", onClick = "follow"}
        }
    })
end

function follow(player, value, id)
    -- print(logString(player), " ", logString(value))
    local who = player.color
    if following then
        following = nil
        self.use_gravity = true
    else
        followRot = self.getRotation()
        followRot.y = math.floor(followRot.y / 36 + 0.5) * 36 -- constrain rotation to a multiple of 36 degrees
        self.use_gravity = false
        following = who
        offset = self.getPosition() - Player[who].getPointerPosition() + Vector(0, 1, 0) -- fixed autoRaise
    end
end

function onFixedUpdate()
    if following ~= nil then
        self.setPosition(Player[following].getPointerPosition() + offset)
        self.setRotation(followRot)
        setNotes(logString(self.getPosition()))
    end
end
#

This is a little cleaner, uses left click to toggle following. It can be a little tricky to get the button with this design if all else fails to to top down view.

#

I use onFixedUpdate because even with use_gravity turned off, the physics is too loose and the piece tends to wander too far.

#

In passing I also have observed that neither update really acts on the object on every pass. If gravity is active on the piece, it still sinks if you click it but don't move it much. The code says move it into position every pass, but it only actually does it if there is some activity.

#

You can demo this by commenting out the use_gravity statements.

undone dirge
#

using fixedupdate is fine as it's tied to the physics update

#

it will tick at the same speed as the rest of physics interactions, where as update just runs every frame (but this of course varies based on your framerate)

tired shuttle
#

You know, in an amusing way, working with TTS physics is a little like working with quantum physics... You're never really sure what will happen when you get down to the nitty gritty!

undone dirge
#

Haha not at all but you could emulate it by randomizing physics calcs with non deterministic randoms