Results 1 to 22 of 22

Thread: Moving an object on a linear path? *RESOLVED*

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421

    Moving an object on a linear path? *RESOLVED*

    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:
    1. 'DestX and DestY refer to where the player clicked for the
    2. 'character to move, while CharX and CharY are the character's
    3. 'current position
    4.  
    5. Do Until CharX = DestX And CharY = DestY
    6.     If NewDest = True Then Exit Do
    7.    
    8.     If DestX > CharX Then CharX = CharX + 1
    9.     If DestX < CharX Then CharX = CharX - 1
    10.     If DestY > CharY Then CharY = CharY + 1
    11.     If DestY < CharY Then CharY = CharY - 1
    12.    
    13.     'draws the character in its new position
    14.     Call MoveCharacter(CharX, CharY)
    15. 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.
    Last edited by Oafo; Nov 6th, 2002 at 11:10 PM.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I suppose so... Remember that slope is (y2 - y1) / (x2 - x1).. you should be set. Let us know how it goes!
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    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.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    I hate it when that happens
    Attached Files Attached Files
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  5. #5
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Thats pure mathematic...you have to calculate the elevation, or what you call it in USA...

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Wink

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

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    By the way, there is a lot more you have to do with it, like check for zero division and stuff like that.....

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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...

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    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?

    Thanks for the code NoteMe, I'm working on it
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  10. #10
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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...

  11. #11
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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...

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    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.
    Attached Files Attached Files
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  13. #13
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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...

    CharX = CharX + (XAmt * Velocity)
    CharY = CharY + (YAmt * Velocity)

    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

    Opinions on my game

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

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    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.

    As for the shaking, do you have a solution?
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  15. #15
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I'm not 100% sure on this one...but I tryed this...

    Do Until DistX > (-1) And DistX < 1 Or DistY > (-1) And DistY < 1
    DistX = DestX - CharX
    DistY = DestY - CharY

    Length = Sqr((DistX ^ 2) + (DistY ^ 2))
    XAmt = DistX / Length
    YAmt = DistY / Length

    CharX = CharX + (XAmt * Velocity)
    CharY = CharY + (YAmt * Velocity)

    DrawChar
    lCount = lCount + 1
    DoEvents
    Loop


    ...loop instead. It gets rid of nearly all the shivering. But it's not perfect. There should be a better way.

  16. #16
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Smile

    This is the way to do it....

  17. #17
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    I havent looked at any of the code posted (attachments are annoying), but this should be what you are looking for.

    http://www.vbforums.com/showthread.p...hreadid=209840

    Z.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    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!
    Attached Files Attached Files
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  19. #19
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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....

  20. #20
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    SOLVED. I changed the code, so if it is at the end. It will exit the loop. Take a look at this....
    Attached Files Attached Files

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    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.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  22. #22
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yes of course that would be better...I have learned so much by helping you out, so I probably should say thanks to you too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width