Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 555449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:48:57+00:00 2026-05-13T11:48:57+00:00

Basically I am making a game where the enemy comes after the hero when

  • 0

Basically I am making a game where the enemy comes after the hero when He is in sight. What I do to achieve this is

var distx = hero.px - px;
var disty = hero.py - py;


moveX += distx * .00005;
moveY += disty * .00005;

Now I had to multiply it by .00005 because anything higher, makes him come up on the hero pretty quick. I want the enemy to move slow so I multiplied it by .00005. My problem is the farther the distance they are, the faster the monster moves which makes it harder for the hero to get away. I know why this is, I just need to know how to resolve it.

I develop the game where if the monster is too far away, i remove him from the stage. but the problem is getting away. I want him to move at a constant speed no matter how far apart they are.

I also want to achieve this by not using any class properties. (i.e I want all varibles to be local). This method is in a loop as well

What am I doing wrong? and what are solutions.

Thanks guys

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:48:57+00:00Added an answer on May 13, 2026 at 11:48 am

    All you have to do is rate limit the velocity of the enemy.

    // Calculate velocity just as you're doing it
    var p = .00005;
    var velx = distx * p;
    var vely = disty * p;
    
    // Rate limit that velocity
    var maxVel = 5; // You will have to tune this value as your max speed
    var vel = Math.sqrt(velx*velx + vely*vely);
    if (vel > maxVel) {
        var r = maxVel / vel;
        velx *= r;
        vely *= r;
    }
    
    // Move the enemy
    moveX += velx;
    moveY += vely;
    

    You have essentially implement only the “proportional” part of what control systems engineers (1) call a PID controller. Your desire is for the enemy to track the player, and the PID controller is a nice simple tunable (think enemy difficulties) tracking controller.

    In a tracking PID controller, you first measure the error between your desired goal and the current state of affairs. (You do this now, and called the variables distx and disty.) In a PID controller, you keep a memory of past errors so that you can act differently based upon different perspectives of the error.

    PID = function() {
        this.p = 0;
        this.i = 0;
        this.d = 0;
        this.reset(0);
    };
    
    PID.prototype.reset = function(error) {
        this.error = error;
        this.integral = 0;
        this.differential = 0;
    }   
    
    PID.prototype.step = function(error) {
        this.integral += error;
        this.differential = (error - this.error);
        this.error = error;
        var control = this.p * this.error +
                      this.i * this.integral +
                      this.d * this.differential;
        return control;
    };
    

    You create a PID controller for each of the controllable states for the object you are trying to control (the enemy). So your update logic will change to:

    // distx and disty are calculated as normal
    var velx = pidx.step(distx);
    var vely = pidy.step(disty);
    
    moveX += velx;
    moveY += vely;
    

    You can now get a variety of behaviors out of enemies by varying the p,i,d parameters of the PID object. To replicate your current AI, you would set:

    pidx.p = 0.00005;
    pidx.i = 0;
    pidx.d = 0;
    // same for pidy
    

    While a more interesting AI can be achieved by bumping up the i parameter and reducing p even further:

    pidx.p = 0.000005;
    pidx.i = 0.0005;
    pidx.d = 0;
    

    Every so often you will want to clear the enemy’s memory if he lasts for a very long time. Just call pidx.reset(distx) to reset that memory.

    Also you will still want to rate-limit the velx, vely as given earlier in this answer.

    What are the benefits of all this complexity? You get enemies that behave very naturally as compared to simpler maths. I also wanted to point out that you’re moving along the right tracks with your distx * 0.00005 logic.

    (1) Think of controls engineers as those who study the behavior of closed systems and use that knowledge to force the system to act in ways they desire.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably easier than I am making it, but basically what I need
I'm making a small game for a school project, and basically there are 20
I'm making a particle system for my game, which basically is smoke coming out
I am making a strategy game and came across a kind of problem. Basically,
I am making a game in ActionScript and it's basically an asteroids type game.
Basically I'm trying to improve on the Ghosts in a Pacman game I'm making.
So, basically, I've been writing this Game of Life PHP script. My output is
I'm making a 3D game with OpenGL that basically has a world made entirely
I'm making the memory game concentration. In this I have a specified obligation to
So I'm basically making an app that adds a count to a number and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.