Talk:Sqrt - Math

From the CreationKit Wiki
Revision as of 15:58, 13 July 2023 by Ingvion (talk | contribs) (→‎Calculating distance (example))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Calculating distance (example)[edit source]

Calculating distance travelled with the Pythagorean theorem, using 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 fDist = 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 " + fDist + " units.")
EndEvent

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