Sign in to follow this  

Design question Making objects move at different speeds

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am drawing a 2d scene with various moving objects that I want to move at different speeds. My c++ program design looks like so right now: 1)main -initializes an engine object, tells screen dimensions, ip address, various other settings. -calls engine's run function -calls engine's cleanup (i.e when run stops, close network connections, free memory blah) 2)Graphics class -Handles all my opengl drawing right now (I want to create a seperate class for each object I create) 3)Networking class -Does my UDP networking 4)Engine class -Initializes an instance of the graphics class and passes a pointer to itself (i.e a pointer to engine) - This is where the gameloop lives and this is what I am having trouble with:
		
			int tick = SDL_GetTicks();

			if (tick <= gLastTick) // one frame every 100 milliseconds, IE 10 frames per second
			{
				/* set next frame as current frame */
				//gLastTick=tick;
				SDL_Delay(10);

			}
			else 
			{
				graphics->DrawGLScene();			
			}
			SDL_Event event;
			SDL_PollEvent(&event);
			switch(event.type) 
			{
			case SDL_QUIT:
				done = true;
				break;
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
				{
				case SDLK_ESCAPE:
					done = true;
					break;
				default:
					break;
				}
			}
                        //This is what causes delays 
			SDL_Delay(10);


Question: I want to be able to define the speed at which each of my objects moves at. I am eventually planning on making each of my objects a separate class. Could some one point me in the direction of a tutorial, provide some sudo code or some more information on what I am doing wrong. Thanks.

Share this post


Link to post
Share on other sites
Hi,

First you need to be more precise. What kind of objects are these. Are they somehow related etc etc.

Second, I dunno how you intended to move them at different speed but you won't need to change the code you posted. When you want something to move faster/slower it is not by changing the time between each frame that will do the trick. It is by the movement effectuated by your objects that will make them look go fast or slow.

Let's say you have 2 car right now, a BMW and a Honda. You want to make the BMW go faster than the Honda.

There is a lot of methods on how to do it but I'll explain it so you can understand.

First of all, both of these object are cars, so they will somehow have data/methods that are the sames.

So we need to centralize these in a base class. Lets call it CAR.
In the CAR class you have a int speed variable and a Draw() Function.

Now we will make two new class BMW and HONDA. Both of these car will inherits (Search Inheritanceon google for more information) from the base clas CAR. So now without any more code both of these class has a variable speed and a function draw. Now you want to make the BMW move fast, so in the Constructor you do speed = 100; and in Honda car speed = 50;

Now when you will draw, you will need to move these object in the Screen space. If two user press UP at the same time, the BMW should be double distance on the screen space than the honda.

Hope it answer your question, because I don't know at what level your struggling and what is your programmation level. If not, post it here ^^

Share this post


Link to post
Share on other sites
Quote:
Original post by deathwearer


Let's say you have 2 car right now, a BMW and a Honda. You want to make the BMW go faster than the Honda.

There is a lot of methods on how to do it but I'll explain it so you can understand.

First of all, both of these object are cars, so they will somehow have data/methods that are the sames.

So we need to centralize these in a base class. Lets call it CAR.
In the CAR class you have a int speed variable and a Draw() Function.

Now we will make two new class BMW and HONDA. Both of these car will inherits (Search Inheritanceon google for more information) from the base clas CAR. So now without any more code both of these class has a variable speed and a function draw. Now you want to make the BMW move fast, so in the Constructor you do speed = 100; and in Honda car speed = 50;

Now when you will draw, you will need to move these object in the Screen space. If two user press UP at the same time, the BMW should be double distance on the screen space than the honda.

Hope it answer your question, because I don't know at what level your struggling and what is your programmation level. If not, post it here


First off thank you very much for taking the time to reply and sorry if my question was a little vague.

At the moment I am trying to have a few objects (lets call them cars) move around in the background of my game at different speeds, I draw all of my objects inside of my graphics class like so:



int i; int sizeOfStrip = 8; float width;
for(i = 0; i <sizeOfStrip; i++)
{
DrawTopBoss(startFrom,(yPosition+1.76));
DrawSliders(startFrom, (yPosition+1.44));
DrawE(startFrom, (yPosition+1.19));
PlaceImage( startFrom, (yPosition+.40), 1);
startFrom = startFrom + widthStrip;
}






