Archived

This topic is now archived and is closed to further replies.

I have searched and troubled but still no luck

This topic is 4981 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 have read almost every post on timing and have tried lots of different solutions with no luck. My problem I am done with my breakout clone and have been testing it out. I don't lock my frames rate because I end up having to move the ball too many pixels on slow machines, to have good game speed which ends up looking choppy. So I use the method of finding TimeDelta and using that along with velocity to update the speed. The problem I am having is that the speed changes on different machines. Another thing is that for it to be payable not to fast on my machine I end putting the velocity at 1 or .4 etc Here is where I get Timedelta which I call LastFrameTime
	FrameTime = timeGetTime();
	LastFrameTime = timeGetTime();
	
	while(Msg.message!=WM_QUIT && g_mES.isEmpty()==FALSE)	
		{
		FrameTime = (float)timeGetTime();

		if(PeekMessage(&Msg,NULL,0,0,PM_REMOVE)) {
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}else {
			g_mES.Process();													//process top engine

			}
		LastFrameTime =(float) timeGetTime() - FrameTime;
		}


   
     
Any problems with that? Here is how I update the paddle
   
pad->loc.x = (float) pad->loc.x -(1 * (LastFrameTime * 1.0f));
     
The only time my ball's x and y velocitys change speed is when they hit the paddle other wise they just get flipped if they hit somthing, so I use time delta only here and not every time the ball moves
 
balls[i]->X_velocity=  balls[i]->velocity * (cos(balls[0]->angle)* (LastFrameTime * 1.0f));				//change x's and y's velocity

balls[i]->Y_velocity=  balls[i]->velocity * (sin(balls[0]->angle)* (LastFrameTime * 1.0f));
						balls[i]->loc.x =(float) balls[i]->loc.x + balls[i]->X_velocity;				//update the balls location

balls[i]->loc.y =(float) balls[i]->loc.y + balls[i]->Y_velocity;				//and return

 
Sorry for the long post but I wanted to make sure there was enough info, I am pulling what hair I have left out. thanks for your help -Marc [edited by - dapeos on April 22, 2004 5:51:08 AM]

Share this post


Link to post
Share on other sites
Ok, the typical format for something like this would be as follows.


float mCurrentTime;
float mPreviousTime;
float mDelta;


/*MAIN LOOP BEGIN*/
while (gameloop == true)
{
// get the current time

mCurrentTime = TimeGetTime();


// calculate the time elapsed since the previous frame

float mDelta = mPreviousTime - mCurrentTime;


// do all update of sprites etc

// using mDelta (the time that

// has passed since the last frame)

mPlayer.locationx += (speed * mDelta);


//record the time of the frame we just did as the previous

mPreviousTime = mCurrentTime;
}
/*MAIN LOOP END*/



Be aware, that is a rough guide and not complete code. Furthermore your first mDelta value could be screwy as the mPrevious will not have been set. That can be counteracted with a quick call of mPreviousTime = TimeGetTime() before you enter that main loop.



Edit: typo and tidy



[edited by - ghosted on April 22, 2004 7:15:44 AM]

Share this post


Link to post
Share on other sites

float mCurrentTime;
float mPreviousTime;
float mDelta;

float padvel = 0;
float ballxvel = 0;
float vallyvel = 0;
float padspeed = 100; //pixels a second



/*MAIN LOOP BEGIN*/
while (gameloop == true)
{
// get the current time

mCurrentTime = TimeGetTime();

// calculate the time elapsed since the previous frame

float mDelta = mPreviousTime - mCurrentTime; //in ms


mDelta /= 1000; //in seconds


//record the time of the frame we just did as the previous

mPreviousTime = mCurrentTime;


if (gotpowerup)
padspeed += 50;

if (moveleft)
padvel = -padspeed
else if (moveright)
padvel = padspeed;
else
padvel = 0;

padx += padvel * mDelta; // pix per second * number of seconds

//thats past is the number of pixels its moved.


DrawPad(padx);
GetInput();

}
/*MAIN LOOP END*/


You just have to understand timing, before you can make a timer. I hope I dind''t misunderstand and annoy you with things you do know.

//Base code complements of ghosted.

Share this post


Link to post
Share on other sites
No not at all I thank you for the help, I have never touched on timers before so I eat up your info. Seems like what I was doing wrong was not dividing TimeDelta by 1000 stupid me.
It seems to work now haven''t tried it yet on another machine, but its not as smooth as it was when I just moving it 1 pixel at a time
the problem was slower comps couldn''t keep up.
So I want the paddle to make it across the sceen in 3 seconds I would use move 1280/3 = 427 to times against delta right?
that works and the paddle makes it across the screen in three but it is kindof jerky not bad but not great ether any ideas?
thanks again for your help
-Marc

Share this post


Link to post
Share on other sites
Still goes incredibly slow on roomates comp shouldn''t it look just choppy if his comp can''t run it but still be the same speed?

WinMain:

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
g_hInst=hInstance;
if(Init()==FALSE)
ShutDown();

