Skip to main content
GameDev.net gamedev.net
🔒 Locked

[SOLVED!]enemy AI problems

Started by Shenjoku Sep 4, 2006 at 2:56 PM 11 replies 1.1k views
Original Post
Shenjoku
Shenjoku
ok I'm still working on my 2d sidescrolling game but I've got problems with the boss' AI script. Everything about movement works fine but his attack is the problem. What I'm trying to do is have him randomly shoot a fireball at the player's current location at the time of firing. I figured out an algorhytm for calculating the position and having it literally "warp" to that spot but I want it to slowly move there at a steady speed like 3 pixels per frame for example and I just can't get it to work. AT ALL. I've tried every variation of multiplying, dividing, adding, subtraction and I can't get anything to work. Any ideas? here's the code for that portion:

//enemies current declared variables
bossA.x=100;
bossA.y=100;
bossA.width=45;
bossA.height=40;
bossA.curframe=1;
bossA.lastframe=3;
bossA.animdelay=3;
bossA.animcount=0;
bossA.movex=3;
bossA.movey=3;
...
if(GotLocation!=true)
		{
			curPlayerx=player.x;
			curPlayery=player.y;
			GotLocation=true;
		}
		dist=GetDistance(curPlayerx,curPlayery,bossA.x,bossA.y);
		if(dist>0 && curPlayerx>bossA.x)
		{
			bossA.x+=curPlayerx + bossA.x;
			bossA.y+=curPlayery + bossA.y;
		}
		else if(dist>0 && curPlayerx<bossA.x)
		{
			bossA.x-=bossA.x - curPlayerx;
			bossA.y+=curPlayery + bossA.y;
		}
		if(bossA.x+bossA.width>=curPlayerx && bossA.x<=curPlayerx+player.width)
		{
			bossA.animdelay=4;
			bossA.width=75;
			bossA.height=80;
			enemyAttack_image->Release();
	enemyAttack_image=LoadTexture("explosion.bmp",D3DCOLOR_XRGB(0,255,0));
			bossA.lastframe=7;
			if(bossA.curframe==7)
			{
			enemyAttack_image->Release();
	enemyAttack_image=LoadTexture("BLANK.bmp",D3DCOLOR_XRGB(0,255,0));
			}
		}

//function for gauging distance needed for attacks in the BOSSAI()
int GetDistance(int playerx,int playery,int enemyx,int enemyy)
{
	int distance;
	distance = sqrt(pow((playerx-enemyx),2)+pow((playery-enemyy),2));
	return distance;
}




[Edited by - Shenjoku on September 5, 2006 8:45:43 PM]
Captain P
Captain P
Considered using a vector for the direction, and floats for the precision? So, writing a 2D vector containing two floats, x and y, which can be used for positions, directions, and other usefull purposes. I'd recommend reading up on vectors, it's definitely worth getting some insight in them.
Shenjoku
Shenjoku
I actually already have vectors declared for them I just never thought to use them for that...cus honestly I wouldn't know how x.x The teacher I currently have hasn't shown us a thing about vectors besides how to declare them and what they CAN be used for but not HOW. So do you have any links to places I could read up on it? :P
EDIT:crap I just noticed there is another thread for AI specifically -_- sorry guys, you can move this one over or whatever if you want.

[Edited by - Shenjoku on September 4, 2006 3:13:39 PM]
Captain P
Captain P
Well, quite simple, in pseudo-code:

----------
// variabeles
Vec2 fireball.movement, player.position, fireball.position;

// Determine movement direction
fireball.movement = player.position - fireball.position;

// Set movement speed, by setting the length of the movement vector
// to 1 and multiplying it with the speed we want
float speed = 5.0f;
fireball.movement = fireball.movement.unitvector() * speed;

// Now move each frame
fireball.position += fireball.movement;
----------

As for links, don't have one right here, but some Googling should provide you with some usefull articles. There's probably also some usefull stuff on it here on Gamedev and in the math board.
Shenjoku
Shenjoku
what does the unitvector() function do? I can't find it anywhere or figure it out. I pretty much getting the same results as before. it just warps to the position instead of moving there =/ and I think it's cus I can't get that part to work.
Captain P
Captain P
It sets the length of the vector to 1 - vectors with length 1 are called unit-vectors. This is done by dividing both the x and y variabele with the length of the vector. And yes, if you don't do this, you'll get warping fireballs. :)
Sirisian
Sirisian
setting a vector to length one is called normalizing. I've started using vectors in my programming too a while back. Works very well and organized things well.
Shenjoku
Shenjoku
ok I tried doing that but it just stays in the same place:

bossA.movex=curPlayerx-bossA.x; bossA.movey=curPlayery-bossA.y;

//where dist=the distance between the two objects
int speed=4.0;
bossA.movex= bossA.movex/dist * speed;
bossA.movey= bossA.movey/dist * speed;

bossA.x+=bossA.movex;
bossA.y+=bossA.movey;

yeah I know I'm not using a vector but this is the only thing that seems to give any results at all so far. I must be doing something wrong...without the middle part it warps but with it it just sits there at the point of creation and doesn't move.
Sirisian
Sirisian
use floats. if you don't it could mess up the numbers and round them to 0.

int(anything between 0 and 1) = 0.

Only thing I can think that would be causing this to happen.
Shenjoku
Shenjoku
changed em all to floats, still nothing =/ i'd do more but it's work time x.x
Captain P
Captain P
Quote:
Original post by Sirisian
setting a vector to length one is called normalizing.


Hmm, true. I just got working with normals (left-hand normal, right-hand), so I started to wonder it normalizing was the correct term. Thanks for reassuring. :)

What I'd suggest you to do, Shenjoku, is doing some debugging. Checking the actual values of these vectors when the problem occurs. What I often found usefull is drawing lines to indicate vectors - it's easy to spot which vector is causing problems, which in turn makes spotting the source of trouble easier.
Bytheway, have you changed the monsters position variabeles to floats as well? As for not using a vector, you're doing exactly the same you could do with a vector, but with twice as much code lines. A vector is just a wrapper for the x and y, making life easier. :)
Shenjoku
Shenjoku
aha! I found what the problem was :) simple problem with conflicting if statements heh ^^; after all that complicated stuff you would think I would notice something that simple hahaha. Oh well, now it works so it's all good. I got a full fledged boss :) Thanks a ton everybody for all the help. Couldn't have done it without yah :)
deadimp
deadimp
Suggestion: Don't load those textures run-time, unless "LoadTexture" simply returns from a list of pre-loaded textures.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.