Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming smooth x/y acceleration change to target a moving point

  1. Sep 1, 2009 #1
    TL;dr: "I am not sure how to calculate a smooth transition of thrust between one vector and another."

    Hi, this looked like the right place to post this being "The mathematics of change and motion". I am programming a simple game where an enemy chases after the player in an open space (no walls). I was calculating the enemy's x & y velocities independently, accelerating them if they were taking them in the direction of the player and quickly slowing them if they were going the wrong way (e.g. EnemyVelocity.x > 0 & player.x < enemy.x, then EnemyVelocity.x - 2.)

    While the gameplay is decently fun trying to dodge the enemy, it is my desire to have the enemy behave using proper physics. What I am currently doing is have the enemy set their thrust (think a spaceship) based on the angle between them and the player, and have their thrust accelerate up to a max speed (calculating side c of the EnemyVelocity triangle). Once that happens, I'm not sure the best way to have the thrust adjust itself. If I leave no max speed, the enemy accelerates nicely but will easily overshoot the player, and then take a long time to get enough momentum heading back in the player's direction.

    What I'd like to happen is have the enemy constantly adjust their velocity on the way to the player, targeting wherever they are at (I don't want them to predict where you will be). Then, when they miss the player, I'd like for the same thrust & acceleration formulas to readjust their velocity and send them back at the player.

    I'm thinking this will involve two vectors: one where the enemy is currently traveling, and one where the enemy wants to travel (the vector that will take them straight to the player). I am not sure how to calculate a smooth transition of thrust between one vector and another.

    Any tips, formulas or questions will be much appreciated! Thank you Physics Forums.
     
  2. jcsd
  3. Sep 2, 2009 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    If v is the current velocity vector of the enemy, and p is the vector pointing towards the player, then your acceleration should be in the direction of a = v - p. As long as v and p vary smoothly, there will be no too large jumps in a (of course, if you find out during game execution that a changes too quickly, you can dynamically change the time step size).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Programming smooth x/y acceleration change to target a moving point
  1. Integral of y=x^x (Replies: 4)

  2. Differentiating y=x^x (Replies: 2)

  3. Suppose y=|x|/x (Replies: 3)

Loading...