playMaker

Author Topic: GetRelativeDirection2D  (Read 459 times)

Thore

  • Full Member
  • ***
  • Posts: 181
    • View Profile
GetRelativeDirection2D
« on: September 26, 2018, 02:05:35 PM »
While learning to code some more, I wanted to do an AddExplosion2D version and ended up making a more generalized action that may be useful. I am almost certain something like this exists, but didn't find it.

GetRelativeDirection2DEvent
You provide an origin GameObject, and a target. It calculates the direction and distance, as X and Y separated. When the target is left or down from origin, it returns the distances as negative numbers.

Note, there is also GetDistance (in Ecosystem at least), which tells you the direction in a straight line from origin to target.



Store Directions: stores the float values for later use. Note that left and down return negative values. When you don't want this, use Float Abs in a later step. Though consider if this is the right action for you.

Store isRight & isUp: convenient bools to use later. E.g. with Bool Test when isUp is false, you know the target is downwards.

Every Frame: what it says on the tin. As is standard, will ignore events. I have made a twin action (GetRelativeDirection2D) that doens't have the events, which you may prefer.

Preference: Allows you to set how to "interpret" the data. Also see my graphic attached.
  • Match Best
    'Common Sense' view, which is in mathematical terms the distance that is furthest away and thus "clearest", i.e. if the target is more up than left, then consider it upwards.
  • Horizontal/Vertical Only
    Only considers one axis.
  • Short Axis
    The opposite from Match Best. Best explained with a use case. Take an arcade shooter like Space Invaders. The enemies might be considered best as "upwards", but to shoot them, you want to move either a bit to the right or left, i.e. along the shorter distance axis.
  • Inverted
    Inverted version from Match Best. Makes intuitive sense to think of the roles reversed, i.e. not the target is upwards from origin, but origin is downwards from target.

As Usual
I'm not a coder, it may or may not be the best way to do this. However, it does the trick nicely. There's an alternative method out-commented to also allow to switch local/world space, which I adapted from another action. However, that didn't make sense to me, so I didn't use it.

If someone wants to improve on it, make more actions based on this, test it, and upload it to Ecosystem, feel free! :)
« Last Edit: September 27, 2018, 05:12:16 PM by Thore »

Thore

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Re: GetRelativeDirection2D
« Reply #1 on: September 26, 2018, 02:36:40 PM »
And here's a quick test scene (does not contain the actions, add GetRelativeDirection2DEvent before you start this).

menosa

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Re: GetRelativeDirection2D
« Reply #2 on: September 27, 2018, 07:31:11 AM »
 I will check them out, if think this is just what I needed. cheers  :)

craigz

  • Beta Group
  • Full Member
  • *
  • Posts: 178
    • View Profile
    • Haven Made
Re: GetRelativeDirection2D
« Reply #3 on: September 27, 2018, 08:44:10 AM »
This is great Thore :D thanks for making this :)

Question! What's the usecase that you've been using it for if you dont mind me asking? :D

Thore

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Re: GetRelativeDirection2D
« Reply #4 on: September 27, 2018, 09:03:57 AM »
This is great Thore :D thanks for making this :)

Question! What's the usecase that you've been using it for if you dont mind me asking? :D

As a side project for fun, I work on a 2D platformer type of game. I originally started the action for a (cartoon) bomb to add force based on where the targets are located, relative to the explosive. I use it there, though I'd like to have a better AddExplosiveForce2D version, which I started making, but am stuck (thread in this help subforum, chime in!).