1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

How to check if a player is facing another...

Discussion in 'Programming' started by BigCat, Mar 3, 2019.

  1. BigCat

    BigCat Member

    Joined:
    Oct 30, 2016
    Posts:
    2
    I'm building a top-down shooter.

    I have a target-A which basically sits in the center of the room
    I have ships which fly around the room in various formation patterns or whatever.
    The ships have laserbeams that fire directly forward.

    I want the ships, as they fly about the room - to check whether they are facing target-A - and if so - shoot their lasers.

    I have everything figured out - EXCEPT - how to check if an instance is facing another instance.
    I've tried: if direction = point_angle ( x, y, target.x, target.y )
    I've tried: if image_angle = point_angle ( x, y, target.x, target.y )

    But I'm missing something or using it wrong - I dunno. It runs fine and doesn't throw any errors that I can see. But it just doesn't pull the trigger when it's supposed to.

    My best guess - was that the two values are really never the same because of the rotation of the enemy ships. They rotate at a specific rate per frame which likely makes the two sides of the equation basically impossible without a relative range in place.

    SO - My next attempt was something like
    IF direction - point_angle (x,y, target.x, target.y) <= 20
    But nope... that didn't do diddly either.

    So now i'm sad. *sigh*

    Any ideas or insight would be great.
    I'm using DND - but I'm not afraid to write complex expressions.
     
    Last edited: Mar 3, 2019
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,008
    Directions and angles are floats, so it might be that the facing's off by 0.0001 degrees, but that still means they ain't equal and no lasers get fired. Instead you need to check that the difference is small enough, then shoot the laser. In that, angle_difference() is your friend.
     
    BigCat likes this.
  3. BigCat

    BigCat Member

    Joined:
    Oct 30, 2016
    Posts:
    2
    BOOM!

    @NightFrost saves my bacon! :)

    By getting the angle_difference between image_angle, and point_angle ; I was able to create a safe fire zone it could find.

    THANK YOU !!!! :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice