Sign in to follow this  

Moving my character...

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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;



Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this