How to determine target relative position

0 favourites
  • 6 posts
From the Asset Store
Aim carefully a shoot the ball at the target to earn scores!
  • Hi all, a new-newbie question.

    I’m sketching a “naval battle” game - below you can see the player’s ship with hers four spotting quadrants.

    Cannot figure out a decent method to get the position (relatively to player’s ship actual facing), of a potential target in a given moment - i.e., in which quadrant a given enemy ship is. I wasn’t able to find anything useful in this section of the forum, in the manual or tutorials .

    The only method I can imagine is pinning four invisible images (pointing 45°, 135°, 225° and 315° with a 90° LOS each) to the player’s sprite, and check in which LOS the target is, but it’s quite rough... I suppose there is a simple & effective method but I cannot find it.

    Thx in advance


  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • What about my capx-source?

    You can drive green sprite with 8Direction behavior.

    When blue sprite will have LOS to green sprite you'll see Angle between them.

    Use this angle to locate, where is your enemy (in I, II, III or IV quadrant).

    Is it what you want?

  • Thank you for your help.

    Your capx would be ok, but it does not take into account the facing, continuously changing, of the player’s ship.

    Let's say your blu square is the player’s ship, pointing upwards as in my attachment (i.e. 270° according to the Construct 2 convention); if the green square is exactly on its right, your code returns an angle of 0°. Ok, but if the player changes his ship’s facing, this value must change accordingly. F. ex., rotating the ship of, say, 90° on the right, while the green square remains still, the code should now return an angle of =90°, as the green square is now exactly in front of the ship.

  • make this conversion by yourself.. it's not so important for math.

    if you need 270 degrees out where my program returns 0 just make a "270-degrees-offset"

    it's only important when you make an output for user

    maybe you need to make an blue sprite angle offset

    I can make one more example for you a bit later, I'm busy for now.

    Maybe you'll make it earlier by yourself

  • No, thx, is not necessary, I alreeady succeeded in figuring it out myself

    It's ok also f.ex. subtracting the Player_ship.Angle to the obtained value.

    Thanks again

  • ok, but be carefull!

    function Angle(x1,y1,x2,y2) returns from 0 to 180 and then from -179.99...99 to -0.00...01

    there is no from 0 to 360 degrees.

    you need to correct this moment if it's necessary

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)