basic artificial inteligence (game bots)??

Feb 24, 2011 at 5:38pm
I am making a rotational space shooter that involves two human players flying around in an infinite 2d world shooting up enemy space ships (bots). I will have 2 virtual cameras and make it a split screen setup. I have done bots before, but not anything as complex as this. Any ideas how I could create bots that will follow the players and point in the right direction? I don't know how I could detect if the bots are pointing towards the players or not. Any ideas would be very helpful.
Last edited on Feb 24, 2011 at 5:38pm
Feb 24, 2011 at 5:54pm
Given two points you can get the direction with some basic trigonometry.

θ = atan ⎛ Δy ⎞
         ⎝ Δx ⎠
Feb 24, 2011 at 8:44pm
Haha I made a particle system like that a year or two ago.. it was beautiful

The arctan method is flawless for directional stuff, I was young and rebellious so I decided to make my own following function... while it didn't actually follow the goal perfectly it made for some interesting random misfires and sort of made me feel like my particles had character =)

aaaaanyway what i'm trying to get at with the above vague story is perfect bots are boring, spice them up with some mistakes and they will be well loved.

On another note, what library are you using for graphics and control and such? OpenGL, SDL, SFML, platform specific stuff?
Feb 24, 2011 at 11:46pm
I am using Allegro for graphics, sounds, user input, etc.
Bazzy, that formula would be used to calculate the direction in radians right?
Feb 25, 2011 at 12:16am
Any specific reason?
(I've used allegro, sdl, sfml, and a titch of opengl) of the graphics libraries I liked allegro the least...
Feb 25, 2011 at 1:09am
Given two vectors you can know if they point to the same direction with the cross product u X v = 0
Feb 25, 2011 at 1:34am
I use Allegro because it is the only graphics library I currently know. I find it to be straightforward and easy to use.
ne555, your talking about comparing one objects direction to anothers right? I need something that can take two points, and tell one point which direction it needs to go to get to the other point. I think Bazzy's formula does this. I will graph it out on paper and see.
Feb 25, 2011 at 1:49am
Well you may want to look into vectors, rather than having instant rotation you can have inertial rotation, which is an awful lot cooler than simple direction finding.

Also why graph it? Why not simply run a program implementing the functionality?

It is fairly straightforward, but so are the rest. Alright done with my input, good luck.
Feb 25, 2011 at 2:16am
I have the formulas working on paper, but I am having trouble with math.h.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <math.h>
#include <iostream>

#define pie 3.14159265

using namespace std;

int px, py, bx, by, dx, dy;
double angle;

int main()
{
    cout << "Type player X: ";
    cin >> px;
    cout << "Type player Y: ";
    cin >> py;
    cout << "Type bot X: ";
    cin >> bx;
    cout << "Type bot Y: ";
    cin >> by;
    
    dx = px-bx;
    dy = py-by;
    angle = atan(dy/dx) * 180 / pie;
    
    cout << dy << endl << dx << endl;
    cout << "The bot has to change its heading " << angle << " degrees to face the player" << endl;
    system("pause");
    return 0;}


What am I doing wrong? It works fine on a calculator for me.
Feb 25, 2011 at 2:49am
Nevermind. I needed to use atan2 instead of atan. Thanks for the help guys!
Feb 25, 2011 at 5:45pm
The problem was that dy/dx is a truncated integer division.
Once you get the angle ( θ in the equation above ) you can make still make smooth/random turns by slowly changing the bot direction instead of setting θ as the new one. There's no need for vectors.
Cartesian coordinates + local polar velocity should be quite simple to implement
Topic archived. No new replies allowed.