• Advertisement
Sign in to follow this  

Calculating how much to move to some position

This topic is 2395 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

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:
[code]
// 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]));
}
[/code]

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 [url="http://www.mediafire.com/?z662k7e6c26fi8b"]this[/url] program to see what the code does.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Its a bit hard to get whats going on, somehting liek this is easier to follow:

[code]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];[/code]

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

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Err ...[quote name='toony' timestamp='1309449007' post='4829628']
...
[code]
// 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]));
}
[/code]
[/quote]
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.

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1309452901' post='4829649']
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...
[/quote]

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

Share this post


Link to post
Share on other sites
[quote]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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='toony' timestamp='1309456095' post='4829685']

So much useful info in this forum! mostly from the mod team...
[/quote]

You ask for help, then berate the guys trying to help you. That's called being a dick.

Share this post


Link to post
Share on other sites
People are trying to [i]help[/i] 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.

Share this post


Link to post
Share on other sites
[quote name='Nanoha' timestamp='1309459373' post='4829711']
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?[/quote]
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.


[quote name='ApochPiQ' timestamp='1309459723' post='4829714']
People are trying to [i]help[/i] 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.[/quote]
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.

Share this post


Link to post
Share on other sites
[quote name='toony' timestamp='1309458997' post='4829708']
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.
[/quote]

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),
[code]

// 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;

[/code]

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

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


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

[/code]

But, this is just silliness. Continue using what you know is the right way.

Share this post


Link to post
Share on other sites
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.
[code]orig[...] += "everything on this side is the speed!"; [/code]

Share this post


Link to post
Share on other sites
[quote name='toony' timestamp='1309462636' post='4829741']
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.
[/quote]

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.


Share this post


Link to post
Share on other sites
[font="Courier New"][size="4"]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. [/size][/font]

Share this post


Link to post
Share on other sites

This topic is 2395 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.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement