#i have some code I wrote 1 year ago and
1 messages · Page 1 of 1 (latest)
this gets called when an object is dropped:
function SmashUp.makeSureMovementStops(card, conditionFxn)
if conditionFxn == nil then
conditionFxn = function()
return card.isDestroyed() or (card.resting and (not card.held_by_color))
end
end
local newPos
local newRot
local justVibrating
local oldPos = card.getPosition()
local oldRot = card.getRotation()
local timer = os.clock()
local waitCond1
local waitCond2
local mainWaitFunc
waitCond1 = Wait.condition(function()
if card == nil then return end
timer = os.clock()
waitCond2 = Wait.condition(function()
if card == nil then return end
card.setLock(true)
card.setVelocity({0, 0, 0})
card.setAngularVelocity({0, 0, 0})
end,
--wait2 condition
function()
if card == nil then return true end
newPos = card.getPosition()
newRot = card.getRotation()
justVibrating = SmashUp.checkIfStoppedButVibrating(newPos, oldPos, newRot, oldRot, 0.01, true)
oldPos = newPos
oldRot = newRot
return (not card.isDestroyed()) and (not card.resting) and (not card.held_by_color) and (os.clock() - timer > 0.5) and (SmashUp.nearlyStoppedCheck(card, 0.03) or justVibrating or (os.clock() - timer > 3))
end)
end,
--wait1 condition
|| (not card.isDestroyed()) and (not card.resting) and (not card.held_by_color) and ((SmashUp.nearlyStoppedCheck(card, 0.03) and os.clock() - timer > 1) or (os.clock() - timer > 3)))
mainWaitFunc = Wait.condition(function()
if card == nil then return end
if waitCond1 then Wait.stop(waitCond1) end
if waitCond2 then Wait.stop(waitCond2) end
card.setLock(false)
card.interactable = true
end, conditionFxn)
end
function SmashUp.checkIfStoppedButVibrating(newPos, oldPos, newRot, oldRot, mvmtAllow, checkRot)
if checkRot == nil then checkRot = false end
local posDifX = newPos.x - oldPos.x
local posDifY = newPos.y - oldPos.y
local posDifZ = newPos.z - oldPos.z
local doneRotating = not checkRot
if checkRot then
local rotDifX = newRot.x - oldRot.x
local rotDifY = newRot.y - oldRot.y
local rotDifZ = newRot.z - oldRot.z
doneRotating = (math.abs(rotDifX) < mvmtAllow) and (math.abs(rotDifY) < mvmtAllow) and (math.abs(rotDifZ) < mvmtAllow)
end
local doneMoving = (math.abs(posDifX) < mvmtAllow) and (math.abs(posDifY) < mvmtAllow) and (math.abs(posDifZ) < mvmtAllow)
return doneMoving and doneRotating
end
function SmashUp.nearlyStoppedCheck(obj, speedLimit)
if speedLimit == nil then speedLimit = 0.1 end
for vect, speed in pairs(obj.getVelocity()) do
if math.abs(Common.round(speed, 3)) > speedLimit then
return false
end
end
return true
end
if you are here, thank you! my question is about waitCond2 in the top snippet
the ultimate goal of this code here is to find those objects (cards, tokens, etc) that never come to a complete "rest" because of some TTS collision issues that might occur causing objects to "vibrate" or "wiggle". If those objects are identified, the code forces them to come to a rest, and the rest of the code that depends on the objects being at a rest can proceed.
the main function makeSureMovementStops gets called when an object is dropped.
the function of waitCond1 triggers when the object is:
-not destroyed
-not resting
-not being held by a player (not picked back up by a player before the object has come to a rest)
AND
-is almost resting (checked by the nearlyStoppedCheck fxn) and it has been more than 1 second since the card was dropped
OR
-it has been more than 3 seconds since the card was dropped (and it still isn't resting)
but then waitCond2 includes conditions that the object is "nearlyStopped" OR "justVibrating"
for waitCond1, the combination of not card.resting and nearlyStoppedCheck means that the object is moving but very slowly.
so, let's say waitCond1 triggers so now waitCond2 starts waiting; waiting for essentially the same things as waitCond1 (including the nearlyStoppedCheck) with the exception of the 0.5 second timer and the justVibrating check.
if you look at the justVibrating and nearlyStopped, they are very similar, except that justVibrating also checks for angular "wiggling". So, what would happen if I remove the "nearlyStopped" check from waitCond2 and just kept the justVibrating? what might be some benefits to keeping it?
Only thing I see is, you could distinguish if the obj were sitting there slowly rotating, and do something different in that case. Otherwise you may as well just use justVibrating().