Another angle-calculating question

0 favourites
  • 9 posts
  • 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
  • This sounds like a job for the Boids plugin.

  • 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.

  • 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!

  • Hold on - might have a solution, pending a bit of testing. Will post test capx in a minute...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yes. It turns out it was demonically simple - as am I!

    Here's the CapX:

  • 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.

  • mrtumbles - very nice solution, this actually might even help me on a game idea i put on the back burner - cool! :D

  • 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)

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