# Another angle-calculating question

Get help using Construct 2

### » Fri Jan 25, 2013 9:09 am

Hallo.

I've had a search and can't really find this anywhere, so I've come to pick everyone's brains. Please feel free to reply with the obvious link I missed :P

What I'd like is for AI players (using car behaviour) to be constantly steering towards the player-character when aggro'd. In order to do this I figure I'll calculate the angle between the AI and the enemy player, and if it's outside a certain range then begin steering, and if it's within a certain range then start accelerating again. I was hoping the behaviour would look something like a fly buzzing around a ham. Unfortunately what I've got so far doesn't seem to be working:

Angle(AI.X, AI.Y, PC.X, PC.Y)

Which seems to give the relative angle between the AI and the PC, but not taking into account the direction in which the AI is facing... does anyone know of any solutions?

- Mr Tumbles
B
17
S
4
Posts: 85
Reputation: 1,304

### » Fri Jan 25, 2013 2:50 pm

This sounds like a job for the Boids plugin.
B
64
S
19
G
104
Posts: 3,092
Reputation: 64,049

### » Fri Jan 25, 2013 4:20 pm

there might be a way "around" it using distance. set a range instance variable for your ai cars and then compare -> range is greater than distance(player.x, player.y, ai.x, ai.y) (so within range) and then add which action you want steer towards or rotate towards player.x player.y

the only problem with this is that if they are ahead of the player they would turn backwards towards them.

you might encounter the same problem if it's a top down (as i found out trying to make a ragdoll that could walk using the sprite's .angle) is that the angle is dependant upon it's position. that is to say a leg piece might be at 45 degrees relative to its joint but it's sprite angle is say 90 or greater due to the whole thing having tipped forward. so your ai car may be at 45 degrees to the player car going up the track but 225 degrees coming down the other side.
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

### » Fri Jan 25, 2013 7:15 pm

Awesome stuff guys! Thanks - looking into both of these now. It's sposed to be for an arena-shooter type game - I wanted the enemies to behave dynamically, and have the same level of control over themselves as the player. I like the car behaviour because of the drift mechanic, which is why I wanted to simulate control rather than dictate movement directly.

The trouble I'm having is really that I need the enemy ship to know when it's facing the player - and thus to stop steering (and start shooting). Also having it steer in an appropriate direction (steering left when the player is to the left, rather than doing a full loop right as mine seems to keep doing!)

Loving the Boids by the way @blackhornet - definitely going in my plugin folder for future use!
B
17
S
4
Posts: 85
Reputation: 1,304

### » Fri Jan 25, 2013 7:20 pm

Hold on - might have a solution, pending a bit of testing. Will post test capx in a minute...
B
17
S
4
Posts: 85
Reputation: 1,304

### » Fri Jan 25, 2013 11:41 pm

Yes. It turns out it was demonically simple - as am I!

Here's the CapX:

B
17
S
4
Posts: 85
Reputation: 1,304

### » Sat Jan 26, 2013 12:15 am

In brief, it was much easier to do than I'd anticipated. I simply checked if the enemy ship was clockwise or counter-clockwise of:
[ Angle(enemy.X, enemy.Y, player.X, player.Y) ]
Then when within 20 degrees of
[ Angle(enemy.X, enemy.Y, player.X, player.Y) ] the enemy accelerates. The same check could be used to start the enemy firing their weapons.
B
17
S
4
Posts: 85
Reputation: 1,304

### » Sat Jan 26, 2013 1:51 am

@mrtumbles - very nice solution, this actually might even help me on a game idea i put on the back burner - cool! :D
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

### » Sat Jan 26, 2013 2:20 am

Yay! I HELPED SOMEBODY! That's not even sarcasm. Chuffed to have helped sir. Chuffed indeed. I'll post any advancements I make to it (working on strafing atm)
B
17
S
4
Posts: 85
Reputation: 1,304