Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Objects are moving?

This thread is locked; no one can reply to it. rss feed Print
Objects are moving?
kebapmanager
Member #14,863
January 2013
avatar

{"name":"w4qPHBo.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/0\/80ee606fab7ddd54d0124a0c999efc52.png","w":696,"h":432,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/0\/80ee606fab7ddd54d0124a0c999efc52"}w4qPHBo.png

So as you can see on the picture ,blocks do not align in different frames of players jumping , basically I draw lines betwen same objects in different timing and you can see pixel or two of difference their height , anyone got any idea why could this happen?

Additional info: I am storing all my objects in vector ( maybe its not fast enough?)

I am using this for camera movement:

#SelectExpand
1CameraUpdate(cameraPosition, player->GetX(), player->GetY(), 16, 16); 2 3al_identity_transform(&camera); 4al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]); 5al_use_transform(&camera); 6 7void CameraUpdate(float *cameraPosition, float x, float y, int width, int height) 8{ 9 if(lastx > x+3) 10 distancec =lastx - x; 11 if(lastx < x+3) 12 distancec =lastx - x; 13 14 lastx = x; 15 16 cameraPosition[0] = -(WIDTH / 2) + (x + width / 2); 17 cameraPosition[1] = -(HEIGHT / 2) + (y + height / 2); 18 19 if(cameraPosition[0] < 0) 20 cameraPosition[0] = 0; 21 if(cameraPosition[1] < 0) 22 cameraPosition[1] = 0; 23 24}

taron 
Member #10,584
January 2009
avatar

Try rounding the camera position down when transforming.

l_translate_transform(&camera, (int)-cameraPosition[0], (int)-cameraPosition[1]);

kebapmanager
Member #14,863
January 2013
avatar

Sadly that did not help :/

