Calculating distance (example)

Calculating distance travelled with Pythagorean theorem, uses sqrt and pow:

Event OnEffectStart(Actor akTarget, Actor akCaster)
	target = akTarget

	coords = new float[6]  ; let's not declare 6 separate vars
    coords[0] = target.X   ; X = hardcoded short for GetPositionX(), same for other axes
    coords[1] = target.Y
    coords[2] = target.Z

	RegisterForSingleUpdate(5)
EndEvent

Event OnUpdate()
    coords[3] = target.X
    coords[4] = target.Y
    coords[5] = target.Z

	; Pythagorean theorem for 3D planes: sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)

	float dist = Math.sqrt(Math.pow((coords[3] - coords[0]), 2) + Math.pow((coords[4] - coords[1]), 2) + Math.pow((coords[5] - coords[2]), 2))
	Debug.trace("Travelled " + dist + " units.")
EndEvent

--Ingvion (talk) 10:13, 13 July 2023 (EDT)

Return to "Sqrt - Math" page.