StartX to DestX...

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Guest

StartX to DestX...

Post by Guest »

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

Post by Guest »

sigh... for some reason it takes out the spaces...

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X..........oooooooooooooooooooooooooooooooooDestX
X.........o
X........o
X.......o
X......o
X.....o
X....o
X...o
X..o
X.o
XStartX
X
X

... that is how the diagram should look...
User avatar
Pete
Site Admin
Posts: 887
Joined: Sun Dec 07, 2003 9:10 pm
Location: Candor, NY
Contact:

Post by Pete »

If you put your diagram in CODE and /CODE (insert the [ ] brackets, of course), it will maintain the spacing.
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

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. :D
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.
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

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~
Guest

Post by Guest »

i do have mouse support. i even have buttons :)

yeah, the problem i was having was with both my camera and my units. but i can just apply the solution to both. thanks.
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

I'm glad I was of some help to you.
You're the first person I've ever been able to help with programming troubles. :D
It's a good feeling.
Good luck on your game!

~Dave~
User avatar
Zamaster
Veteran
Posts: 174
Joined: Wed Jun 15, 2005 1:51 pm

Post by Zamaster »

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:

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
This way is also more accurate then the other way. Have fun.
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
User avatar
Deleter
Veteran
Posts: 119
Joined: Sat May 07, 2005 7:31 pm

Post by Deleter »

and if you want any form of pathfinding i'd suggest a*... 8)
User avatar
Halifax
Coder
Posts: 21
Joined: Thu Apr 07, 2005 4:20 pm
Location: utah

Post by Halifax »

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... *?:)?
And you will come to find that we are all one mind, capable of all that's imagined and all conceivable - Maynard
User avatar
Deleter
Veteran
Posts: 119
Joined: Sat May 07, 2005 7:31 pm

Post by Deleter »

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
Post Reply