This for loop is executed within the graphics->DrawGLScene() function in engine. I eventually intend for DrawTopBoss, DrawSliders, DrawE to be all sub classes of a master class called unit (much like car in you're above example).


Anyway, each object: DrawTopBoss, DrawSliders, DrawE perform a different series of actions on the screen, I want each of these objects to go through their actions at different rates of speed. I am still not sure how I go about doing this. At present each object (i.e DrawTopBoss) is represented by a function in my graphics class, I intend to make these functions into seperate classes latero on, once I have a better idea of how game loops/graphics work.

Share this post


Link to post
Share on other sites
Ok I understand a litle bit more your problem now. Before starting to explain a partern to do such thing, may I ask you the reason(s) to draw them at different rate of speeds? What kind of result do you want doing so? Because there might be an easier solution than what you are trying to do.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathwearer
Ok I understand a litle bit more your problem now. Before starting to explain a partern to do such thing, may I ask you the reason(s) to draw them at different rate of speeds? What kind of result do you want doing so? Because there might be an easier solution than what you are trying to do.


Ok, well in the game a little ball has to move through a series of obstacles moving at different speed, like blocks that can push you, things that spin etc etc.

Share this post


Link to post
Share on other sites
Ok i get it. So basicly you don't have to DRAW them at different rate of speed, you need to MOVE them at different rate of speed.

If you current block of code you showed me up there works good beside that fact, don't touch it, it's fine.

Let's throw therical numbers here. Your ball move at 20px per second and you paddle at 5px per second. So your ball move faster obviously. All your animated object (For the drawing, I'm not talking about colision) need two function. Move() and Draw(). In your loop, before you do the Draw, you have to call Move() which will move the objects based on their speed, and when you call Draw() You transform your space position in screen position (If your gamemap/interface is bigger than the screen and you scroll around)

Hope you understand if you have any question, ask. ^^

Share this post


Link to post
Share on other sites
Thanks for all the help death weaver,you put me on the right path. Here is how I ended up solving the problem in the end; I created a master class called sprite, and derived different sprite classes for each of my enemies, in each class I setup a different frequency variable (frequency which they do stuff), and whenever I called there update position function I passed the current time. Updates only got performed if (currenttime - lastsavedtime) > frequency.

Is this a good way to go about doing things? Any feedback would be most appreciated and thanks once again for all the help.

Game loop from my Engine class:



getCurrentTime = SDL_GetTicks();
int incrementTime = 40;
getNextTime = getCurrentTime + incrementTime;
//SetupScene
graphics->SetUpGLScene();
while ( !done )
{


graphics->UpdateGLScene();
SDL_Event event;
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
done = true;
break;
default:
break;
}
}
SDL_Delay(100);
getNextTime += incrementTime;


}

}



Game loop from my Engine class:



Graphics::UpdateGLScene()
{
// Clear The Screen And The Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//testing.update(internalCounter);
int presentTick = SDL_GetTicks();
testing.draw(); // testing = different sprites
testing2.draw();
testing3.draw();
testing4.draw();
testing.UpdateSliderPositions(presentTick);
testing4.UpdateBallPosition(presentTick);
internalCounter++;
//DrawRectangle(0, 0);
SDL_GL_SwapBuffers();

}

Graphics::UpdateGLScene()
{
// Clear The Screen And The Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//testing.update(internalCounter);
int presentTick = SDL_GetTicks();
testing.draw();
testing2.draw();
testing3.draw();
testing4.draw();
testing.UpdateSliderPositions(presentTick);
testing4.UpdateBallPosition(presentTick);
internalCounter++;
//DrawRectangle(0, 0);
SDL_GL_SwapBuffers();

}



Ok now code from within one of my sprite objects:


//internalCOunter = passed output from sdl_getticks
void
Sliders::UpdateSliderPositions(int internalCounter)
{
int i;
if((internalCounter - lastUpdateCounter) > frequency)
{
//update variables which control what this sprite does
}
}

Share this post


Link to post
Share on other sites
The more common approach is to have your object have a position defined by floats and a velocity defined in terms of pixels per second.

Then wherever you update your objects, you pass in a float representing the proportion of a second that has elapsed since last frame.

You can then just add the object's speed times this delta to the position.

For example, given a float X position and a float XVelocity defined as the number of pixels per second the object moves, and a float Delta that is number of seconds since last frame (so would be, say, 0.04 at a 25 fps frame rate):


X+=XVelocity*Delta;


HTH

Share this post


Link to post
Share on other sites
I haven't look at all the code (I'm kinda busy at job with my own code :P) but that look pretty good. You just have to think that there is always 2 different step and It's updating the object (based on timer, triger, input, etc) and Draw these object after.

[Edited by - deathwearer on October 4, 2007 3:51:05 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
The more common approach is to have your object have a position defined by floats and a velocity defined in terms of pixels per second.

Then wherever you update your objects, you pass in a float representing the proportion of a second that has elapsed since last frame.

You can then just add the object's speed times this delta to the position.

For example, given a float X position and a float XVelocity defined as the number of pixels per second the object moves, and a float Delta that is number of seconds since last frame (so would be, say, 0.04 at a 25 fps frame rate):


X+=XVelocity*Delta;


HTH


This is what I was going to sugest at first, but I believe he wants to move object with fixed speed each X times. Exemple, Each 5 second a paddle move 10 px left.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this