Advertisement

how do I represent enemy movement?

Started by July 01, 2002 08:54 AM
6 comments, last by Jedyte 22 years, 4 months ago
Hi, I''m building a 2D shoot ''em up, and I''m stuck on the design of the enemies. Of course I want the enemies in my game to follow different patterns of movement (like straight down, wavering from left to right, zigzagging, ...) but I wouldn''t know how to represent and implement such a movement in code... These patterns are the same for the same kind of enemies, but they have to be dynamical in position (like an enemy on the left can follow the same pattern as one on the right, but their path is different because they start at different points). Suppose I want to know, each cycle of my gameloop, which position to move every enemy to next, how do I do that? (the same thing is a problem for attack patterns of bullets)
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan
This is a simple gunshot pattern in a game with the camera right from above. Maybe this will give you an idea on the enemy patterns.


    		for(char i = 1; i <= shots; i++)    {//Moves the bullet in the direction it originaly was fired at.		yShot[i-1] = (int)yShot[i-1]-(sin(shotangle[i])*8);		xShot[i-1] = (int)xShot[i-1]+(cos(shotangle[i])*8);		//Check if the bullet reached the one of the ends of the screen for example.		//If its true then remove the bullet and replace with the bullet at the end of the buffer.		if (xShot[i] < ScreenLeft || xShot[i] > ScreenRight || 					yShot[i] < ScreenUp || yShot[i] > ScreenDown)		{				yShot[i-1] = yShot[shots-1];				xShot[i-1] = xShot[shots-1];				shotangle[i-1] = shotangle[shots-1];				shots = shots - 1;				i--;		}		else		{		//Blt here...		}	}    


[edited by - Aram on July 1, 2002 11:15:32 AM]
True words are not always beautiful, beautiful words are not always truthful.
Advertisement
Thank you,
When exactly is this loop run? Does this calculate a path, or does it update every cycle? I don''t know what all the variables do, you see...

Another thing that I forgot to mention, I''m looking for a way to makes these patterns "pluggable". I mean provide a way, so I don''t have to hard-code every pattern into it''s enemy. (Maybe later I can even expand that to defining the patterns in external scripts.)
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan
Here''s a method that might work. Each enemy would need:

1)Start position (s)
2)time value (t)
3)Movement equation based on t

The movement can be any mathematical equation. For example, to move down the screen in a wavy motion, something like:

x = sin(t)
y = t*3

To get the position of an enemy at any frame, just put your t value into the equation, and add the start position for that enemy. t would need to increase by a small amount each frame (this is what changes the enemies positions).
For example, if t=5.6 and s=(100, 10), then

x = s.x + sin(t)
y = s.y + t*3

x = 100 + sin(5.6)
y = 10 + (5.6*3)

Next frame, t might equal 5.7, so the enemy would have moved a bit.

You may want to convert t between -PI and PI(radians) when (and only when) it''s being used in trig functions.
Each enemy would need a seperate t and s, but could share the same equation for movement. (the equation could be stored in a function, or loaded in from a script). The function way would be simpler to start out with, and would probably save you some work unless you have 100+ movement patterns.

Some C pseudo-code
void main(){   Enemy EnemyA;   InitEnemy(EnemyA);    //set s value and init t to 0   while(TRUE){      UpdateEnemy(EnemyA);      DrawEnemy(EnemyA);   }}void InitEnemy(Enemy &EnemyGuy){   Enemy.t = 0.0;   Enemy.s.x = 100;   Enemy.s.y = 0;}void UpdateEnemy(Enemy* EnemyGuy){   Position Start = EnemyGuy->s;  //these vars simplify equation   float t = EnemyGuy->t;   EnemyGuy->p.x = Start->x + sin(t);  //p is position   EnemyGuy->p.y = Start->x + t*3;   EnemyGuy->t += 0.1;} 

Sorry but I''m not realy good at explaining to others. Usualy I''m the only one that understand what I''m doing. But maybe you''ll get something out of this...


  char Bullets[100] = 0; //Count total bullets fired and currently "active".int xBullet[100], yBullet[100] = 0; //Coordinates of your bulletsfloat Bulletangle[100] = 0;//the angles of the bullets when they were fired. 						 //In which direction your gun pointed.  if(hit gunbutton){	Bullets++;	xBullet[0] = XCoordsSoldier;//Adjust theese so that they seem coming from the guns	yBullet[0] = YCoordsSoldier;}for(char i = 1; i <= shots; i++)    {	//Moves the bullet 8 pixels in the direction it originaly was fired at. 		yBullet[i-1] = (int)yBullet[i-1]-(sin(Bulletangle[i])*8);				xBullet[i-1] = (int)xBullet[i-1]+(cos(Bulletangle[i])*8);			//Check if the bullet reached the one of the ends of the screen for example.	//If its true then remove the bullet and replace with the bullet at the end of the bullet list.	//Like if you have 10 bullets and bullet number 4 has hit the screen boarder. 	//Then bullet number 10 would replace number 4 and decreases the bullet counter by one.  	if (xBullet[i] < ScreenLeft || xBullet[i] > ScreenRight || 					yBullet[i] < ScreenUp || yBullet[i] > ScreenDown)		{						yBullet[i-1] = yBullet[shots-1];						xBullet[i-1] = xBullet[shots-1];						Bulletangle[i-1] = Bulletangle[shots-1];					Bullets = Bullets - 1;						i--;			}	else			{				//Blt here...			}	}  
True words are not always beautiful, beautiful words are not always truthful.
I answered in your other post->Isometric formn.

,Jay
Advertisement
Ah, it''s a lot clearer now, tnx!

Thrump:
Is there a reason you let each enemy hold the time? Couldn''t you use _one_ central variable?

Thank you
Jedyte
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan
I meant for the t to be the relative time for each enemy. It would probably start at 0 whenever the enemy is first on-screen. Otherwise, if every enemy shared the same t, they all would be in the same place if they shared the same equation (and only offset by their initial position). All the enemies would look very well synced together (and is probably not wanted for most cases).

This topic is closed to new replies.

Advertisement