Irrational Movement

Things are moving way too fast . . . .:/

Page 1 of 1

8 Replies - 554 Views - Last Post: 27 August 2010 - 05:12 PM Rate Topic: -----

#1 PoiXen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-September 09

Irrational Movement

Posted 27 August 2010 - 01:28 PM

Hey guys.

Im trying to get a sprite to move up when the user clicks a button.

Below is the code I have so far.

What happens: When the user clicks, the image moves like lightning and just appears at the top of the screen.

What I want: For the image to move slowly and smoothly.

I think its one of 2 reasons why its moving so fast.
1.the math is wrong
2.I am using the wrong loop for the situation

I have put the deltaTime in there if you think that will help some way.


Thankyou in advance. Ill give you a green + if you know the answer.

Application::input(float deltaTime)
{
	posX = pInput->mousePositionX();
	bFire = pInput->mouseDown();
	if(bFire)
	{
		do
		{
			posY -= 0.0001;  // the current pos is at 420
		}while(posY > 0);
	}
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Irrational Movement

#2 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 50
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Irrational Movement

Posted 27 August 2010 - 02:11 PM

Quote

do
{
   posY -= 0.0001;  // the current pos is at 420
}while(posY > 0); 


Instead, use

while (posY > 0)
{
   posY -= 0.0001;
   Sleep(30);
}



Include Windows.h
Was This Post Helpful? 0
  • +
  • -

#3 PoiXen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-September 09

Re: Irrational Movement

Posted 27 August 2010 - 03:11 PM

View PostSplatocaster, on 27 August 2010 - 01:11 PM, said:

Quote

do
{
   posY -= 0.0001;  // the current pos is at 420
}while(posY > 0); 


Instead, use

while (posY > 0)
{
   posY -= 0.0001;
   Sleep(30);
}



This didnt work. The Sleep(30) makes the game freeze. I am not sure if I want to use Windows.h as it is a platfrom specific call. Do you know any math I could use instaed to make the image move slowly?

Just a guess, but, the bool bFire is only true for a fraction of a second. Has that got something to do with the speed at which the image travels?

I am making a version of space invaders. So that is a good thing to think about. I am trying to program the bullet to travel up, slowly.

Any other ideas?
Was This Post Helpful? 0
  • +
  • -

#4 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 50
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Irrational Movement

Posted 27 August 2010 - 03:16 PM

Please show declaration for posY.

If it is a float or double, make sure you have a 'f' at the end of "0.0001".
posY -= 0.0001f;


If there is a type mismatch, it might be subtracting an amount other than you think.


As long as this function is on the same thread as the rest of the program, anything that slows the movement will also slow the game. You can either subtract a smaller amount or subtract every so and so frames

This post has been edited by Splatocaster: 27 August 2010 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Irrational Movement

Posted 27 August 2010 - 03:30 PM

I get the sense that you haven't really established how the function works within the larger context of the program. Consider:

Application::input(float deltaTime)

Please explain to me the role of deltaTime.
Was This Post Helpful? 0
  • +
  • -

#6 PoiXen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-September 09

Re: Irrational Movement

Posted 27 August 2010 - 03:42 PM

View PostSplatocaster, on 27 August 2010 - 02:16 PM, said:

Please show declaration for posY.

If it is a float or double, make sure you have a 'f' at the end of "0.0001".
posY -= 0.0001f;


If there is a type mismatch, it might be subtracting an amount other than you think.


As long as this function is on the same thread as the rest of the program, anything that slows the movement will also slow the game. You can either subtract a smaller amount or subtract every so and so frames



in the header:

float posY; 



initialise in constructor:

 posY = 420.0f; 



The render code:

 Application::render() const
{
	pArt[0]->render(0, 0, 1.000000); //background
	pArt[21]->render(posX + 25, posY, 1.000000); // pew pew!
	pArt[11]->render(posX, 400, 1.000000); // ship
	
} 



and lastly the code form earlier:

 Application::input(float deltaTime)
{
	//player movement
	posX = pInput->mousePositionX();
	
	//player fire
	bFire = pInput->mouseDown();
	if(bFire)
	{
		while(posY > 0)
		{
			posY -= 0.00001f;
			//Sleep(30);    <----- makes game freeze, so, commented out
		}
	}
} 




Hope this helps uncover the problem.
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Irrational Movement

Posted 27 August 2010 - 03:46 PM

What does your program's main loop look like?
Was This Post Helpful? 0
  • +
  • -

#8 PoiXen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-September 09

Re: Irrational Movement

Posted 27 August 2010 - 04:44 PM

View PostOler1s, on 27 August 2010 - 02:46 PM, said:

What does your program's main loop look like?



MAIN:

#include "Application.h"
#include "..\3rdParty\System.h"
#include "..\3rdParty\Input.h"
#include <SDL.h>

int
main(int argc, char* argv[])
{
	argc = argc;
	argv = argv;

	System::init();

	Application app;

	SDL_Event event;
	Uint32 oldTicks = SDL_GetTicks();

	bool leftKey = false;
	bool rightKey = false;
	bool upKey = false;
	bool downKey = false;
	bool fireKey = false;	
	bool continueUpdating = true;
	do
 	{
		SDL_Delay(10);
		const Uint32 ticksNow = SDL_GetTicks();
		const float deltaTime = (float) (ticksNow - oldTicks) / 1000.0f;
		oldTicks = ticksNow;
		continueUpdating = app.update(deltaTime);
		if (!continueUpdating)
			break;
		System::clear();
		app.render();
		System::flip();

		Input::setMouseMotion(0, 0);
		Input::setMouseDown(false);

		while (SDL_PollEvent(&event))
		{
	        switch (event.type) 
			{
            case SDL_MOUSEMOTION:
				Input::setMouseMotion(event.motion.xrel, event.motion.yrel);
				Input::setMousePosition(event.motion.x, event.motion.y);
                break;
            case SDL_MOUSEBUTTONDOWN:
				Input::setMouseDown((event.button.button != 0));
                break;
			case SDL_KEYDOWN:
			case SDL_KEYUP:
				switch (event.key.keysym.sym)
				{
				case SDLK_LEFT:
					leftKey = (event.type == SDL_KEYDOWN);
					break;
				case SDLK_RIGHT:
					rightKey = (event.type == SDL_KEYDOWN);
					break;
				case SDLK_UP:
					upKey = (event.type == SDL_KEYDOWN);
					break;
				case SDLK_DOWN:
					downKey = (event.type == SDL_KEYDOWN);
					break;
				case SDLK_LCTRL:
					fireKey = (event.type == SDL_KEYDOWN);
					break;
				case SDLK_ESCAPE:
					if (event.type == SDL_KEYDOWN)
					{
						continueUpdating = false;
					}
				}
				Input::setKeys(leftKey, rightKey, upKey, downKey, fireKey);
			break;
            case SDL_QUIT:
				continueUpdating = false;
                break;
			}
       }
	} while (continueUpdating);

	return 0;
}
 


THE APPLICATION CODE:
#include "Application.h"
#include "..\3rdParty\System.h"
#include <Windows.h>

Application::Application()
{
	//game objects
	pFx    = new Sound();
	pArt   = new Image *[22];
	pInput = new Input();

	//x and y location of player ship
	posX = 320;
	posY = 420.0f;
	
	//flags
	bGameStart   = true;
	bFire        = false;

	//load in the background
	pArt[0] = new Image();
	pArt[0]->loadBmp("backdrop.bmp");
	pArt[21] = new Image();
	pArt[21]->loadBmp("shot.bmp");

	//temp char array
	char cFileName[255];	
		
	//this loop loads all the ship images into the pArt pointer
	for (int i = 1; i < 20; i ++)
	{
		sprintf(cFileName, "defender_%03d.bmp", i);
		std::string filename(cFileName);

		pArt[i] = new Image();
		pArt[i]->loadBmp(filename);
	}
}

Application::~Application()
{
}

bool
Application::update(float deltaTime)
{
	deltaTime = deltaTime;

	render();	
	sound ();
	input (deltaTime);

	//return false to quit
	return true;
}

void
Application::render() const
{
	pArt[0]->render(0, 0, 1.000000); //background
	pArt[21]->render(posX + 25, posY, 1.000000); // pew pew!
	pArt[11]->render(posX, 400, 1.000000); // ship
	
}

void 
Application::sound()
{ 
	if(bGameStart)
	{
		pFx->loadWav("voice_get_ready.wav");
		pFx->play();
		bGameStart = false;
	}
	if(bFire)
	{
		pFx->loadWav("player_shot.wav");
		pFx->play();
	}
}

void
Application::input(float deltaTime)
{
	//player movement
	posX = pInput->mousePositionX();
	
	//player fire
	bFire = pInput->mouseDown();
	if(bFire)
	{
		while(posY > 0)
		{
			posY -= 0.00001f;
			//Sleep(30);
		}
	}
}



The deltaTIme is the game time.
Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Irrational Movement

Posted 27 August 2010 - 05:12 PM

So, think about what you are doing in your code. You have an update->render->process input loop.

You take in user input, and factor that information into your game. You update the game, and then show the updates for the user to see. When it comes to updating, you don't know how much time has passed the last update. Because various aspects of your game (like animations) are linked to real time, you need to track how much time passes between updates, and update your system accordingly.

How does this relate to animate the moving of a sprite? To animate the movement, you need two things. You need position information. That is, the location of the sprite, and the rate of the movement. Movement occurs over time, so you need a notion of speed, right?

The idea is that you set a target location, and then every time you update the game, you move the sprite some amount. How much do you move? It's governed by the speed of the movement, which you choose, and the time elapsed between updates.

Nothing you do involves the above.

do
{
    posY -= 0.0001; 
}while(posY > 0);



This is just an incredibly inefficient way to assign 0 to posY. There is no notion of updating over time. You're just instantaneously changing the position, although you do it through a while loop...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1