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] - function for calculating AI aim

Author
Message
noobnerd
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 3rd Oct 2011 11:02 Edited at: 3rd Oct 2011 11:02
Hello there

Here is a function i made ( with some help from neuro fuzzy )

what it does is that it calculates the x,y coordinates where the AI should Aim so that it will hit the target if it continues to move att a constant speed in the same direction.

Another use is to draw a cross to where the player should aim to hit the enemy. In for example a space shooter. ( remember freelancer anyone? )

It can also easily be modified for 3D

the first one is the one meant for the AI
the second one draws a cross ( for the player )

in both functions x1#/y1# are the coordinates of the target
xv#/yv# is the x and y velocity of the target ( easily obtained with vectors )
x2#/y2# are the coordinates of the shooter
v# is the bullet velocity of the shooter

the returned value d# is the distance in turns to when the collision will happen.
with d# we easily get the coordinates like this :

cx# = x1#+xv#*d# : cy# = y1#+yv#*d#
where cx#/cy# are the coordinates where the ai should aim.

Rapidrory
14
Years of Service
User Offline
Joined: 21st Aug 2009
Location:
Posted: 15th Oct 2011 01:22 Edited at: 15th Oct 2011 01:25
Very nice I tried it out and it work's really well. I'm posting the demo code i wrote for it so people can get a feel of how it works:



Steer with arrow keys, click on the white cross, and you should hit the red guy pretty much every time... Might make a game out of this demo at some point :L Thanks for the code!

There is no such thing as "Too Fast!"
noobnerd
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 16th Oct 2011 18:08
im happy that you found it usefull

thanks also for posting a demo code

Login to post a reply

Server time is: 2024-03-29 06:13:54
Your offset time is: 2024-03-29 06:13:54