# How do I Calculate Angle Toward Position?

Get help using Construct 2

### » Sun Aug 24, 2014 3:57 pm

hi,
how do i Calculate angle toward position ?
in construct 2 we can set that and its good .. but i want to get it and test if my object angle is toward another object position then do something ...
thanks....
B
17
S
6
Posts: 246
Reputation: 1,786

### » Sun Aug 24, 2014 4:08 pm

I don't know if there's a specific way but if you know the locations of both objects, then the angle is given by the inverse tan of the difference in vertical pixels divided by the difference in horizontal pixels. just be aware of some symmetry problems.
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

### » Sun Aug 24, 2014 4:12 pm

Expression "angle(x1, y1, x2, y2)" will calculate angle between two points. Is that what you asked?
B
160
S
69
G
44
Posts: 2,612
Reputation: 36,855

### » Sun Aug 24, 2014 4:29 pm

shinkan wrote:Expression "angle(x1, y1, x2, y2)" will calculate angle between two points. Is that what you asked?

No , i want to Calculate Angle Toward Position of another object ..
look in this example:
dl.dropboxusercontent.com/u/5426011/examples%209/turret.capx
my problem is the turrets fire when they finde the target. but i want they not do that until they rotate toward the target .. like turret behavior
....
i can not use turret behavior because i want to set object with diffrent instance variable as a target
B
17
S
6
Posts: 246
Reputation: 1,786

### » Sun Aug 24, 2014 4:55 pm

@mahdi71

Objects have X and Y values for their position

So if you had an object Turret and an object Target, using the expression Shinkan suggested you'd do angle(Turret.X, Turret.Y, Target.X, Target.Y) to get the angle from Turret to Target.

You'd then compare this angle to Turret.Angle to see if it was pointing in the right direction.
Last edited by OddConfection on Sun Aug 24, 2014 5:08 pm, edited 1 time in total.
Find all the Construct 2 games that were on Steam Greenlight here
B
42
S
11
G
7
Posts: 513
Reputation: 7,355

### » Sun Aug 24, 2014 4:57 pm

OddConfection wrote:@mahdi71

Objects have X and Y values for their position

So if you had an object Turret and an object Target, using the expression Shinkan suggested you'd do angle(Turret.X, Target.X, Turret.Y, Target.Y) to get the angle from Turret to Target.

You'd then compare this angle to Turret.Angle to see if it was pointing in the right direction.

actually it should be angle(turret.x,turret.y,target.x,target.y)
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

### » Sun Aug 24, 2014 5:05 pm

LittleStain wrote:
OddConfection wrote:@mahdi71

Objects have X and Y values for their position

So if you had an object Turret and an object Target, using the expression Shinkan suggested you'd do angle(Turret.X, Target.X, Turret.Y, Target.Y) to get the angle from Turret to Target.

You'd then compare this angle to Turret.Angle to see if it was pointing in the right direction.

actually it should be angle(turret.x,turret.y,target.x,target.y)

Ok, so angle(Turret.X, Turret.Y, Target.X, Target.Y) as might be case-sensitive
Find all the Construct 2 games that were on Steam Greenlight here
B
42
S
11
G
7
Posts: 513
Reputation: 7,355

### » Sun Aug 24, 2014 5:25 pm

ok ...
you guys right
but angle(Turret.X, Turret.Y, Target.X, Target.Y) give me a number less then 0 sometimes like -110 so i add the number to 360 if that number is less then 0 then this solve my problem ... thanks ...
B
17
S
6
Posts: 246
Reputation: 1,786

### » Sun Aug 24, 2014 5:42 pm

@mahdi71

You could also use anglediff(angle1,angle2) when doing the comparison, which should be ok with negative angles.

Here's an example
Turret Rotate to Target.capx

Just drag the blue target around and the turret will follow it.
You do not have the required permissions to view the files attached to this post.
Find all the Construct 2 games that were on Steam Greenlight here
B
42
S
11
G
7
Posts: 513
Reputation: 7,355

### » Sun Aug 24, 2014 6:00 pm

OddConfection wrote:@mahdi71

You could also use anglediff(angle1,angle2) when doing the comparison, which should be ok with negative angles.

Here's an example
Turret Rotate to Target.capx

Just drag the blue target around and the turret will follow it.

Yes .. its better .. thanks ...
B
17
S
6
Posts: 246
Reputation: 1,786