#SelectExpand
1map<string, ALLEGRO_BITMAP*> ResourceManager::textures; 2 3void ResourceManager::initialize() 4{ 5 loadAllTextures(); 6} 7 8void ResourceManager::loadAllTextures() 9{ 10 loadTexture("Background.png"); 11 loadTexture("Blocks.png"); 12 loadTexture("Starseed Pilgrim.png"); 13} 14 15void ResourceManager::cleanUp() 16{ 17 map<string, ALLEGRO_BITMAP*>::iterator tex_it; 18 for (tex_it = textures.begin(); tex_it != textures.end(); ++tex_it) 19 { 20 al_destroy_bitmap(tex_it->second); 21 tex_it->second = NULL; 22 } 23 textures.clear(); 24} 25 26bool ResourceManager::loadTexture(string filename) 27{ 28 if (textures.find(filename) != textures.end()) 29 { 30 //cout<< filename << " has already been loaded!" <<endl; 31 return false; 32 } 33 ALLEGRO_BITMAP* t = al_load_bitmap(filename.c_str()); 34 al_convert_mask_to_alpha(t, al_map_rgb(255, 255, 255)); 35 36 textures.insert(pair<string, ALLEGRO_BITMAP*>(filename,t)); 37 return true; 38} 39 40void ResourceManager::unloadTexture(string filename) 41{ 42 map<string, ALLEGRO_BITMAP*>::iterator tex_it = textures.find(filename); 43 if (tex_it != textures.end()) 44 { 45 al_destroy_bitmap(tex_it->second); 46 textures.erase(tex_it); 47 } 48} 49 50ALLEGRO_BITMAP& ResourceManager::getTexture(string filename) 51{ 52 return *(textures.find(filename)->second); 53}

However it stopped when I disabled my latest feature in my game. Which is this up here, ResourceManager, it seems that when I load textures directly , block desync( or whatever you would call it) is not happening anymore , anyone got idea why?

Ben Delacob
Member #6,141
August 2005
avatar

I'm suspicious about that lastx variable. Is it global?

It might be necessary to see where you are loading textures and your drawing code.

__________________________________
Allegro html mockup code thread -website-
"two to the fighting eighth power"

kebapmanager
Member #14,863
January 2013
avatar

I am very sorry about lastx ,you can ignore that it was fore testing purposes only.
I forgot to mention that I was doing some testing yesterday , I drawed punch of 1 pixel width lines on my screen and watched what was going on , basicly they are flickering , going up and down by 1 pixel while my player is in jumping state.
If you are thinking of render code , here it is:

#SelectExpand
1if(render && al_is_event_queue_empty(event_queue)) 2 { 3 render = false; 4 5 //BEGIN PROJECT RENDER================ 6 for(iter = objects.begin(); iter != objects.end(); ++iter) 7 (*iter)->Render(); 8 9 //FLIP BUFFERS======================== 10 al_flip_display(); 11 al_clear_to_color(al_map_rgb(70, 200, 255)); 12 }

Here is an example of how I am creating new objects in main loop:

#SelectExpand
1player->Init(&ResourceManager::getTexture("Starseed Pilgrim.png")); 2Cube *cube = new Cube(b, c, &ResourceManager::getTexture("Blocks.png"));

and this is how update works in specific object when its called from main( 1st code I posted on this post)

#SelectExpand
1void Cube::Render() 2{ 3 GameObject::Render(); 4 5 al_draw_scaled_bitmap(image, frameCount, 0, frameWidth, frameHeight, x, y, 24, 24, 0); 6}

Edgar Reynaldo
Member #8,592
May 2007
avatar

taron  said:

Try rounding the camera position down when transforming.

l_translate_transform(&camera, (int)-cameraPosition[0], (int)-cameraPosition[1]);

You should use floor, not (int), otherwise you are rounding towards zero, not down.

You can also try manually adjusting for the camera position by subtracting it from the real position. That might let you know whether it is the transform at fault.

ALLEGRO_BITMAP& ResourceManager::getTexture(string filename) {
return *(textures.find(filename)->second);
}

Just a question, why are you using an ALLEGRO_BITMAP& when allegro only uses ALLEGRO_BITMAP*? It would save all this silly addressing and dereferencing. Also, that code will crash if you look for a texture not in the map because you would be dereferencing an invalid iterator.

Do the cubes flicker in position at all when you are moving? I can only see three or four that are off by one pixel of height. I'm not sure what is wrong.

kebapmanager
Member #14,863
January 2013
avatar

Just a question, why are you using an ALLEGRO_BITMAP& when allegro only uses ALLEGRO_BITMAP*? It would save all this silly addressing and dereferencing. Also, that code will crash if you look for a texture not in the map because you would be dereferencing an invalid iterator.

I am not really expert on pointers and references , but that was the only way I could make this kind of system work. If you are interested in taking a lookin on whole code and giving me some tips and trick, I would love that.

Do the cubes flicker in position at all when you are moving? I can only see three or four that are off by one pixel of height. I'm not sure what is wrong.

When I for example jump they start flickering , not all of the cubes ,but sometimes even all of them. Like randomly going up and down for 1-2 pixels as my players jump progresses

Edgar Reynaldo
Member #8,592
May 2007
avatar

kebapmanager
Member #14,863
January 2013
avatar

Edgar, I tryed that floor thing u said, it made my player flicker like that.So I am assuming that problem is definitly in that function,the problem is I dont understand why.

Feel free to post a 7z or zip of your src. I might take a gander at it.

There it is , I uploaded the whole project.

Some quick info so you can understand it quicker:
-All objects are based on gameobject class.
-ResourceManager deals with getting and loading the textures.
-S-key = snow
-Space-key = Big "island"
-Down-key = smaller configurable boxes
-You cant spawn more then 150 cubes and not have lagg, probably because of "objects" vector I am itterating trough is not capable of holding soo much data, if anyone has suggestion of how to solve this problem let me know.

You can also see what coordinates are being drawn on as you can use al_transform_coordinates to see the result of the selected transformation.

I know about it, but I don't see a way that would help or how would I keep track of 50 or more coordinates.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Track the object under the mouse and print out its coordinates. Print out the camera coordinates too.

I got it to build, but no cubes show up, and the whole screen is blue with lines across it. The guy eventually stops on some level which spans the whole way across. Any time I press a key it crashes with this message :

terminate called after throwing an instance of 'std::runtime_error'
  what():  random_device::random_device(const std::string&)

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

c:\downloads\forum_code\kebapManager_Game\Game\Game>

kebapmanager
Member #14,863
January 2013
avatar

terminate called after throwing an instance of 'std::runtime_error'
what(): random_device::random_device(const std::string&)

U have visual studio 2013?
I can see its something about random I used.

Maybe you can try and remove snow.cpp and snow.h and remove calling function from main on key s and comment out in cube.cpp lines 15, 16, 17, and make 21st line look like this : frameCount = 24;

This will remove random_device, maybe you are not runing over visual studio so you dont have: #include <random>

The guy eventually stops on some level which spans the whole way across. Any time I press a key it crashes with this message :

Yes I made him stop falling at some point for easier testing purpose.
You can comment that part out in Player::Update() from line 42 -57, and use R key to reset players position

Edgar Reynaldo
Member #8,592
May 2007
avatar

It's probably this code, here. This is from Snow.cpp. Can you explain it? I'm not familiar with random_device, or uniform_int_distribution. I use MinGW 4.5.0 at the moment, from CodeBlocks.

Quote:

  random_device rd;                            // better source of seeds than time()
  mt19937 gen(rd());                            // seed Mersenne Twister from rd
  uniform_int_distribution<> distX(x - WIDTH,x + WIDTH);                  // range of numbers
  uniform_int_distribution<> distY(1, 4);
  uniform_int_distribution<> distVelY(3, 5);
  uniform_int_distribution<> distVelX(1, 7);
  uniform_int_distribution<> distDirX(0, 7);

  velX = distVelX(gen);
  velY = distVelY(gen);

kebapmanager
Member #14,863
January 2013
avatar

It's probably this code, here. This is from Snow.cpp. Can you explain it? I'm not familiar with random_device, or uniform_int_distribution. I use MinGW 4.5.0 at the moment, from CodeBlocks.

That is basically a better way of getting a random number then with time because, if I would use time in this example, all of my lets say 25 snowflakes would generate at the same place with same speed etc etc because the program makes them too fast.

std::uniform_int_distribution<> Generates random integer values of closed interval, as far as I know this was added in c++11

You can just comment that out, or remove snow because its kinda irrelevant at this point for this bug.
As I said in previouse post you have that same random generator in cube.cpp.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Well I removed the use of random_device and it didn't show any artifacts or mis alignment horizontally. So it works fine for me.

And with random number generators, you generally seed them once at the beginning and then let them create random numbers to their hearts content.

taron 
Member #10,584
January 2009
avatar

random_device is supposed to be true random number generator if supported, but it's not required to actual be one.

kebapmanager
Member #14,863
January 2013
avatar

I finnaly found solution to my problem.
Only objects that are created while my player is in jump are getting flickered.
Reason: probably because they are created on float value, and updated on int.
not shure to be honest but I dont even care. :D

Thanks alot guys :)

Go to: