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

In summary, the individual is seeking advice on how to calculate a smooth transition of thrust between two vectors for their enemy character in a game. They currently have a system in place using angles and velocities, but would like to incorporate proper physics and have the enemy constantly adjust its velocity towards the player. They are looking for tips, formulas, or questions to help them achieve this.
  • #1
xenotron
1
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
  • #2
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).
 

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

1. What is the purpose of programming smooth acceleration to a moving point?

The purpose of programming smooth acceleration to a moving point is to achieve a more natural and fluid movement of the point towards its target. This can improve the user experience, especially in applications that involve animation or user interaction.

2. How does smooth acceleration differ from linear acceleration?

Smooth acceleration involves gradually increasing or decreasing the speed of movement, while linear acceleration involves constant and immediate changes in speed. Smooth acceleration can result in a more realistic and visually pleasing movement, while linear acceleration can be jarring and abrupt.

3. What factors should be considered when programming smooth acceleration?

When programming smooth acceleration, factors such as the starting speed, target speed, and maximum acceleration should be considered. The distance to the target and any obstacles that may affect the movement should also be taken into account.

4. How can smooth acceleration be implemented in programming?

Smooth acceleration can be implemented using algorithms that calculate the necessary changes in speed over time. This can involve using mathematical equations, such as the Pythagorean theorem, to determine the distance and speed of the point at different points in time.

5. What are the potential challenges in programming smooth acceleration?

Some potential challenges in programming smooth acceleration include accurately predicting the movement of the point, accounting for unforeseen obstacles or changes in the environment, and finding a balance between smoothness and efficiency. It may also require extensive testing and tweaking to achieve the desired results.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
Replies
3
Views
1K
Replies
2
Views
1K
  • Differential Geometry
Replies
3
Views
2K
Replies
8
Views
1K
Replies
8
Views
3K
Replies
17
Views
2K
  • Classical Physics
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
9
Views
1K
Back
Top