- Joined
- May 24, 2005
- Messages
- 609
Hey everyone,
this is a small library that provides 2 functions, one for detecting if a unit is within the field of view of another unit, and one for detecting if a unit is behind another unit (using a specified angle). For example, these functions can be used for stealth gameplay involving field of view detection, backstab abilitites, etc.
I've attached a small testmap.
If you know ways to improve the script, i.e. some way of performance optimization or better approaches for FoV detection, please let me know and I will update it.
Sidenote: If you want to implement a realistic visibility/line of sight system in your map, you should also take care of doodads, like houses, which naturally should also block line of sight. Therefore, you'd need an additional function for checking if line of sight is blocked by a doodad; One way to do this is using a pathability library and check, if a pathing blocker is between the 2 units.
this is a small library that provides 2 functions, one for detecting if a unit is within the field of view of another unit, and one for detecting if a unit is behind another unit (using a specified angle). For example, these functions can be used for stealth gameplay involving field of view detection, backstab abilitites, etc.
- IsUnitInSightOfUnit( unit observer, unit target, real value )
Checks if target is within the field of view of observer
- IsUnitBehindUnit( unit unitToCheck, unit target, real value )
Checks if unitToCheck is behind target (within specified behind angle)
JASS:
library FieldOfView
//*********************************************************************
//* =================================
//* FieldOfView 1.0.3 (by MoCo)
//* =================================
//*
//* This library provides 2 functions:
//*
//* - IsUnitInSightOfUnit(unit observer, unit target, real fieldOfView)
//* Checks if unit 'target' is within the field of view cone of unit 'observer'
//*
//* - IsUnitBehindUnit(unit unitToCheck, unit target, real fieldOfView)
//* Checks if unit 'unitToCheck' is behind unit 'target' within fieldOfView
//*
//*
//* Setup:
//* ======
//* Copy this library to your map.
//*
//*
//* Usage:
//* ======
//* Use the parameter fieldOfView to set the field of view (FoV) that should be used for the detection function.
//* The parameter needs to be set to half of the total field of view you want the unit to use.
//* For example, if you want a unit to have a total field of view cone of 180°, you need to use a parameter value of 90
//*
//* Note that the natural human field of view is about 135°, so you could use a value of 67.5 here.
//*
//* See the FovTester script for practical examples on how to use the functions.
//*
//*
//********************************************************************
function IsUnitInSightOfUnit takes unit observer, unit target, real fov returns boolean
local real face = GetUnitFacing(observer)
local real angle = bj_RADTODEG*Atan2(GetUnitY(target) - GetUnitY(observer), GetUnitX(target) - GetUnitX(observer))
return not (RAbsBJ(face - angle) > fov and RAbsBJ(face - angle - 360.) > fov)
endfunction
function IsUnitBehindUnit takes unit unitToCheck, unit target, real fov returns boolean
local real face = GetUnitFacing(target)
local real angle = bj_RADTODEG*Atan2(GetUnitY(target) - GetUnitY(unitToCheck), GetUnitX(target) - GetUnitX(unitToCheck))
return not (RAbsBJ(face - angle) > fov and RAbsBJ(face - angle - 360.) > fov)
endfunction
endlibrary
Code:
//* 2015-04-25: The fieldOfView parameter usage has made more intuitive
//* 2015-01-21: The external Math library is no longer needed
//* 2015-01-10: The fieldOfView parameter now is an argument and now longer a global constant
I've attached a small testmap.
If you know ways to improve the script, i.e. some way of performance optimization or better approaches for FoV detection, please let me know and I will update it.
Sidenote: If you want to implement a realistic visibility/line of sight system in your map, you should also take care of doodads, like houses, which naturally should also block line of sight. Therefore, you'd need an additional function for checking if line of sight is blocked by a doodad; One way to do this is using a pathability library and check, if a pathing blocker is between the 2 units.
Attachments
Last edited by a moderator: