I'm still working on my game, and I want a better movement algorithm than the current. It's a 2D tile based game, and the current algorithm works something like this:
VB Code:
'DestX and DestY refer to where the player clicked for the
'character to move, while CharX and CharY are the character's
'current position
Do Until CharX = DestX And CharY = DestY
If NewDest = True Then Exit Do
If DestX > CharX Then CharX = CharX + 1
If DestX < CharX Then CharX = CharX - 1
If DestY > CharY Then CharY = CharY + 1
If DestY < CharY Then CharY = CharY - 1
'draws the character in its new position
Call MoveCharacter(CharX, CharY)
Loop
That's no good because the character moves in a diagonal line until it reaches the same X or Y position as the destination, then moves in a straight line. I need the character to move in a diagonal line the entire time, heading towards the destination each turn.
My theory is this: Get the slope of the character's position and the destination's position each turn, and move the character according to the slope. This could work, correct? If not, then what's another theory? Thanks.
Here's my very first attempt (see next post). It needs work so if anyone can come up with a solution, it'd really help. Thanks.
Also -- I know that reading code is frustrating unless you're coding it yourself, so I've tried to comment it the best I can and included a quadrant diagram for those of you unfamiliar with geometry. Any questions, just ask.
OK I made a little sample...but I have to go to school no, so I didn't have time to comment the code....so if it is something you don't understand...just post again....I will be happy to answer. I love mathematics.....
I thoufgt about a thing, what I posted, is probably not perfect for what you want to do. Because, when the x- distance getting smaller and the y - distanse getting larger, the object would move faster. So you would have to try to calculate a movement per loop distance, that calculate how much it is going to move int x distanve compared to y distance....
I'll think of this a bit...but my code is a good start anyway...
What you call, "Elevation," I call, "Slope." They're the same thing.
Anyway, I've been working with this all day and can't figure out a way to make the character move at a constant speed in all directions. Anyone have any ideas?
I think I got the answer....I haven’t tested it that much, because it's late here in Norway, and I have to get up early tomorrow.....I got the idea when I watched "Son's of the beach" or what the ......... the thing is called on TV (can you believe somebody get paid to make TV like that). What I have done is nothing new....I have just done the same ting to the X coordinate as I did to the Y coordinate...so now I think the object would move at the same speed regardless of what distance. Now you have to use the GetTickCount API call to do the rest...
Check it out....and post again if it doesn't work...
No, actually there maybe one other thing to it....my code would probably make an object move slightly faster diagonally then it would do on a straight line...
But I have no idea how to fix that now, so I think I will slip on that one...
No no, I got it now for real. Still has a slight problem:
Try clicking around a bit, before the circle reaches it's destination... it shakes rapidly until the "queue" is empty. It tries to finish all of the destinations before the last, for some reason. If anyone has a solution for this one, it'd be very much appreciated. Thanks.
So you are using the exactly same method as I posted yesterday...but with other variable names and things like that. So you would have the same problem. The shape will move a little bit faster when it moves diagonally. Because of this two lines...
Because if the shape only moves in the X coordinates not in the Y as well, the total vector would be smaller then the total vector of the shape going diagonally.
But you can't see it in your app because it's only one shape moving at the same time....But if you go to this thread
...where I posted a small Arcade Game some weeks ago, you can see the lines moving down from the sky are using the same method as we did. And they have slightly different speed. The speed increases when the degree increases to 45 degrees.
But that might not be a problem, that depends on your game.
Oops, I didn't even look at your code yesterday. It's fine that it moves a little bit faster in certain directions, it doesn't need to be very precise.
Thanks NoteMe, it all works fine now. The only problem with your attachment was that when the velocity was greater than 1, it would shake when handling too many clicks. I fixed it, in case you were wondering (see attachment). Thanks for the help!
Actually, with your code, it still shivers on my computer. If I click N times before it reaches the end, it will shiver N times on the end. Because it won't end the loop in the mean time....
Nice. A slight modification and it was finished. I changed the part about Exiting if Length < 1 and Length > -1 to if Length < Velocity and Length > -Velocity. Thanks again.