[SDL] Object routing in game?

Matt Busigin cow at amber.org.uk
Fri Sep 22 13:51:33 PDT 2000


On Fri, Sep 22, 2000 at 10:49:12AM -0700, GEM Products wrote:
> how might I be able to optimize this so I can make it
> move a little then stop then move again, then stop.
> Ay help is appreciated!
> 
> /* Moves target in memory, needs an AI */
> 
> if (huntmode == 1) {
>    /* Move the elk */
>   for ( i=0; i<MAX_ELK; ++i ) {
>      if ( elk[i].alive ) {
>        elk[i].x += elk[i].facing*ELK_SPEED;
>           if ( elk[i].x < 0 ) {
>              elk[i].x = 0;
>              elk[i].y += elk[i].image->h;
>              elk[i].facing = 1;
>         } else
>      if ( elk[i].x >=(screen->w-elk[i].image->w) ) {
>      elk[i].x =(screen->w-elk[i].image->w)-1;
>      elk[i].y += elk[i].image->h;
>      elk[i].facing = -1;
>                                 }
>                         }
>                     }
> 
>                 }
> It moves it in memory, and I draw it later. But
> it seems like if I touch this code even a little, 
> and I know I'm doing it wrong, the movement won't 
> work anymore. thanks in advance...........


Like the other response to this said, you should probably use vectors;  But
use a tiny bit of simple trig.


struct elk_s
{
	/*
	 * Elk things ..
	 */
	int x, y;
	float direction;
	float speed;
};



/* 
 * Moves target in memory, needs an AI 
 */

if ( huntmode == 1 )
{
	for ( i = 0; i < MAX_ELK; ++ i )
	{
		if ( !elk[i].alive )
			continue;
		
		elk[i].x += (int) ( elk[i].speed * sin(elk[i].direction) );
		elk[i].y += (int) ( elk[i].speed * cos(elk[i].direction) );
	}
}



			Cheers,
				Matt



More information about the SDL mailing list