I'm making a game using love2d's physics module and I currently have some AI units that are supposed to try to fly into the player. They and the player have the same movement system where the player controls dx and dy values bounded by -1 and 1, when using a joystick or using a keyboard - this then defines the angle the player tries to move at and the player's movement is the same speed in any direction. The enemies simply calculate the angle between where they are and where the player is and fly in that direction, whatever the dx and dy values need to be.
This would work fine if gravity didn't exist, but it does now so the enemies undershoot. How do I adjust their aim so that they can take into account the effect of the gravity on the physics world?
Thanks
Smart AI under gravity
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Smart AI under gravity
If a module has a constant acceleration a = (a_x, a_y), with initial speed v = (v_x, v_y), and initial position r_0 = (r_0x, r_0y), then its position at time t is given by
r(t) = (r_x(t), r_y(t)) = t^2 * a / 2 + t * v + r_0.
(This directly comes from integrating twice the accelaration, taking into account the constants of integration that are initial speed and position.)
In your case, I assume the gravity is given by g = (0, -c) (for c > 0 a constant), then a = g and you have
r_x(t) = v_x * t + r_0x
r_y(t) = -t^2 * c / 2 + v_y * t + r_0y.
Now, you need to find v_x and v_y such that these two equations have a solution for t (you will have infinite choices for v_x and v_y, but probably you should have constraint on the norm of the speed). Let's imagine that the norm of your speed is v. Then v_x = v cos(alpha) and v_y = v sin(alpha), where alpha is the angle between the speed and the horizontal axis. If you express t = (x - x_0) / (v cos(alpha)), then you end up have to solve this equation
-c(x-x_0)^2/2v^2 X^2 + X(x-x_0) - (y - y_0) - c(c-c_0)^2 / 2v^2 = 0,
where X = tan(alpha). This is a second degree equation for X, that you can solve easily (you will get constraint on v in order for the solutions to exist). Then you recover alpha by taking arctangent.
If you need more help on how to proceed for the last steps, let us know.
r(t) = (r_x(t), r_y(t)) = t^2 * a / 2 + t * v + r_0.
(This directly comes from integrating twice the accelaration, taking into account the constants of integration that are initial speed and position.)
In your case, I assume the gravity is given by g = (0, -c) (for c > 0 a constant), then a = g and you have
r_x(t) = v_x * t + r_0x
r_y(t) = -t^2 * c / 2 + v_y * t + r_0y.
Now, you need to find v_x and v_y such that these two equations have a solution for t (you will have infinite choices for v_x and v_y, but probably you should have constraint on the norm of the speed). Let's imagine that the norm of your speed is v. Then v_x = v cos(alpha) and v_y = v sin(alpha), where alpha is the angle between the speed and the horizontal axis. If you express t = (x - x_0) / (v cos(alpha)), then you end up have to solve this equation
-c(x-x_0)^2/2v^2 X^2 + X(x-x_0) - (y - y_0) - c(c-c_0)^2 / 2v^2 = 0,
where X = tan(alpha). This is a second degree equation for X, that you can solve easily (you will get constraint on v in order for the solutions to exist). Then you recover alpha by taking arctangent.
If you need more help on how to proceed for the last steps, let us know.
Re: Smart AI under gravity
I have attached a demo which uses a form of position prediction.
It's not perfect, and there's a comment stating a few ways it could be improved, but it's a good start.
Hope it helps!
It's not perfect, and there's a comment stating a few ways it could be improved, but it's a good start.
Hope it helps!
- Attachments
-
- Target.love
- (1.92 KiB) Downloaded 141 times
-
- Citizen
- Posts: 87
- Joined: Tue Dec 30, 2014 6:07 pm
Re: Smart AI under gravity
I'm not quite sure I follow sherpal. The enemies in the game control the force they apply to themselves each frame - speed has no explicit limits as such, the only restraint to stop insane speeds is some linear damping applied each frame. The gravity is indeed a constant. I'm not sure what you mean by norm of speed though?
Tjakka5 is the moveSpeed value the speed in which the player/enemy is moving in a particular direction in your example?
If it helps this is the game footage showing the enemies https://www.youtube.com/watch?v=KrPoJXToKtE
Tjakka5 is the moveSpeed value the speed in which the player/enemy is moving in a particular direction in your example?
If it helps this is the game footage showing the enemies https://www.youtube.com/watch?v=KrPoJXToKtE
Re: Smart AI under gravity
The moveSpeed value is indeed the speed at which the object's move in the direction they are facing.
At line 85, 86 is where is calculated where the player will be in x seconds. Where x is the amount of time it would take the enemy to get to the player's current position.
These 2 lines could be adapted to take other forces in account, such as wind.
At line 85, 86 is where is calculated where the player will be in x seconds. Where x is the amount of time it would take the enemy to get to the player's current position.
These 2 lines could be adapted to take other forces in account, such as wind.
Who is online
Users browsing this forum: Bing [Bot], Brotein and 4 guests