How to determine target relative position

Just started using Construct 2? Post your questions here

Post » Wed Mar 04, 2015 10:00 am

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

Massimo
You do not have the required permissions to view the files attached to this post.
B
26
S
3
G
1
Posts: 31
Reputation: 1,513

Post » Wed Mar 04, 2015 10:14 am

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?
You do not have the required permissions to view the files attached to this post.
B
8
S
1
Posts: 72
Reputation: 606

Post » Wed Mar 04, 2015 1:59 pm

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.
B
26
S
3
G
1
Posts: 31
Reputation: 1,513

Post » Wed Mar 04, 2015 2:46 pm

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
B
8
S
1
Posts: 72
Reputation: 606

Post » Wed Mar 04, 2015 2:50 pm

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
B
26
S
3
G
1
Posts: 31
Reputation: 1,513

Post » Wed Mar 04, 2015 3:04 pm

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
B
8
S
1
Posts: 72
Reputation: 606


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 3 guests