GameSalad

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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

ShanestaShanesta Posts: 63Member
edited 12:55PM 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 Posts: 8,188Member
    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: http://www.quantum-sheep.com

  • CodeMonkeyCodeMonkey Posts: 1,803Head Chef, Member, PRO
    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 Posts: 63Member
    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 Posts: 1,246Member
    I'm gonna leave this one up to CodeMonkey since I'm not sure he was even speaking English there. :)
  • CodeMonkeyCodeMonkey Posts: 1,803Head Chef, Member, PRO
    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 Posts: 63Member
    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 Posts: 63Member
    Ok, I figured it out, there was one to many ) at he end there.

    Thx again for your help CodeMonkey!
This discussion has been closed.