Calculating how much to move to some position

Started by
17 comments, last by ApochPiQ 12 years, 9 months ago
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.
Advertisement
I really don't follow your math here.

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

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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;

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

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.
If all you want is to arrive at a destination, why not just do:

new_position = destination;

And be done? ;-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I dont want to teleport there! :D
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.

I really don't follow your math here.

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...
new_position = old_position + (speed * (target_position - old_position))
step1: new_position = 100 + (1.0f * (338-100))
step2: = new_position = 338
Now im flying![/quote]


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.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

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.

This topic is closed to new replies.

Advertisement