StartX to DestX...
StartX to DestX...
i can only provide psuedocode because im using freebasic.
i have a 640x480 pixel-perfect-pixel scroller :)
it uses tiles (16x16). the map is 1000 tiles by 1000 tiles
(layer 1 is anyway. havent made any other layers)
i have been able to place my NPCs at a certain pixel location on the tile map. however, NPCs do not move tile*tile. they move pixel*pixel.
right now, the algorithm i use to move an NPC from StartX to DestX is
if StartX < DestX then StartX = StartX - 1
if StartX < DestX then StartX = StartX - 1
if StartY > DestY then StartY = StartY + 1
if StartY > DestY then StartY = StartY + 1
however, this makes my NPC move in a diagonal line toward the nearest xy axis, then move in a straight line toward the final pixel.
like this.
xxxxxxxxxxxxxxxxxxxxxxx
x ooooooooooooooooooX
x o
x o
x o
x o
i need it to move in a straight line...
i have a 640x480 pixel-perfect-pixel scroller :)
it uses tiles (16x16). the map is 1000 tiles by 1000 tiles
(layer 1 is anyway. havent made any other layers)
i have been able to place my NPCs at a certain pixel location on the tile map. however, NPCs do not move tile*tile. they move pixel*pixel.
right now, the algorithm i use to move an NPC from StartX to DestX is
if StartX < DestX then StartX = StartX - 1
if StartX < DestX then StartX = StartX - 1
if StartY > DestY then StartY = StartY + 1
if StartY > DestY then StartY = StartY + 1
however, this makes my NPC move in a diagonal line toward the nearest xy axis, then move in a straight line toward the final pixel.
like this.
xxxxxxxxxxxxxxxxxxxxxxx
x ooooooooooooooooooX
x o
x o
x o
x o
i need it to move in a straight line...
hmm... your method will work if the slope of the line from start to dest is 1, but it won't work right any other time.
What you need to do is find the slope of the line and have him move accordingly.
If the slope is 3/1 then he goes up 3 on the y axis, then over 1 on the x axis. This process is repeated until whatever is being moved reaches it's destination point.
the formula for slope is: Ystart-Ydest / Xstart-Xdest
Hope that helps you! Just implement the slope formula into your code and things will move exactly like they should. Aaaand, thanks to your question I just realized I knew how to do something I thought I couldn't do.
Good Luck!
~Dave~
edit: More about the slope formula...
Keep the difference of Xstart and Xdest and the difference of Ystart-Ydest seperate, you don't really need to divide them unless the fraction can be reduced(do something in your program to reduce the fraction as much as possible for the most natural looking movement), they form the fraction of Rise over Run. rise
..............................................................run
Just had to clarify so I don't screw your code up.
What you need to do is find the slope of the line and have him move accordingly.
If the slope is 3/1 then he goes up 3 on the y axis, then over 1 on the x axis. This process is repeated until whatever is being moved reaches it's destination point.
the formula for slope is: Ystart-Ydest / Xstart-Xdest
Hope that helps you! Just implement the slope formula into your code and things will move exactly like they should. Aaaand, thanks to your question I just realized I knew how to do something I thought I couldn't do.
Good Luck!
~Dave~
edit: More about the slope formula...
Keep the difference of Xstart and Xdest and the difference of Ystart-Ydest seperate, you don't really need to divide them unless the fraction can be reduced(do something in your program to reduce the fraction as much as possible for the most natural looking movement), they form the fraction of Rise over Run. rise
..............................................................run
Just had to clarify so I don't screw your code up.
Last edited by DaveUnit on Sun Nov 27, 2005 2:36 pm, edited 2 times in total.
May I suggest something else to you?
If you haven't already, get mouse support, It'll make moving so much easier for the player so rather than making the player hold down arrow keys for a long time to get somewhere, just let the player click, and have the character move to that position on the screen!
Just a little suggestion to make your game more user friendly.
~Dave~
If you haven't already, get mouse support, It'll make moving so much easier for the player so rather than making the player hold down arrow keys for a long time to get somewhere, just let the player click, and have the character move to that position on the screen!
Just a little suggestion to make your game more user friendly.
~Dave~
Dave was right, but there's a faster way to do it. Its called Bresenham's line algorithm. It moves faster due to the use of integers(faster than floating point numbers(numbers with digits past the decimal point)). It looks like this:
This way is also more accurate then the other way. Have fun.
Code: Select all
SCREEN 12
DO
x1% = INT(RND * 640): y1% = INT(RND * 480)
x2% = INT(RND * 640): y2% = INT(RND * 480)
IF x1% > x2% THEN
XDir% = -1
xd% = x1% - x2%
ELSE
XDir% = 1
xd% = x2% - x1%
END IF
IF y1% > y2% THEN
YDir% = -1
yd% = y1% - y2%
ELSE
YDir% = 1
yd% = y2% - y1%
END IF
x% = x1%: y% = y1%
PSET (x%, y%), 15
IF xd% > yd% THEN
Mc% = 2 * yd% - xd%
Ma% = Mc% + xd%
Mr% = Mc% - xd%
DO
IF x% = x2% THEN EXIT DO
x% = x% + XDir%
IF Mc% < 0 THEN
Mc% = Mc% + Ma%
ELSE
Mc% = Mc% + Mr%
y% = y% + YDir%
END IF
PSET (x%, y%), 15
LOOP
ELSE
Mc% = 2 * xd% - yd%
Ma% = Mc% + yd%
Mr% = Mc% - yd%
DO
IF y% = y2% THEN EXIT DO
y% = y% + YDir%
IF Mc% < 0 THEN
Mc% = Mc% + Ma%
ELSE
Mc% = Mc% + Mr%
x% = x% + XDir%
END IF
PSET (x%, y%), 15
LOOP
END IF
LOOP
Last edited by Zamaster on Tue Nov 29, 2005 4:39 pm, edited 1 time in total.
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN
C:\DOS\RUN
RUN\DOS\RUN
thanks for all the advice. ill fool around when i get the chance (baby on the way this week...). sounds like bresenhams line is what i need because i also plan to modify the line in certain ways, for instance having it follow a curve. (for some sort of flanking manuever or maybe for the Magic Missles...)
PS, love that sig zam.
and deleter... what? i am going to need some advanced pathfinding (my units are 5-10 pixels at the base and they need to go between trees that are a random amount of pixels apart... oh boy)
i have no idea what you are trying to say... if i need pathfinding... *?:)?
PS, love that sig zam.
and deleter... what? i am going to need some advanced pathfinding (my units are 5-10 pixels at the base and they need to go between trees that are a random amount of pixels apart... oh boy)
i have no idea what you are trying to say... if i need pathfinding... *?:)?
And you will come to find that we are all one mind, capable of all that's imagined and all conceivable - Maynard
pathfinding = finding the shortest path when it ISNT a line because there are obstacles. Since you need this you ought to consider A*. If you have a tile based world, A* rocks, if not, either A* or Dijkstra's algorithim should be good for node stuff. If none of that makes sense then:
http://en.wikipedia.org/wiki/A%2A is a good start on A*,
http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm for Dijkstra's algo
http://en.wikipedia.org/wiki/A%2A is a good start on A*,
http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm for Dijkstra's algo