Thread Tools Display Modes
12-30-23, 05:10 PM   #1
x2infinity
A Defias Bandit
Join Date: Apr 2013
Posts: 2
Texture position lags behind cursor

Hey,

I've been using the StarCursor addon but I noticed the texture lags behind the cursor when moving the mouse. I'd rather it fix to the cursor position. Here is the code for the addon:

Lua Code:
  1. local frame = CreateFrame("Frame", nil, UIParent);
  2. frame:SetFrameStrata("TOOLTIP");
  3. local texture = frame:CreateTexture();
  4. texture:SetTexture([[Interface\Cooldown\star4]]);
  5. --texture:SetVertexColor(1, 0, 0,.5);
  6. texture:SetBlendMode("ADD");
  7. texture:SetAlpha(0.4);
  8. local x = 0;
  9. local y = 0;
  10. local speed = 0;
  11.  
  12. local function OnUpdate(_, elapsed)
  13.   --Somehow speed or something else is getting to -nan, reset if so (temporary fix by raymar_of_brovaria)
  14.   if speed + 1 == speed then
  15.     speed = 0
  16.   end
  17.   if x + 1 == x then
  18.     x = 0
  19.   end
  20.   if y + 1 == y then
  21.     y = 0
  22.   end
  23.   --end of temp fix
  24. --local function OnUpdate(_, elapsed)
  25.   local dX = x;
  26.   local dY = y;
  27.   x, y = GetCursorPosition();
  28.   dX = x - dX;
  29.   dY = y - dY;
  30.   local weight = 2048 ^ -elapsed;
  31.   speed = math.min(weight * speed + (1 - weight) * math.sqrt(dX * dX + dY * dY) / elapsed, 1024);
  32.   local size = speed / 8 - 16;
  33.   if (size > 0) then
  34.     local scale = UIParent:GetEffectiveScale();
  35.     texture:SetHeight(size);
  36.     texture:SetWidth(size);
  37.     texture:SetPoint("CENTER", UIParent, "BOTTOMLEFT", (x + 0.5 * dX) / scale, (y + 0.5 * dY) / scale);
  38.     texture:Show();
  39.   else
  40.     texture:Hide();
  41.   end
  42. end
  43. frame:SetScript("OnUpdate", OnUpdate);

I've tried playing with SetPoint but haven't been able to figure it out.

EDIT: Ok so I've figured out the issue seems to be framerate, if I lock my frames the texture stays on the cursor as intended, if I uncap the framerate the texture will begin to lag behind the cursor. I'm going to assume this has something to do with how updating the position works and isn't something you can change via the addon? But I'll leave this up in case someone knows how this works.

Last edited by x2infinity : 12-30-23 at 06:42 PM.
  Reply With Quote
12-30-23, 08:59 PM   #2
Sharpedge
A Wyrmkin Dreamwalker
 
Sharpedge's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2022
Posts: 54
Try this and see if it helps:

Code:
local frame = CreateFrame("Frame", nil, UIParent);
frame:SetFrameStrata("TOOLTIP");
local texture = frame:CreateTexture();
texture:SetTexture([[Interface\Cooldown\star4]]);
texture:SetBlendMode("ADD");
texture:SetAlpha(0.5);

local x, y, speed = 0, 0, 0;
local MAX_SPEED = 1024;
local SPEED_DECAY = 2048;
local SIZE_MODIFIER = 6;
local MIN_SIZE = 16;

local function isNan(value)
  return value ~= value;
end

local function OnUpdate(_, elapsed)
  if isNan(speed) then speed = 0; end
  if isNan(x) then x = 0; end
  if isNan(y) then y = 0; end

  local prevX, prevY = x, y;
  x, y = GetCursorPosition();
  local dX, dY = x - prevX, y - prevY;

  local distance = math.sqrt(dX * dX + dY * dY);
  local decayFactor = SPEED_DECAY ^ -elapsed;
  speed = math.min(decayFactor * speed + (1 - decayFactor) * distance / elapsed, MAX_SPEED);

  local size = speed / SIZE_MODIFIER - MIN_SIZE;
  if size > 0 then
    local scale = UIParent:GetEffectiveScale();
    texture:SetHeight(size);
    texture:SetWidth(size);
    texture:SetPoint("CENTER", UIParent, "BOTTOMLEFT", (x + 0.5 * dX) / scale, (y + 0.5 * dY) / scale);
    texture:Show();
  else
    texture:Hide();
  end
end

frame:SetScript("OnUpdate", OnUpdate);
  Reply With Quote
01-01-24, 04:21 PM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
This is basically the difference between hardware and software cursor settings in many games. Anything rendered by the game is always going to have significant latency added on top of what the OS is doing.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Texture position lags behind cursor


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off