megabyterain

P2 Space Shooter Dogfight AI Rotation

Recommended Posts

I'm trying to make a 2d space game involving a sort of space dogfight between the player and some ai bots.

I need help with the enemy ship automatically rotating towards the player, but not being exactly pointed towards the ship 24/7. I want the enemy ship to have some "lag" in its response to the player's movement so it's more realistic.

Right now I have a model which is not realistic and would probably be too hard for the player to win:

update: function(){
     //blah blah
     var tempAngle = Math.atan2(fightSprites.player.y-fightSprites.enemy[i].y, fightSprites.player.x - fightSprites.enemy[i].x); //gets angle between player and enemy
     fightSprites.enemy[i].body.rotation = tempAngle + (3.141/2); //too precise
}

 

Share this post


Link to post
Share on other sites

You could make a hidden ship that flies behind the player, maybe even following in its path. Aim the enemy toward that. It'll follow eventually and encourage the player to not stay in the same place.

The enemy ships could point at a displacement of the vector of the player ship: if it's a hard AI it'll lead the ship, if easier it could lag it.

Share this post


Link to post
Share on other sites
3 hours ago, drhayes said:

You could make a hidden ship that flies behind the player, maybe even following in its path. Aim the enemy toward that. It'll follow eventually and encourage the player to not stay in the same place.

The enemy ships could point at a displacement of the vector of the player ship: if it's a hard AI it'll lead the ship, if easier it could lag it.

But then I would need to lag the hidden ship which gives me the same problem I'm having right now.

3 hours ago, drhayes said:

The enemy ships could point at a displacement of the vector of the player ship

point at the displacement of vector?

Share this post


Link to post
Share on other sites
var currentAngle = fightSprites.enemy[i].body.rotation;
    correctAngle = tempAngle + (3.141/2);
    difference = correctAngle - currentAngle;
    lag = 0.3;
    
fightSprites.enemy[i].body.rotation += difference * lag;

Maybe it will help. 

Share this post


Link to post
Share on other sites
9 hours ago, noobshit said:

var currentAngle = fightSprites.enemy[i].body.rotation;
    correctAngle = tempAngle + (3.141/2);
    difference = correctAngle - currentAngle;
    lag = 0.3;
    
fightSprites.enemy[i].body.rotation += difference * lag;

Maybe it will help. 

That works much better; not perfect, but better. However, there is another problem. When I fly the player around the enemy the enemy will glitch and spin for a few moments. I've attached a video. I'm guessing this is because body.rotation is a value from -3.14 to 3.14 and jumps between them? How can I fix this?

nimbus-record-video.webm

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.