# 2D Movement

This topic is 4112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello folks, this is a really generic codeing practice question pretend game engine description: blank white screen on the screen is a white 10x10 rectangle wherever i click it sets a destination coord to that (xDest, yDest) then the gameloop runs the check rectangle status function it notices that its current location is not the destination coord. so it modifies the rectangles xVelocity and yVelocity values accordingly. eventually the rectangle will reach the destination, and then reset the velocities to zero... assuming a velocity of +-1 at the end of the main game loop there is a delay function to ensure a steady and univerally constant game update speed of, lets say, 35ms. my question is - lets say this square represents a man, moving on the ground. and he has a gun and fires a bullet.... obviously the bullet needs to travel very fast. i can manage this by having the bullets velocities be +-10. that is to say, 10 pixel movement per game update (every 35ms) or even faster velocity +-50, +-100, etc depending on speed of the fast object. perhaps you know my problem. how then can i make this movement seem "smooth" atm its very jerky and ugly. im also having issues coming up with a good checking system for locations when my movement velocities are != +-1. my checks use a simple > or < check to see how it should move, and if i move past my destination via a large velocity. it will then give me the opposite movement velocity and the object will move back and forth infinitly. I have kinda solved this by creating a grid system for each object based off screen dimensions and movement speed, where every persons speed is required to be a factor of the screen width. then when you click it give you a grid coord specificaly based on that objects personalized grid.... and moves it accordingly (note, the grid movement would be based on intersection of the lines, not the boxes themselves) is this an effective method? doesnt sound like it. there must be an easy way. perhaps i can base the game around maxium speeds or percentages? so that my slow moving man has a speed coefficient of .05*screenwidth and my bullet has a speed coefficient of 1*screenwidth? i dont know to summarize. 1) how do i create smooth movement for fast moving objects w/out it seeming like jittery flashy updates. 2) is the grid method for movement effective? what are the standards for doing stuff like this. i could just remove the delay timer, but that means a user moves much faster on a faster machine, and possibly much too fast. thank you for your time, and i hope this made sense >_<

##### Share on other sites
pseudo code

main{     check input     if mouse click update destination coords          if location != destination update velocities accordingly     update location by velocities     delay by amount of time equal to 35ms - time requried to reach this point}

##### Share on other sites
I'm not sure I understood your question completely, but if fast objects are moving choppily, could it simply be an FPS issue? In other words, what if you decrease your 'delay' AND accordingly adjust the 'pixels-per-loop' speed of the objects to ensure that they still look like they're moving the same speed as they were before you changed the delay? Would that work? I mean, having such a HUGE delay, 35 ms, it's no surprise stuff would look choppy. Yeah, decrease it!

A standard delay for a game would be like 0.02 seconds, I'm guessing? So, make the delay like 20 ms or less and see if that improves the smoothness at all. And also, if you decrease your delay by a factor of 2, for example, make sure you also decrease your 'pixels-per-loop' speed of the objects by a factor of 2 as well, I think, haha!

Tell me if that helped at all, or if I was WAAY off the dot... You may also be talking about something else, that I'm having problems with at the moment as well. In my game, set at 60 frames per second, a ball moving at ANY pixels-per-frame speed (except for 1 ... haha), will occasionally look like it JUMPS couple pixels in front. It's really annoying, and if you're talking about THAT, then I have NO idea how to fix it. I think it's a part of all games, and you'll have to deal with it. Or apply some algorithm to counteract that rough movement. But that would just make your coding 10 times harder. Forget it. Just put your FPS to 200+ or something ... lol!

Good luck,

- Dalal

##### Share on other sites
Make movement time-based. You don't need to worry about keeping a constant frametime then. Because, as Dalal already said, the lower your fps, the choppier movement looks. Simply record the time it took a single frame to be processed, and multiply all movement speeds with it (you'll probably need to use different speeds now, but that's a one-time modification).

