Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Functions for 2D projectile physics AI

Author
Message
lower logic
17
Years of Service
User Offline
Joined: 15th Jun 2006
Location:
Posted: 5th Jan 2008 04:38 Edited at: 5th Jan 2008 05:11
This code snippet shows 4 projectile functions I made that could be used by AI player in Worms-like or Scorched Earth-like games:


PRJ_Calculate_Minimum_Launch_Velocity() calculates the minimum amount of velocity with which a projectile much be shot in order to hit the specified target. Note that the starting position and the target don't have to be at the same height. accely# would be -9.8 or whatever you use for gravity. If you simulate wind as a constant horizontal acceleration, you can pass that in as accelx#.

PRJ_Calculate_Launch_Angle() calculates the angle the projectile must be launched at with the given velocity in order to hit the target. vel# is the launch velocity (it must be at least as large as the minimum launch velocity). The last parameter, flag, can be either 0 or 1. If flag is 0, it will return the angle that creates the higher trajectory, else it will return the angle for the lower, more direct trajectory.

The other 2 functions calculate the launch velocity and angle for a given amount of time. This would be handy if you wanted an AI to throw a grenade that takes, say, 3 seconds to explode, these functions would find the trajectory to the target that takes exactly 3 seconds.

In the example below it shows the trajectory with the minimum launch velocity (in green) as well as 10 other trajectories for 5 other launch velocities greater than the minimum. The red trajectory uses the last 2 functions, so an object launched along the path of the red trajectory would always take 14 seconds to reach the target.

Using these functions you should be able to make an AI that tries the trajectory of minimum velocity first, and if that intersects the landscape, tries increasing the velocity until it finds a working trajectory.

To compile you will need D3DFunc installed.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 5th Jan 2008 17:20
That should come in extremelly handy to some people! Great word, again

Your signature has been erased by a mod
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Jan 2008 11:42
Hmm, these could help me cheat at that old QB game, Gorillas!


Login to post a reply

Server time is: 2024-05-18 01:58:11
Your offset time is: 2024-05-18 01:58:11