Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • This topic is locked This topic is locked
18 replies to this topic

#1 toony   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.

Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 15008

Like
2Likes
Like

Posted 30 June 2011 - 10:55 AM

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

#3 Nanoha   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;


#4 toony   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.

#5 ApochPiQ   Moderators   -  Reputation: 15008

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? ;-)

#6 toony   Members   -  Reputation: 80

Like
1Likes
Like

Posted 30 June 2011 - 11:34 AM

I dont want to teleport there! :D

#7 haegarr   Crossbones+   -  Reputation: 4166

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.

#8 toony   Members   -  Reputation: 80

Like
-4Likes
Like

Posted 30 June 2011 - 11:48 AM

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

#9 Nanoha   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.

#10 toony   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.

#11 Nanoha   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?

#12 BeerNutts   Crossbones+   -  Reputation: 2793

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)

#13 ApochPiQ   Moderators   -  Reputation: 15008

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.

#14 toony   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.

#15 BeerNutts   Crossbones+   -  Reputation: 2793

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)

#16 toony   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!";


#17 BeerNutts   Crossbones+   -  Reputation: 2793

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)

#18 toony   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.

#19 ApochPiQ   Moderators   -  Reputation: 15008

Like
5Likes
Like

Posted 30 June 2011 - 04:05 PM

That'll be enough of that.




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