Zwiebelchen
Hosted Project GR
- Joined
- Sep 17, 2009
- Messages
- 7,234
JASS:
library CameraPosition initializer init
/*
Getting the target and eye position of a local player's camera is crucial
for many systems that provide additional UI elements on screen
(like trackables, lightnings, texttags or special effects).
These natives, however, update at a much lower frequency in multiplayer games (every ~0.05-0.1 seconds)
despite not being synced anyway.
The aim of this system is to provide an alternative to:
GetCameraTargetPositionX
GetCameraTargetPositionY
GetCameraEyePositionX
GetCameraEyePositionY
by interpolating returned values between two consecutive value updates.
It also provides a sanity check in that the following functions will not interpolate if the camera was moved faster than a certain
configurable MOVEMENT_THRESHOLD per INTERVAL.
This is to make sure that instant camera movement (f.ex. by left-clicking the minimap) will not trigger the interpolation.
If you want to disable this feature, simply set MOVEMENT_TRESHOLD to a very large value (like 100.000).
The configurable MAX_TIMEOUT defines the number of iterations done until a guaranteed return value from GetCameraPosition natives is received. Usually this happens around 0.1 seconds. This is required to find out when the camera has stopped moving.
API:
function GetCameraTargetX takes nothing returns real
function GetCameraTargetY takes nothing returns real
function GetCameraEyeX takes nothing returns real
function GetCameraEyeY takes nothing returns real
*/
globals
//Configurable
private constant real INTERVAL = 0.03125 //determines the timer speed for update intervals; recommended value: 0.3125
private constant real MOVEMENT_THRESHOLD = 100 //determines the distance a camera can move within INTERVAL before the camera movement is assumed to be instant
private constant real MAX_TIMEOUT = 0.09 //determines the maximum time span over which the camera data will be interpolated if no new value is received (when the camera stopped moving)
private real TargX = 0.
private real TargY = 0.
private real EyeX = 0.
private real EyeY = 0.
private real lastTargX = 0.
private real lastTargY = 0.
private real lastEyeX = 0.
private real lastEyeY = 0.
private real vXTarg = 0.
private real vYTarg = 0.
private real vXEye = 0.
private real vYEye = 0.
private real timeout = 1.
endglobals
function GetCameraTargetX takes nothing returns real
return TargX
endfunction
function GetCameraTargetY takes nothing returns real
return TargY
endfunction
function GetCameraEyeX takes nothing returns real
return EyeX
endfunction
function GetCameraEyeY takes nothing returns real
return EyeY
endfunction
private function update takes nothing returns nothing
local real tx = GetCameraTargetPositionX()
local real ty = GetCameraTargetPositionY()
local real ex = GetCameraEyePositionX()
local real ey = GetCameraEyePositionY()
if tx != lastTargX or ty != lastTargY or ex != lastEyeX or ey != lastEyeY then
set vXTarg = (tx - lastTargX) / timeout
set vYTarg = (ty - lastTargY) / timeout
set vXEye = (ex - lastEyeX) / timeout
set vYEye = (ey - lastEyeY) / timeout
if vXTarg > MOVEMENT_THRESHOLD or vYTarg > MOVEMENT_THRESHOLD or vXEye > MOVEMENT_THRESHOLD or vYEye > MOVEMENT_THRESHOLD then
set vXTarg = 0
set vYTarg = 0
set vXEye = 0
set vYEye = 0
endif
set TargX = tx
set TargY = ty
set EyeX = ex
set EyeY = ey
set lastTargX = tx
set lastTargY = ty
set lastEyeX = ex
set lastEyeY = ey
set timeout = 1
else
if (timeout*INTERVAL) >= MAX_TIMEOUT then
set vXTarg = 0
set vYTarg = 0
set vXEye = 0
set vYEye = 0
set TargX = tx
set TargY = ty
set EyeX = ex
set EyeY = ey
set timeout = 1
else
set TargX = TargX + vXTarg
set TargY = TargY + vYTarg
set EyeX = EyeX + vXEye
set EyeY = EyeY + vYEye
set timeout = timeout + 1
endif
endif
endfunction
private function init takes nothing returns nothing
call TimerStart(CreateTimer(), INTERVAL, true, function update)
endfunction
endlibrary
Last edited: