View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

# General newbie question

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1cold_heats_.--.  Members

Posted 20 June 2012 - 10:44 AM

Hello there.I'm reading a book about physics and how to make your game look more realistic.It's a fascinating book, but even so, there are some things I dont understand yet.

Suppose I have a dot in a 2D cartesian plane, placed randomly. [Dot.x=2, Dot.y=3].It moves in a straight line with a constant speed/velocity.[const int speed = 5 ;// meters /seconds]
I'm imagining the game loop would look like this :
while(!game_over)
{
seconds++;
delete the dot;
dot.x+=speed;
redraw the dot;
}
I dont understand how exactly is the dot's animation done.At the second 0 the dot is in (2,3) and the second 2 it immediatly jumps at (7,3).Isnt that motion kind of unnatural?Its like, one moment he's here and the next moment he's suddenly far away, without me seeing how he actually "goes".
To help you understand better what I'm trying to say, l sketched the first and second iteration [or, how I think they should be ] :
Iter1:

Iter2:

I hope you understand.
So how can the game loop be altered to achieve that animation ?
Does it have something to do with the frame-rate ? I dont know much about this concept except that in one second, the user seex 40-60 frames.
The only background I have in game programming is simple console games and replicates after popular games [like snake, breakout, collapse, ].So this is a new territory for me .

Edited by cold_heats_.--., 20 June 2012 - 10:50 AM.

### #2ZeroBeat  Members

Posted 20 June 2012 - 11:00 AM

Hope what I write actually makes some sense:

The simplest of the simplest way to move the ball would probably be:

while(!game_over)
{
dot.x++;
drawball(dot.x, dot.y);
}
This way the x position will constantly increase while the game is running.
You don't delete the ball and then create it again. Only the position is changed.

Generally when things are drawn. They are actually done on a backbuffer(a 2D image) and then this image if flipped to the screen (what we see). This flipping thing happens 30/60 (or more) times depending on program/machine. And thus making it look like things are moving smoothly.

Next try to move the ball constantly while taking account time:
dot.x = dt * dot.x;
dt - the delta time change between frames.
The movement should be much nicer.....(at least it was when i wrote something like that)

To move to a position such as from 2,2, to 7,2. Psuedo code :

gameloop
{
if(ball has target)
{
move() * dt
}
}

where move would calculate 5,0

Just try to build something small and play around with the code. Hope this is somewhat useful hehe

Edited by ZeroBeat, 20 June 2012 - 11:22 AM.

### #3Serapth  Members

Posted 20 June 2012 - 11:05 AM

You want to modify your movement to take into effect how fast your game is running, so yes, the frame rate is a factor.

Lets say you want your dot to move .x at a rate of 100 pixels PER second.

Then you need to update your .x value by a fraction of 100 represented by your current frame rate..

So in your game loop, you need to keep track of how much time occured since the last time it was called. Call this delta.

Delta will represent the amount of time that has elapsed between your current frame and previous frame in (hopefully ) milliseconds. Now you need to figure out how much you want to move .x if you want a speed of 100 pixels per second, you first figure out your frame rate:

frameRate = 1000/delta;

This means that if you currently are taking 100 ms between game loop calls, you are running at 1000/100 or 10fps.

Now that you know you are running at 10 frames per second, if you want your pixels to move by 100 pixels per second, that means this frame, you want to move a total of 10% ( as at your current speed, you are going to update 10 times in a second) of 100, or in other words, by 10 pixels.

### #4GameCreator  Members

Posted 20 June 2012 - 04:25 PM

For the time being, I strongly suggest going simple. Something like this:

erasedot();
dot.x++;
drawdot();
Sleep(1000);  //  Makes the computer wait for 1 second


Now, this is the amateur, introductory way of doing things but you're more likely to understand this than anything else. After this, you can move on to timing.

Edited by GameCreator, 20 June 2012 - 04:27 PM.

### #5cold_heats_.--.  Members

Posted 21 June 2012 - 12:32 AM

For the time being, I strongly suggest going simple. Something like this:

erasedot();
dot.x++;
drawdot();
Sleep(1000);  //  Makes the computer wait for 1 second


Now, this is the amateur, introductory way of doing things but you're more likely to understand this than anything else. After this, you can move on to timing.

I do understand this though, I've done similar things like this in my console games, especially making the computer "lag" a bit .

### #6GameCreator  Members

Posted 22 June 2012 - 11:32 PM

Great. Then we can get a bit more specific. Which engine or library are you using?

### #7antiHUMANDesigns  Members

Posted 23 June 2012 - 06:19 PM

If you're on windows, I recommend using a function called timeGetTime() (it's good and easy), which will return the current time. If you check this every cycle of your game, and store it in a variable, you can check the difference between the time stored in your last cycle and the time of the current cycle to see how many milliseconds have passed, and then update the entire game based on that value.

Remember that pretty much everything is dependent on time, so use the time value for anything that happens in the game. One example where time gets interesting and important is when the main character has a machine gun, which fires faster than the framerate. If the gun needs to fire 4 bullet in one cycle, time will stil need to pass between each bullet, even though you're only calculating the time between each frame. If you don't calculate this correctly, the machinegun will fire 4 bullets at the same time, then wait until the next frame, and fire another 4 bullets, instead of seemingly firing them evenly.
The same thing goes for particle emitters, for example. And to get even more advanced, the dreaded collision detection and response.

Just do not settle with Sleep(x), unless you want to simulate some kind of old-school effect. Making the computer sleep during game play is as bad as it sounds.

One tip, if you want to play around, is to assume you will be running at 60fps (vsync'd), and therefore set the time per frame to 16ms, or 0.016 seconds. You can add a real timer later on. I tend to do this when I'm testing stuff, to avoid having to add and link a library when I know my compute rwill be running it at 60fps.

/Justice

### #8cold_heats_.--.  Members

Posted 24 June 2012 - 10:52 AM

Great. Then we can get a bit more specific. Which engine or library are you using?

Sorry for replying a bit late.Well , currently, I'm not using any engine or library .
I'm trying to learn SDL .

### #9GameCreator  Members

Posted 24 June 2012 - 02:19 PM

No prob. Check this out:
http://gameprogrammingtutorials.blogspot.com/2010/01/sdl-tutorial-series-part-5-dealing-with.html

Does that help or do you need a simpler explanation?

### #10cold_heats_.--.  Members

Posted 24 June 2012 - 04:43 PM

Wow, that really helped !
Saank you !

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.