• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

# Calculating how much to move to some position

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

18 replies to this topic

### #1toony  Members   -  Reputation: 80

Like
0Likes
Like

Posted 30 June 2011 - 09:50 AM

I wrote some lines of code to calculate the exact number of pixels needed to increment the position of moving objects (each time around the game loop) in order to get the object to some "x, y" position like "224, 329".

Here is my aproach:
// float orig[2], dest[2]; // [0] = x, [1] = y positions

// path calculation
if (dest[0] >= dest[1]) {
orig[0] += dest[0] / (dest[1] + (dest[0]-dest[1]));
orig[1] += dest[1] / dest[0];
} else {
orig[0] += dest[0] / dest[1];
orig[1] += dest[1] / (dest[0] + (dest[1] - dest[0]));
}


Is this approach clear enough and efficient in your opinion? The code isn't 100%, it's just a glimpse of the full code but this part is the juicy part !!

This code would be used to move a player by clicking on the ground where you want to move, like you see in games like baldurs gate for example.Try this program to see what the code does.

### #2ApochPiQ  Moderators   -  Reputation: 10412

Like
2Likes
Like

Posted 30 June 2011 - 10:55 AM

The typical formula is this:

new_position = old_position + (speed * (target_position - old_position))

Repeat on X, Y, Z axes.

Your math seems to exhibit weirdness, and mixes axes, which I honestly can't unravel...
Maker of Machinery

### #3Nanoha  Members   -  Reputation: 296

Like
0Likes
Like

Posted 30 June 2011 - 11:00 AM

Its a bit hard to get whats going on, somehting liek this is easier to follow:

float dir[2];
dir[0] = dest[0] - orig[0]
dir[1] = dest[1] - orig[1];

// You could clamp them to limit speed or something
dir[0] = clamp(dir[0], -1, 1); // max or 1 pixel in x direction
dir[1] = clamp(dir[1], -1, 1); // max or 1 pixel in y direction
// obviously you can now go faster diagonally

orig[0] += dir[0];
orig[1] += dir[1];

I'd suggest implementing a basic Vector class with a few functions, subtraction, addition, scaling and normalising. Then you can do:

float maxSpeed = speedPerSecond * time; // time since last frame
Vector dir = dest - orig;
dir.normalize();
dir *= maxSpeed;
orig += dir;

### #4toony  Members   -  Reputation: 80

Like
0Likes
Like

Posted 30 June 2011 - 11:30 AM

i find it strange that to me your code looks hard to understand. In my code i'm just trying to perfectly move something to another position. And speed/velocity variables contain values that will normaly move ahead of the destination i need. I'll take a closer look at your codes and see where that gets me. And my code might seem weird, but it works and for some reason my code looks easy to figure out for me.

### #5ApochPiQ  Moderators   -  Reputation: 10412

Like
2Likes
Like

Posted 30 June 2011 - 11:33 AM

If all you want is to arrive at a destination, why not just do:

new_position = destination;

And be done? ;-)
Maker of Machinery

### #6toony  Members   -  Reputation: 80

Like
1Likes
Like

Posted 30 June 2011 - 11:34 AM

I dont want to teleport there!

### #7haegarr  Crossbones+   -  Reputation: 2340

Like
0Likes
Like

Posted 30 June 2011 - 11:39 AM

Err ...

...

// float orig[2], dest[2]; // [0] = x, [1] = y positions

// path calculation
if (dest[0] >= dest[1]) {
orig[0] += dest[0] / (dest[1] + (dest[0]-dest[1]));
orig[1] += dest[1] / dest[0];
} else {
orig[0] += dest[0] / dest[1];
orig[1] += dest[1] / (dest[0] + (dest[1] - dest[0]));
}


orig[0] + dest[0] / (dest[1] + (dest[0]-dest[1]))
= orig[0] + dest[0] / (dest[1] + dest[0] - dest[1])
= orig[0] + dest[0] / (dest[0])
= orig[0] + 1
and similarly
orig[1] + dest[1] / (dest[0] + (dest[1] - dest[0]))
= orig[1] + 1

Hence this is not "clear code". Perhaps it is efficient, but only if the compiler is smart enough to find the shorter version.

### #8toony  Members   -  Reputation: 80

Like
-4Likes
Like

Posted 30 June 2011 - 11:48 AM

The typical formula is this:

new_position = old_position + (speed * (target_position - old_position))

Repeat on X, Y, Z axes.

Your math seems to exhibit weirdness, and mixes axes, which I honestly can't unravel...

new_position = old_position + (speed * (target_position - old_position))
step1: new_position = 100 + (1.0f * (338-100))
step2: = new_position = 338
Now im flying!

So much useful info in this forum! mostly from the mod team...

### #9Nanoha  Members   -  Reputation: 296

Like
1Likes
Like

Posted 30 June 2011 - 12:25 PM

new_position = old_position + (speed * (target_position - old_position))
step1: new_position = 100 + (1.0f * (338-100))
step2: = new_position = 338
Now im flying!

target_position - old_position needs to be normalized before scaling for speed, it'll end up being eithe -1, 1 or 0 which will work out ok with speed. It makes more sense if these are vectors rather than just numbers.

new_position = old_position + (speed * normalize(target_position - old_position))
step1: new_position = 100 + (1.0f * normalize(338-100))
step2: new_position = 100 + (1.0f * normalize(238))
step3: new_position = 100 + (1.0f * 1))
step4: = new_position = 101

if speed was 10, you'd be at 110 now which makes sense.

### #10toony  Members   -  Reputation: 80