However, very fast moving objects will still look choppy. That's why many games render a bullet trail rather than an actually moving projectile. To check if the bullet hits anything, they simply trace a line and check if it collides with anything. Other people choose to let their bullets go slower. Which approach you take is up to you. ;)

##### Share on other sites
hi guys,
im just a hobbiest so im sorry if the question was a bit noobish.

i just want to clarify a bit
im making a 2d aventure game
and lets say i have a rabbit on speed hopping around in the background
at incredible speeds. i need to animate this so that it looks like a fluid cartoon.
i cant just have him appear in place to place.
lowering the time per tick would help
but i guess im still just worried about how to "transition" the character
from one location to the next.
maybe my animation should include a movement blur trail type thing.

i really like the idea of having every item(my base class) in the game have its own timer - instead of everything depending on the global timer.

i really appreciate your responses.. +ratings

##### Share on other sites
Quote:
 Original post by cwyersi just want to clarify a bitim making a 2d aventure gameand lets say i have a rabbit on speed hopping around in the backgroundat incredible speeds. i need to animate this so that it looks like a fluid cartoon.i cant just have him appear in place to place.lowering the time per tick would helpbut i guess im still just worried about how to "transition" the characterfrom one location to the next.maybe my animation should include a movement blur trail type thing.

That can work well, sure. Often, games that feature helicopters, use a blurred rotor image rather than a fast-rotating modelled rotor.

Quote:
 Original post by cwyersi really like the idea of having every item(my base class) in the game have its own timer - instead of everything depending on the global timer.

That's not what I said. I meant: make movement time-based, not frame-based. You should just let the game run as fast as it can, without building in some artificial delay to get constant frame-times. Every delay makes the game look more choppy. Giving objects different timers won't solve that.

Currently, your game runs at a constant fps. What if you change that value? Objects suddenly move faster, because their speed depends on how often they get updated, not on how much time has passed. Play an old DOS game and notice how unplayable fast some of them run. To prevent that, you should record the frametime, and multiply all movement with that value.

The result is that objects move at the same speed, regardless of the system they're running on. If, say, your rabbits should move 200 pixels per second, then they'll move 200 pixels per second, whether the game runs at 2 fps or 50 fps. At 2 fps, the frametime is 0.5 seconds. 0.5 * 200 = 100 -> the rabbits move 100 pixels per frame. At 50 fps, the frametime is 0.02 seconds. 0.02 * 200 = 4 -> the rabbits move at 4 pixels per frame. This shows that, the faster the game runs, the smoother the movement will look. It will look more choppy on slower systems, but that's ok: these systems are less powerfull in the first place. Of course, if your game is pretty heavy, then it'll look choppy on high-end systems too, and there are limits to how smooth things will look: the refresh rate of your monitor, for example.

##### Share on other sites
awesome!
thank you,
sorry i misunderstood.
i had juggled this idea in my head but couldnt really make sense of it.
im worried about the implications of developing something like this in a multiplayer environment....

thats not really relevant really....just something nagging my brain...
this wont be a multiplayer game

here is my concern...
i bench mark my game based on my computer...
then time my walking animation frames accordingly
then when someone gets on an incredibly fast computer
my equivlant of guybrush threepwood, or whatever, his animation is moving
slowly, compaired to his very fast velocity.

thats why i interpreted the above mentioned idea as specifically time every individual object's frame update.

i really REALLY appreciate your time discussing this with me, im sorry if my questions or thoughts are noobish ^.^

omgosh
calculate the frame rate for every computer at the start of the program....
wow im so freaking dumb....
no its just my crappy IT admin job draining my braincells along with my budget :(

[Edited by - cwyers on July 18, 2007 2:54:21 PM]

##### Share on other sites
Nope, don't calculate the frametime at the start. Do it every frame. Frametimes can fluctuate after all. One moment your game can be busy handling a chock-full screen, another moment may be relatively peacefull.

##### Share on other sites
dont meant to suck up.
but everytime you post here
its a great revelation in my mind

thanks so much

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 101
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009664
• ### Who's Online (See full list)

There are no registered users currently online

×