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

Click For Summary
The discussion focuses on programming smooth acceleration for an enemy character in a game that chases the player. The current approach involves calculating independent x and y velocities based on the player's position, but the enemy tends to overshoot due to lack of a max speed limit. The goal is to have the enemy continuously adjust its velocity towards the player without predicting their future position. A proposed solution involves using two vectors: the enemy's current velocity and the target direction towards the player, with acceleration calculated as the difference between these vectors. The discussion emphasizes the need for smooth transitions in thrust to enhance gameplay dynamics.
xenotron
Messages
1
Reaction score
0
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.
 
Physics news on Phys.org
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).
 

Similar threads

Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
1K
  • · Replies 37 ·
2
Replies
37
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 13 ·
Replies
13
Views
465
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K