Is there a way to check the distance between 2 actors?

ShanestaShanesta Member Posts: 63
edited November -1 in Working with GS (Mac)
I'm making a shooting game, and I really need to be able to tell the distance between 2 actors for scoring purposes, or per pixel collision.

Is there a way to do this?

Thx

Comments

  • quantumsheepquantumsheep Member Posts: 8,188
    No per pixel collision I'm afraid :(

    Not sure about the other thing!

    Sorry!

    QS

    Dr. Sam Beckett never returned home...
    Twitter: https://twitter.com/Quantum_Sheep
    Web: https://quantumsheep.itch.io

  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    You can calculate the distance between centerpoints of your actors with a simple Pythagorean theorem.
    http://en.wikipedia.org/wiki/Pythagorean_theorem

    You'll need to set up some global attributes to contain the X/Y position of the target when it is struck. And another game attribute signifying that you have struck the target.

    Once the target is hit, the target should change those 3 game attributes with its position and make true the attribute showing that it has been struck.

    Once your firing actor sees that the target has been struck via the game attribute, use Pythagorean theorem with the targets position and the firing actor's position to find the sides of the right triangle.
  • ShanestaShanesta Member Posts: 63
    So I made 3 global variables TargetPos, ShotPos, and Distance. No I want to do a change attribute of game.Distance to TargetPos squared+ ShotPos squared, correct?

    How do I enter the expression, I don't see squared under the function dropdown?

    BTW , thx for you help man!
  • JGary321JGary321 Member Posts: 1,246
    I'm gonna leave this one up to CodeMonkey since I'm not sure he was even speaking English there. :)
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    http://gamesalad.com/wiki/expression_editor_reference#implicit_mathematical_functions

    See that link for doing squared and square root.

    game.Distance = (((game.TargetPosX-game.ShotPosX)^2)+((game.TargetPosY-game.ShotPosY)^2)))^(1/2)
  • ShanestaShanesta Member Posts: 63
    Ok, I made all 5 variables, TargetPosX,TargetPosY, ShotPosX,ShotPosY, and Distance.

    Made a Change Attribute actor, and in change attribute, I put game.Distance, and on the right side I put...

    (((game.TargetPosX-game.ShotPosX)^2)+((game.TargetPosY-game.ShotPosY)^2)))^(1/2)

    It does not work for me, the screen is black when I run it.

    Any idea what is going wrong? Have you actually tried it?

    This is an extremely common function, I'm surprised there isn't a Behavior for it.

    Thank man!
  • ShanestaShanesta Member Posts: 63
    Ok, I figured it out, there was one to many ) at he end there.

    Thx again for your help CodeMonkey!
Sign In or Register to comment.