Sign in to follow this  
goyeneche

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.

Thanks!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.