Domanda

function update(event) -- check if the game is over
  if gameOver == true then
    return
  end
  fWait = (wTime > 0)
  if fWait then
    wTime = wTime – 0.01
    if wTime < 0 then
      wTime = 0
    end
    return
  end
  if hasAccel==true then
    local gx, gy = getAcceleration()
    fx = gx * filter + fx * (1-filter)
    fy = gy * filter + fy * (1-filter)
    updatePlayer(fx, fy)
  end
end
stage:addEventListener(Event.ENTER FRAME, update)

In above code, what is fx and fy? why is gx multiplied with a constant and fx with (1-constant)?

È stato utile?

Soluzione

It is a little difficult to say without more info but look at this:

local gx, gy = getAcceleration()
fx = gx * filter + fx * (1-filter)

This gets the acceleration of device. Based on fact that filter has no physical units, fx is an acceleration too (although sometimes developers take shortcuts and drop units by making assumptions about time periods and such but I don't think it is the case here).

  • If filter is 0, new fx is just previous fx, everytime this update(event) is called (there is no info about how fx is computed, it may get updated in other parts of program too).
  • If filter is 1, fx is gx (device accel) only, without any consideration of previous fx value.

So it looks like filter is a relative weight to give to device accel: when weight is zero, there is no device accel used so fx is just whatever has been computed so far; when weight is 1 then fx is entirely due to device accel so it is just gx. For all other intermediate values, updated fx is a proportional mixture of gravity and the previous value of fx: filter=0.25 means use 1/4 the effect of device accel, and the rest (1-1/4 or 3/4) the fx computed thus far.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top