cMenuEng *mMenuEng= new cMenuEng(); //create a menu engine

g_mES.Push(mMenuEng); //push it


LastFrameTime = timeGetTime();


while(Msg.message!=WM_QUIT && g_mES.isEmpty()==FALSE)
{
FrameTime = (float)timeGetTime();
DeltaTime =(float) LastFrameTime - FrameTime;
DeltaTime = DeltaTime / 1000 * -1;

if(PeekMessage(&Msg,NULL,0,0,PM_REMOVE)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}else {
g_mES.Process(); //process top engine

}
LastFrameTime = FrameTime;
}

ShutDown(); //shutdown game

return(Msg.wParam);
}



handle input:


void cGameEng::HandleInput()
{

g_mDI.ReadKey();
if(KeyState(DIK_LEFT) && pad->loc.x >= 25 ){ //move paddle left

if(balls[0]->velocity==0)
balls[0]->loc.x = (float) balls[0]->loc.x - (640 * (DeltaTime));
pad->loc.x = (float) pad->loc.x -(640 * (DeltaTime));
}
if(KeyState(DIK_RIGHT)&& (pad->loc.x +pad->width) <= 1255){ //move paddle right

if(balls[0]->velocity==0)
balls[0]->loc.x = (float) balls[0]->loc.x+ (640 * (DeltaTime));
pad->loc.x =(float) pad->loc.x + (640 * (DeltaTime));
}

if((balls[0]->Y_velocity==0) && KeyState(DIK_SPACE)) //launch ball

balls[0]->velocity = 200;

if(KeyState(DIK_ESCAPE)){ //exit game

ShowCursor(TRUE);
g_mES.Pop();
}
}


update ball collision dectection etc:

for(int i=0; i<3; i++){
if(balls[i]->active==TRUE){
if((balls[i]->loc.y + balls[i]->height) >= pad->loc.y && //check to see if ball hit paddle

balls[i]->loc.x + balls[i]->width >= pad->loc.x &&
balls[i]->loc.x <= pad->loc.x + pad->width &&
balls[i]->loc.y + balls[i]->height < pad->loc.y + pad->height)
{

double centerPad = pad->loc.x + pad->width/2;
double dfc = balls[i]->center.x - centerPad;
dfc = dfc * .83;
double angle = 90 - dfc;
balls[i]->angle = (angle * 3.14)/180;
balls[i]->angle = balls[i]->angle * -1; //negate the angle

balls[i]->X_velocity= balls[i]->velocity * (cos(balls[0]->angle)* (DeltaTime)); //change x''s and y''s velocity

balls[i]->Y_velocity= balls[i]->velocity * (sin(balls[0]->angle)* (DeltaTime));

if(balls[i]->velocity!=0) //play bounce sound

g_mDS.sound_fx[g_mDS.sound_pad_hit].dsbuffer->Play(0,0,0);

balls[i]->loc.x =(float) balls[i]->loc.x + balls[i]->X_velocity; //update the balls location

balls[i]->loc.y =(float) balls[i]->loc.y + balls[i]->Y_velocity; //and return

return;
}

int hit = mBM->collide(balls[i]);

if(hit!=0){ //hit a block


g_mDS.sound_fx[g_mDS.sound_bounce].dsbuffer->Play(0,0,0); //play sound


if(hit == 1)
balls[i]->Y_velocity = balls[i]->Y_velocity * -1;
else if (hit == 2)
balls[i]->X_velocity = balls[i]->X_velocity * -1;

balls[i]->loc.x =(float) balls[i]->loc.x + balls[i]->X_velocity; //update the balls location

balls[i]->loc.y =(float) balls[i]->loc.y + balls[i]->Y_velocity; //and return

return;
}

if(balls[i]->loc.y <= 25) //check borders

balls[i]->Y_velocity = balls[i]->Y_velocity * -1;
else if(balls[i]->loc.x <= 25)
balls[i]->X_velocity = balls[i]->X_velocity * -1;
else if(balls[i]->loc.x + balls[0]->width >= 1255)
balls[i]->X_velocity = balls[i]->X_velocity * -1;

balls[i]->loc.x =(float) balls[i]->loc.x + balls[i]->X_velocity; //update the balls location

balls[i]->loc.y =(float) balls[i]->loc.y + balls[i]->Y_velocity;

if( balls[i]->loc.y >1024) //ball fell thru

balls[i]->active = FALSE;
if(balls[0]->active==FALSE && balls[1]->active==FALSE && balls[2]->active==FALSE) //are all balls gone

reset();

}
}


Any ideas?
thanks again
-Marc

Share this post


Link to post
Share on other sites
The jerkyness is showing you the difference in the delta time''s. There are ways to limit the difference and catch back up in later frames. Search on higher resolution timers and see if you have a big function that gets called every so often providing a jerk. The faster you move and the longer the delta time''s determines the jerky level. Slow it (the paddle) down or speed you game up and it will solve it.

Share this post


Link to post
Share on other sites