# Moving my character...

This topic is 3862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Greetings all, I am trying to build a game engine but I can't seem to work out how to move my character around the way I want. What I'm after is the character moving to where I have clicked my mouse so if the char is at 0, 0 (it's a 2D game btw) and I click on 10, 10 then the character moves to 10, 10 at a pre-determined speed, like 10. The trouble is that if I click on 200, 100 then it moves diagonally up until it comes to 100, 100 and then moves along until it hits 200, 100. This obviously isn’t any good. I realize that the off-diagonal means that it travels along faster than up but I can't seem to set my C++ code to do so and retain the 10 movement speed. I’ve tried moving it along at 10 and slowing down the up movement by finding how many up must occur to every along (i.e. 2 along to every 1 up) but my code just doesn’t seem to work. Anyone got any ideas by any chance? Cheers. /Ludo

##### Share on other sites
For 2D look up the Bresenham line algorithm. It should do what you want.

##### Share on other sites
Funny thing, i just worked out how to do it before I checked here.

if ( dbMouseClick ( ) == 1 )
{
if ( Once == 1 )
{
if ( dbObjectPositionX ( 2 ) < dbObjectPositionX ( 1 ) ) { One = dbObjectPositionX ( 1 ) - dbObjectPositionX ( 2 ) ; } // 100
if ( dbObjectPositionY ( 2 ) < dbObjectPositionY ( 1 ) ) { Two = dbObjectPositionY ( 1 ) - dbObjectPositionY ( 2 ) ; } // 50

Three = ( One / 10 ) ; // X Moves - 10
Four = ( Two / 10 ) ; // Y Moves - 5

if ( Three > Four ) { Five = Three / Four ; } // - 10 / 5 = 2
if ( Three < Four ) { Five = Four / Three ; } //

if ( Three > Four ) { Six = 10 / Five ; } else { Six = 10 ; } //
if ( Three < Four ) { Seven = 10 / Five ; } else { Seven = 10 ; } // 10

Once = 0 ;
}

if ( dbObjectPositionX ( 2 ) < dbObjectPositionX ( 1 ) ) { dbPositionObject ( 1, dbObjectPositionX ( 1 )-Seven, dbObjectPositionY ( 1 ), dbObjectPositionZ ( 1 ) ) ; }
//if ( dbObjectPositionX ( 2 ) > dbObjectPositionX ( 1 ) ) { dbPositionObject ( 1, dbObjectPositionX ( 1 )+10, dbObjectPositionY ( 1 ), dbObjectPositionZ ( 1 ) ) ; }

if ( dbObjectPositionY ( 2 ) < dbObjectPositionY ( 1 ) ) { dbPositionObject ( 1, dbObjectPositionX ( 1 ), dbObjectPositionY ( 1 )-Six, dbObjectPositionZ ( 1 ) ) ; }
//if ( dbObjectPositionY ( 2 ) > dbObjectPositionY ( 1 ) ) { dbPositionObject ( 1, dbObjectPositionX ( 1 ), dbObjectPositionY ( 1 )+10, dbObjectPositionZ ( 1 ) ) ; }
}

Thanks anyway. ^_^

##### Share on other sites
Or you simply use vectors

pseudo:
function MoveTo( X, Y )  vector2d direction = ( X - posX, Y - posY )  normalize( direction )    while not at destination     posX += direction.x * speed;     posY += direction.y * speed;

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 9
• ### Forum Statistics

• Total Topics
634085
• Total Posts
3015407
×