Komaqtion
You can change this now in User CP.
- Reaction score
- 469
Hello
I was just helping out a person in a thread in the WE Help section, and he asked for a rather complex formula to calculate the interception-point of two entities moving.
This formula was made up by perkeyone, but he didn't really know how to put it into JASS code, so I did that and also made a few tweaks and additions to it, and here it is:
This is just a small snippet, so I don't think a demo map is necessary
I was just helping out a person in a thread in the WE Help section, and he asked for a rather complex formula to calculate the interception-point of two entities moving.
This formula was made up by perkeyone, but he didn't really know how to put it into JASS code, so I did that and also made a few tweaks and additions to it, and here it is:
JASS:
library InterceptionData
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//*********************************************************************************************************//
//@@/////////////////////////////////// *// InterceptionData \\* ////////////////////////////////////////@@//
//@@ @@//
//@@ Made by , Komaqtion @ TheHelper.net @@//
//@@ @@//
//@@ @@//
//@@ Purpose: @@//
//@@ @@//
//@@ # The purpose of this small snippet is to easily be able to locate a point @@//
//@@ in the map, where two entities will meet if their path isn't changed. @@//
//@@ @@//
//@@ Usage: @@//
//@@ @@//
//@@ # *NOTE*: All functions here use one of two sets of parameters: @@//
//@@ @@//
//@@ Parameter-Set 1: @@//
//@@ 'real castX, real castY, real castSpeed, real targX, real targY, real targSpeed, real targAngle' @@//
//@@ @@//
//@@ real castX, real castY -> These are the coordinates of the entity which you probably @@//
//@@ will want to use to move against the intersection point. @@//
//@@ real castSpeed -> This is the current speed of the "casting" entity. @@//
//@@ real targX, real targY -> These are the current coordinates of the entity which the @@//
//@@ caster will be intersecting @@//
//@@ real targSpeed -> This is the current speed of the target entity @@//
//@@ real targAngle -> This is the angle of which the target is currently moving @@//
//@@ towards. @@//
//@@ @@//
//@@ Parameter-Set 2: @@//
//@@ 'unit caster, real casterSpeed, unit target, real targetSpeed' @@//
//@@ @@//
//@@ unit caster -> This is the "casting" unit, or the unit which you will @@//
//@@ use the intersection point with. @@//
//@@ real casterSpeed -> This is the current speed of the "casting" unit. @@//
//@@ unit target -> This is the target unit, which currently has a heading @@//
//@@ real targetSpeed -> This is the current speed of the target unit. @@//
//@@ @@//
//@@ @@//
//@@ # Now, the functions provided by this small snippet are: @@//
//@@ *NOTE*: ALL THE ANGLES IN THE PARAMETERS HERE ARE IN RADIANS ! @@//
//@@ (To make degrees into radians, simply multiply the angle by bj_DEGTORAD) @@//
//@@ @@//
//@@ function GetInterceptionLoc takes Parameter-Set 1 returns location @@//
//@@ @@//
//@@ - "returns location" -> This function will return the location where @@//
//@@ the two paths will intersect, or cross eachother. @@//
//@@ @@//
//@@ function GetInterceptionLocSimple takes Parameter-Set 2 returns location @@//
//@@ @@//
//@@ - "returns location" -> This function will return the location where @@//
//@@ the two paths will intersect, or cross eachother. @@//
//@@ @@//
//@@ function GetInterceptionX takes Parameter-Set 1 returns real @@//
//@@ @@//
//@@ - "returns real" -> This function will return the X-coordinate @@//
//@@ where the two paths will intersect, or cross eachother. @@//
//@@ @@//
//@@ function GetInterceptionXSimple takes Parameter-Set 2 returns real @@//
//@@ @@//
//@@ - "returns real" -> This function will return the X-coordinate @@//
//@@ where the two paths will intersect, or cross eachother. @@//
//@@ @@//
//@@ function GetInterceptionY takes Parameter-Set 1 returns real @@//
//@@ @@//
//@@ - "returns real" -> This function will return the Y-coordinate @@//
//@@ where the two paths will intersect, or cross eachother. @@//
//@@ @@//
//@@ function GetInterceptionYSimple takes Parameter-Set 2 returns real @@//
//@@ @@//
//@@ - "returns real" -> This function will return the Y-coordinate @@//
//@@ where the two paths will intersect, or cross eachother. @@//
//@@ @@//
//@@ function GetInterceptionAngle takes Parameter-Set 1 returns real @@//
//@@ @@//
//@@ - "returns real" -> This function will return the angle @@//
//@@ from the "caster" to the intersect point. (Should be in radians !) @@//
//@@ @@//
//@@ function GetInterceptionAngleSimple takes Parameter-Set 1 returns real @@//
//@@ @@//
//@@ - "returns real" -> This function will return the angle @@//
//@@ from the "caster" to the intersect point. (Should be in radians !) @@//
//@@ @@//
//@@ @@//
//@@ Requirements: @@//
//@@ @@//
//@@ # This snippet's only requirement is vJASS compilement, which is @@//
//@@ easiest achieved by downloading JASS Newgen Pack, at @@//
//@@ <a href="http://www.thehelper.net/forums/showthread.php?t=73936" class="link link--internal">http://www.thehelper.net/forums/showthread.php?t=73936</a> @@//
//@@ You'll also have to update JASS Helper to the latest version... @@//
//@@ @@//
//@@ @@//
//@@ Credits: @@//
//@@ @@//
//@@ # - Da1nOnlyEd, for actually making me code this little thing <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin :D" loading="lazy" data-shortname=":D" /> @@//
//@@ - Perkeyone, for actually doing the math and providing the formula @@//
//@@ which this snippet uses ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin :D" loading="lazy" data-shortname=":D" /> @@//
//@@ - BlackRose, for a small tweak for a slight efficiency gain ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink ;)" loading="lazy" data-shortname=";)" /> @@//
//@@ @@//
//@@ # And credits, if you use this that is, is not needed to give me @@//
//@@ though it's always welcome <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink ;)" loading="lazy" data-shortname=";)" /> @@//
//@@ @@//
//@@/////////////////////////////////////////////////////////////////////////////////////////////////////@@//
//*********************************************************************************************************//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function GetInterceptionLoc takes real castX, real castY, real castSpeed, real targX, real targY, real targSpeed, real targAngle returns location
local real difx = castX - targX
local real dify = castY - targY
local real interX
local real interY
local real cosa = Cos( targAngle )
local real sina = Sin( targAngle )
local real dist = difx * difx + dify * dify
local real a = 2 * targSpeed * ( cosa * difx + sina * dify )
local real sqr = SquareRoot( ( a * a ) - ( 4 * dist * ( targSpeed * targSpeed - castSpeed * castSpeed ) ) )
local real x1 = 2 * dist / ( ( -a ) + sqr )
local real x2 = 2 * dist / ( ( -a ) - sqr )
local real x
if x1 >= 0. and ( x1 < x2 or x2 <= 0. ) then
set x = x1
else
set x = x2
endif
set interX = targX + targSpeed * x * cosa
set interY = targY + targSpeed * x * sina
return Location( interX, interY )
endfunction
function GetInterceptionLocSimple takes unit caster, real casterSpeed, unit target, real targetSpeed returns location
return GetInterceptionLoc( GetUnitX( caster ), GetUnitY( caster ), casterSpeed, GetUnitX( target ), GetUnitY( target ), targetSpeed, GetUnitFacing( target ) * bj_DEGTORAD )
endfunction
function GetInterceptionX takes real castX, real castY, real castSpeed, real targX, real targY, real targSpeed, real targAngle returns real
local real difx = castX - targX
local real dify = castY - targY
local real cosa = Cos( targAngle )
local real sina = Sin( targAngle )
local real dist = difx * difx + dify * dify
local real a = 2 * targSpeed * ( cosa * difx + sina * dify )
local real sqr = SquareRoot( ( a * a ) - ( 4 * dist * ( targSpeed * targSpeed - castSpeed * castSpeed ) ) )
local real x1 = 2 * dist / ( ( -a ) + sqr )
local real x2 = 2 * dist / ( ( -a ) - sqr )
local real x
if x1 >= 0. and ( x1 < x2 or x2 <= 0. ) then
set x = x1
else
set x = x2
endif
return targX + targSpeed * x * cosa
endfunction
function GetInterceptionXSimple takes unit caster, real casterSpeed, unit target, real targetSpeed returns real
return GetInterceptionX( GetUnitX( caster ), GetUnitY( caster ), casterSpeed, GetUnitX( target ), GetUnitY( target ), targetSpeed, GetUnitFacing( target ) * bj_DEGTORAD )
endfunction
function GetInterceptionY takes real castX, real castY, real castSpeed, real targX, real targY, real targSpeed, real targAngle returns real
local real difx = castX - targX
local real dify = castY - targY
local real cosa = Cos( targAngle )
local real sina = Sin( targAngle )
local real dist = difx * difx + dify * dify
local real a = 2 * targSpeed * ( cosa * difx + sina * dify )
local real sqr = SquareRoot( ( a * a ) - ( 4 * dist * ( targSpeed * targSpeed - castSpeed * castSpeed ) ) )
local real x1 = 2 * dist / ( ( -a ) + sqr )
local real x2 = 2 * dist / ( ( -a ) - sqr )
local real x
if x1 >= 0. and ( x1 < x2 or x2 <= 0. ) then
set x = x1
else
set x = x2
endif
return targY + targSpeed * x * sina
endfunction
function GetInterceptionYSimple takes unit caster, real casterSpeed, unit target, real targetSpeed returns real
return GetInterceptionY( GetUnitX( caster ), GetUnitY( caster ), casterSpeed, GetUnitX( target ), GetUnitY( target ), targetSpeed, GetUnitFacing( target ) * bj_DEGTORAD )
endfunction
function GetInterceptionAngle takes real castX, real castY, real castSpeed, real targX, real targY, real targSpeed, real targAngle returns real
local real difx = castX - targX
local real dify = castY - targY
local real interX
local real interY
local real cosa = Cos( targAngle )
local real sina = Sin( targAngle )
local real dist = difx * difx + dify * dify
local real a = 2 * targSpeed * ( cosa * difx + sina * dify )
local real sqr = SquareRoot( ( a * a ) - ( 4 * dist * ( targSpeed * targSpeed - castSpeed * castSpeed ) ) )
local real x1 = 2 * dist / ( ( -a ) + sqr )
local real x2 = 2 * dist / ( ( -a ) - sqr )
local real x
local real angle
if x1 >= 0. and ( x1 < x2 or x2 <= 0. ) then
set x = x1
else
set x = x2
endif
set interX = targX + targSpeed * x * cosa
set interY = targY + targSpeed * x * sina
set angle = Atan2( interY - castY, interX - castX )
return angle
endfunction
function GetInterceptionAngleSimple takes unit caster, real casterSpeed, unit target, real targetSpeed returns real
return GetInterceptionAngle( GetUnitX( caster ), GetUnitY( caster ), casterSpeed, GetUnitX( target ), GetUnitY( target ), targetSpeed, GetUnitFacing( target ) * bj_DEGTORAD )
endfunction
endlibrary
This is just a small snippet, so I don't think a demo map is necessary