• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] [Solved] How to calculate the distance between two units exactly?

Status
Not open for further replies.
Level 8
Joined
May 12, 2018
Messages
106
I want to create a ability that if A uses a spell to B, A and B are teleported halfway point between A and B. (Same as Chaos Knight's Reality Rift in DotA)

Untitled-1.jpg


But I'm having a hard time getting these math formulas. Below is my example script.
but this has a problem that they bounce off big time each the units' point and offset are changed.
JASS:
library FelbornChaosKnightRealityRift requires SpellEffectEvent, PluginSpellEffect, Utilities
 
    globals
        private constant integer AID = 'A0AD'

    endglobals
 
    private struct RealityRift
  
        private static method onCast takes nothing returns nothing
            local real x0 = Spell.source.x
            local real y0 = Spell.source.y
            local real x1 = Spell.target.x
            local real y1 = Spell.target.y
            local real angle = Atan2(y1 - y0, x1 - x0)
            local real xOffset = 0
            local real yOffset = 0
      
            set xOffset = x0 + (x1 - x0) * Cos(angle) * 0.5
            set yOffset = y0 + (y1 - y0) * Sin(angle) * 0.5

            call SetUnitX(Spell.source.unit, xOffset)
            call SetUnitY(Spell.source.unit, yOffset)
            call SetUnitX(Spell.target.unit, xOffset)
            call SetUnitY(Spell.target.unit, yOffset)
        endmethod
  
  
        private static method onInit takes nothing returns nothing
            call RegisterSpellEffectEvent(AID, function thistype.onCast)
        endmethod
    endstruct
endlibrary
 
Last edited:

MindWorX

Tool Moderator
Level 20
Joined
Aug 3, 2004
Messages
709
You can simply use the positions of the two units and then scale the difference between them, like this:
JASS:
// position of the first unit
local real x1 = GetUnitX(u1)
local real y1 = GetUnitY(u1)

// position of the second unit
local real x2 = GetUnitX(u)
local real y2 = GetUnitY(u2)

// vector between the two unit
local real dx = x2 - x1
local real dy = y2 - y1

local real t = 0.50 // midpoint

// point between the two units scaled by t, where 0 would be position of first unit and 1 would be position of second unit
local real tx = x1 + dx * t
local real ty = y1 + dy * t
 
Status
Not open for further replies.
Top