# Object routing in game?

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
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.

Might I suggest a simple vector?

Vector
{
int x, y;
}

const Vector NORTH={0,-1};
const Vector SOUTH={0,1};
const Vector EAST={1,0};
const Vector WEST={-1,0};
const Vector STAND_STILL = { 0, 0 };

Vector facing;

// so moving the elks looks like:
elk[i].x += facing.x * ELK_SPEED;
elk[i].y += facing.y * ELK_SPEED;

// and setting direction becomes something like:
facing = EAST;

So hopefully it will be a little easier for you to check your bounding
conditions. For instance,

if ( elk[i].x < 0 ) // the east edge
{
elk[i].x = 0;
elk[i].facing = WEST; // go the other way.
}

I haven’t compiled this so I apologize for errors. Hope it helps.

lee>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

Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.
http://im.yahoo.com/

Let me amend that, EAST being the opposite of WEST…

if ( elk[i].x < 0 ) // the east edge
{
elk[i].x = 0;
elk[i].facing = EAST; // go the other way.
}

lee

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,
MattOn 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

Hey thanks, I adapted it to my code and it works well.

Thanks again.

— Matt Busigin wrote:> 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

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
``````

Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.