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

Click For Summary
SUMMARY

The discussion focuses on programming smooth x/y acceleration changes for an enemy character in a game, specifically targeting a moving player. The user is implementing physics-based movement by adjusting thrust based on the angle to the player and calculating the enemy's velocity using vector mathematics. Key challenges include preventing overshooting the player and ensuring smooth transitions between velocity vectors. The suggested approach involves using the current velocity vector and the target vector towards the player to calculate acceleration, ensuring that changes in acceleration remain gradual.

PREREQUISITES
  • Understanding of vector mathematics in game development
  • Familiarity with physics-based movement principles
  • Knowledge of programming concepts related to game AI
  • Experience with game engines that support physics calculations
NEXT STEPS
  • Implement vector interpolation techniques for smooth transitions
  • Explore the use of PID controllers for velocity adjustments
  • Research game physics libraries that facilitate smooth movement
  • Learn about predictive algorithms for targeting moving objects
USEFUL FOR

Game developers, particularly those working on AI behavior and physics-based movement systems, will benefit from this discussion.

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
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 14 ·
Replies
14
Views
2K