Like
-3Likes
Like

Posted 30 June 2011 - 12:36 PM

Ok the code will cap the number to the nearest -1 to 1, but the code will cause the object to move in zigzag form. Thats why my method is important to me. Well i have to have some sort of a divider variable to make sure the object moves in a strait line. I must have something that holds the number of increments it will take to get to the desired destination.I already have it working just wanted to see other approaches, not silliness.

### #11Nanoha  Members   -  Reputation: 296

Like
1Likes
Like

Posted 30 June 2011 - 12:42 PM

So you you only wanted to move 1 tile at a time down only one axis at a time (the longest axis)? Or have I misunderstood?

### #12BeerNutts  Crossbones+   -  Reputation: 1890

Like
2Likes
Like

Posted 30 June 2011 - 12:46 PM

So much useful info in this forum! mostly from the mod team...

You ask for help, then berate the guys trying to help you. That's called being a dick.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #13ApochPiQ  Moderators   -  Reputation: 10412

Like
1Likes
Like

Posted 30 June 2011 - 12:48 PM

People are trying to help you here. Being dismissive, rude, and calling it "silliness" is not really polite behaviour. It sure as hell doesn't make me interested in helping you any further.
Maker of Machinery

### #14toony  Members   -  Reputation: 80

Like
-3Likes
Like

Posted 30 June 2011 - 12:58 PM

So you you only wanted to move 1 tile at a time down only one axis at a time (the longest axis)? Or have I misunderstood?

No, i wanted to move from lets say my position (x: 100, y: 136) to (x: 330, y: 542) "not instantly" in a straight line. try the linked program i provided on this topic and you know what im trying to do.

People are trying to help you here. Being dismissive, rude, and calling it "silliness" is not really polite behaviour. It sure as hell doesn't make me interested in helping you any further.

Fair enough, but i dont call providing wrong information help (i call it silliness)... My first message was fairly clear to understand, i also provided a working program example of my goal.

### #15BeerNutts  Crossbones+   -  Reputation: 1890

Like
0Likes
Like

Posted 30 June 2011 - 01:10 PM

Ok the code will cap the number to the nearest -1 to 1, but the code will cause the object to move in zigzag form. Thats why my method is important to me. Well i have to have some sort of a divider variable to make sure the object moves in a strait line. I must have something that holds the number of increments it will take to get to the desired destination.I already have it working just wanted to see other approaches, not silliness.

Your code is crap, you have no speed component (except 1 pixel per frame). The object's actual speed, in your code, is dependent on the speed of the computer it is running on, unless you're capping the framerate of your game.

You should have something like one of these methods (general methods, to get point across),

// variables are floats, CurrentLocation and DestinationLocation are points (with an x and y component)

// Get the distance between 2 points
Distance = GetDistance(CurrentLocation, DestinationLocation);

// compute the time it would take the object to get there based on it's current speed (in pixel's per second)
Time = Distance/Speed;

SpeedX = (DestinationLocation.x - CurrentLocation.x)/Time;
SpeedY = (DestinationLocation.y - CurrentLocation.y)/Time;

// move my current location
CurrentLocation.x += SpeedX;
CurrentLocation.y += SpeedY;



That's the easy, conceptual, way. The proper way is to use trig functions:

Angle = atan2((DestinationLocation.y - CurrentLocation.y), (DestinationLocation.x - CurrentLocation.x)) * 180 / PI;

CurrentLocation.x += cos(Angle) * Speed;
CurrentLocation.y += sin(Angle) * Speed;



But, this is just silliness. Continue using what you know is the right way.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #16toony  Members   -  Reputation: 80

Like
-3Likes
Like

Posted 30 June 2011 - 01:37 PM

Let me guess, Distance = DestinationLocation.x - CurrentLocation.x + DestinationLocation.y - CurrentLocation.y? Cant check the code if i dont know what's going on inside GetDistance(). My code shows you up front how i calculate the speed.
orig[...] += "everything on this side is the speed!";

### #17BeerNutts  Crossbones+   -  Reputation: 1890

Like
0Likes
Like

Posted 30 June 2011 - 01:41 PM

Let me guess, Distance = DestinationLocation.x - CurrentLocation.x + DestinationLocation.y - CurrentLocation.y? Cant check the code if i dont know what's going on inside GetDistance(). My code shows you up front how i calculate the speed.

No, wrong guess.

Have you had your 9th grade geometry class yet? Heard of pythagorean theorem? A^2 + B^2 = C^2? You want C, where A is (DestX - OrigX), B is (DestY - OrigY). There are more efficient ways of calculating distance besides having to do a square root, but a smart guy like should've known them all.

And, again, your "speed" is always 1 pixel per frame or less. You can't control the speed of different objects with your code. What will you do for a "fast" object, versus a "slow" object.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #18toony  Members   -  Reputation: 80

Like
-10Likes
Like

Posted 30 June 2011 - 03:49 PM

A^2 + B^2 = C^2 doesnt make u smart, not by a long shot star (read star in revers order)! How about you get your lilprick outurass and into your ignorant mouth then smell your breath. That alone should give you more entertainment than the bs u post on random topics. Thanks for the thumbs down, too bad they didnt program the forum to allow rates below 0! Like i give a pile of shit about some pricks rating me down. It's simple you guys are a bunch of woofters and have no life, only the idiotic crap like rating random people down and providing wrong info.

### #19ApochPiQ  Moderators   -  Reputation: 10412

Like
5Likes
Like

Posted 30 June 2011 - 04:05 PM

That'll be enough of that.
Maker of Machinery

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS