Calculating how much to move to some position

Started by
17 comments, last by ApochPiQ 12 years, 9 months ago
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?

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

Advertisement


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)

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.

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


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.

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)

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!";

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)

[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. [/font]
That'll be enough of that.

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

This topic is closed to new replies.

Advertisement