Jump to content

Enemy rotation AI degrees problem


Recommended Posts

Hi, yesterday i was struggling with rotation of an enemy. In my game you drive a spaceship and the enemies should rotate to your location in the nearest way (left or right), so they can follow you.

This seems to be simple, but i find some problems in the rotation degrees.


First i have to find the angle that the enemy should have, to point to my spaceship.

I find two ways to do it. The first doesn't work:

angle = game.physics.arcade.angleBetween( this , player ); // returns angles between something like 3.1 and -3.1// ( this = Enemy class )

I don't know why. Maybe because i am using p2 physics and not arcade?


The second works:

angleRadians = Math.atan2(this.position.y - player.position.y, this.position.x - player.position.x);angle = Math.atan2(this.position.y - player.position.y, this.position.x - player.position.x) * 180 / Math.PI;// returns 180 to -180

So i use that instead.


Here I find something strange, the 0° angle in the Enemy is on Top. But, the 0° angle returned in the var angle is on the left. ( both ways in clockwise )


Finally i try doing something tricky like this, but doesn't work properly:

if( this.body.angle+180 - angle-90 <= 180 && this.body.angle+180 - angle-90 >= 0 ){    this.body.angle += .6;}else{    this.body.angle -= .6;} 

May if I figure out why the degrees are different, i can get it work.


Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...