Results 1 to 13 of 13
  1. #1
    Marrow Fiend
    Join Date
    Jun 2009
    Posts
    4,479

    Default Calculating distance

    So I am mathematically calculating the distance between a character and a target location. Dealing with positive numbers is no problem, but I am unsure of how to manage the negatives. The Map is placed at 0,0,0 so there will be equally much negative vectors as positive, so I need to work this out.

    Is there perhaps a way to get the total amount between two numbers other than subtracting (since that doesn't work with negatives)

    I tried doing abs() to make it all positives, but there a logic issue with that. The calculation is not correct if one is positive and the other negative, IE the character is at -1000 and the target is at x 1000, it believes the target is reached since it subtracts 1000 with 1000.

    Or should I perhaps do some conditions, and subtract double the amount (to accomodate being negative)? It will have to be a ton conditions though.

    Edit: I think I've gone about it the wrong way, I'll just subtract the negatives then do an abs() on the result.
    Last edited by Graylord; 12-25-2011 at 01:39 PM.
    Please don't send me private messages asking how to use UDK unless it has to do with my work, everything I can teach is already out there.

    I am not support, I am here to learn myself.

  2. #2
    Redeemer
    Join Date
    Dec 2008
    Location
    Germany
    Posts
    1,234

    Default

    Uhm,

    Code:
    aVector = Target.Location - Character.Location;
    VSize(aVector);
    ?
    2B || !(2B)

  3. #3
    Marrow Fiend
    Join Date
    Jun 2009
    Posts
    4,479

    Default

    Could you elaborate on VSize? How does it work and what does it give?

    I need an int or a float that gives the total distance.
    Please don't send me private messages asking how to use UDK unless it has to do with my work, everything I can teach is already out there.

    I am not support, I am here to learn myself.

  4. #4
    MSgt. Shooter Person
    Join Date
    Sep 2010
    Location
    Slovakia
    Posts
    483

    Default

    I usually use Vsize too. But i found this on google:

    Code:
    Math:
    Vector1=(1,2,0) 
    Vector2=(-2,3,5)
    
    sqrt((1 + 2)^2 + (2 - 3)^2 + (0 - 5)^2)
    = sqrt(9 + 1 + 25)
    = sqrt(35)
    Code:
    Unrealscript:
    VectorCache = Vector1 - Vector2;
    Distance = VSize(VectorCache);
    Distance is float or int...
    Vector 1 is usually target and Vector 2 location of pawn...
    Last edited by nonder; 12-25-2011 at 02:34 PM.
    Czech and Slovak UDK site - www.udk-site.net-core.eu
    Space Shock project on STEAM GREENLIGHT

  5. #5

    Default

    Use VSize, it's exactly what you "found on google".
    Wormbo's UT/UT2004/UT3 mods | PlanetJailbreak | Unreal Wiki | Liandri Archives

    <elmuerte> you shouldn't do all-nighters, it's a waste of time and effort
    <TNSe> nono
    <TNSe> its always funny to find code a week later you dont even remember writing
    <Pfhoenix> what's worse is when you have a Star Wars moment
    <Pfhoenix> "Luke! I am your code!" "No! Impossible! It can't be!"
    Note that your questions via PMs will be ignored if they actually belong in the forum.

  6. #6
    Marrow Fiend
    Join Date
    Jun 2009
    Posts
    4,479

    Default

    TargetDistance = Abs(PawnLocation.X - TargetLocation.X) + Abs(PawnLocation.Y - TargetLocation.Y);

    Wouldn't this also work? (I want to ignore Z, that's why it's missing, though it should theoretically just be adding another abs with it.)
    Last edited by Graylord; 12-25-2011 at 03:40 PM.
    Please don't send me private messages asking how to use UDK unless it has to do with my work, everything I can teach is already out there.

    I am not support, I am here to learn myself.

  7. #7
    Redeemer
    Join Date
    Dec 2008
    Location
    Germany
    Posts
    1,234

    Default

    Well that's only half way there.
    Draw that on a piece of paper or something and you will see that you need to use Pythagorean theorem at some point.

    You also don't necessarily need the sqrt.

    (If you want to ignore z then there's also a VSize2D function which still takes a normal vector [with z] as a parameter)
    2B || !(2B)

  8. #8
    Marrow Fiend
    Join Date
    Jun 2009
    Posts
    4,479

    Default

    Ah I see.

    What do you mean with Z as a parameter? I can't find any obvious examples of it, and there seems to be no references on UDN.

    But I should theoretically be able to just use VSize if I nullify the Z on both vectors right before the math, right?
    Please don't send me private messages asking how to use UDK unless it has to do with my work, everything I can teach is already out there.

    I am not support, I am here to learn myself.

  9. #9

    Default

    Use VSize2D if you want to ignore the Z component. I recommend reading up on vector operations in general, as you seem to be a bit lacking in that area.
    Wormbo's UT/UT2004/UT3 mods | PlanetJailbreak | Unreal Wiki | Liandri Archives

    <elmuerte> you shouldn't do all-nighters, it's a waste of time and effort
    <TNSe> nono
    <TNSe> its always funny to find code a week later you dont even remember writing
    <Pfhoenix> what's worse is when you have a Star Wars moment
    <Pfhoenix> "Luke! I am your code!" "No! Impossible! It can't be!"
    Note that your questions via PMs will be ignored if they actually belong in the forum.

  10. #10
    Marrow Fiend
    Join Date
    Jun 2009
    Posts
    4,479

    Default

    So I can see the reference to VSize2D there, but I cannot see how you would use it to just simply avoid Z. Why is using VSize with two vectors that has 0 in the Z unfeasible?
    Please don't send me private messages asking how to use UDK unless it has to do with my work, everything I can teach is already out there.

    I am not support, I am here to learn myself.

  11. #11

    Default

    It's what VSize2D does, ignores the Z component.

    TargetDistance = VSize2D(PawnLocation - TargetLocation);

  12. #12

    Default

    N.B.: The expression "Vector2 - Vector1" calculates a new vector such that if you put the starts of the two vectors together, the resulting vector goes from the first vector's end to the second vector's end. And since location vectors basically all have their starts stuck together at the origin, the end-to-end vector is exactly what you want to calculate the length of.
    Wormbo's UT/UT2004/UT3 mods | PlanetJailbreak | Unreal Wiki | Liandri Archives

    <elmuerte> you shouldn't do all-nighters, it's a waste of time and effort
    <TNSe> nono
    <TNSe> its always funny to find code a week later you dont even remember writing
    <Pfhoenix> what's worse is when you have a Star Wars moment
    <Pfhoenix> "Luke! I am your code!" "No! Impossible! It can't be!"
    Note that your questions via PMs will be ignored if they actually belong in the forum.

  13. #13
    MSgt. Shooter Person
    Join Date
    Sep 2010
    Posts
    30

    Default

    For an optimization if you are trying to compare 2 distances or compare to a fixed distance you can use VSizeSQ which removes the square root from the distance equation. This is a nice little optimization if you are checking distance every tick. ex. to check which of 2 actors is closer,
    dist1 = vsizesq(location - actor1.location)
    dist2 = vsizesq(location - actor2.location)

    if(dist1 > dist2)
    //actor 2 is closer.

    the the values will be squared but the actual distance is irrelevant. similarly if you want to see if something is within 25 uu. you can just check if(dist < 625) //25^2


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright ©2009-2011 Epic Games, Inc. All Rights Reserved.
Digital Point modules: Sphinx-based search vBulletin skin by CompletevB.com.