Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Not rendering position and rotation accurately  (Read 4145 times)

0 Members and 1 Guest are viewing this topic.

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Not rendering position and rotation accurately
« on: January 06, 2014, 07:11:06 pm »
I am using Box2D and SFML to make a game.
Everything works great and perfectly except for this.
The problem is that when an object is spinning quickly in Box2D, the sprites start to not render in the correct rotation/position.

I know the problem is probably with Box2D, but I posted there   (http://www.box2d.org/forum/viewtopic.php?f=3&t=9614)   and I haven't gotten any replies, so I figured I would post here, and hopefully get some input on what I could try and do to fix it, if there is anything I can do.

Spinning slowly, 5 rpm maybe:


Spinning at about 40 rpm:


Here is the code that draws the sprite. Drawing happens after the Box2D timeStep().
Each Fixture of the Body has its own sprite, called m_sprite.

    m_sprite.setPosition(scale*(m_pFixture->GetAABB(0).GetCenter().x), scale*(m_pFixture->GetAABB(0).GetCenter().y));//scale is 64
    m_sprite.setRotation(180.0*m_pBody->GetAngle()/PI);//PI is defined as 3.1415926
    m_rWindow.draw(m_sprite);

lucas92

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Not rendering position and rotation accurately
« Reply #1 on: January 06, 2014, 07:30:13 pm »
You should pause the physics when you draw the sprites. From your picture, it looks like you call the step function between each draw of sprites.

Otherwise, you should consider creating your own custom sf::Drawable, sf::Transformable and draw Quads or Triangles instead of individual sprites. It will be much faster this way.

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Not rendering position and rotation accurately
« Reply #2 on: January 06, 2014, 09:00:29 pm »
Well I do updateAllPhysics, then, drawAllSprites and I loop over that. And each cube does need to be its own image.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Not rendering position and rotation accurately
« Reply #3 on: January 06, 2014, 09:07:35 pm »
Might it be that Box2D doesn't give such smooth results when the rotation speed is higher? Check if the results of your physics simulation are as expected.

No, not each cube needs to be its own image. Have a look at the vertex array tutorial.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Not rendering position and rotation accurately
« Reply #4 on: January 06, 2014, 09:16:43 pm »
Some doc i found: http://www.learn-cocos2d.com/api-ref/1.0/Box2D/html/classb2_fixture.html

"const b2AABB & b2Fixture::GetAABB   (   int32    childIndex   )    const [inline]
Get the fixture's AABB. This AABB may be enlarge and/or stale. If you need a more accurate AABB, compute it using the shape and the body transform."
SFML.Utils - useful extensions for SFML.Net

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Not rendering position and rotation accurately
« Reply #5 on: January 07, 2014, 12:30:07 am »
Thank you all very much, I would imagine this info is what I needed. Haven't tried the stuff out yet though.

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Not rendering position and rotation accurately
« Reply #6 on: January 07, 2014, 02:35:43 am »
If I should post this in a new thread let me know, but it's related to this.

If you look at the first images I posted, the main object is comprised of many sprites (9x19).
Nexus told me to look at Vertex Arrays, which i'm trying to learn to use now: http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php

In the examples, it shows a custom made "TileMap" class that does some of what I need, except for a few things, which I don't really understand.

1. My object will potentially contain 30-40 different images, each of which could have 2-3 states for each sub-block of the object seen in the picture (I just colored it with a gradient to show the different cubes). I can't just have a sprite sheet with 40*3=120, 64x64 images on it.

2. Each block of the object will want to control the state of its image. Before I had each Block object hold its own sprite. Now, they are all grouped together in a tilemap. Should each block object hold a quad pointer now?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Not rendering position and rotation accurately
« Reply #7 on: January 07, 2014, 08:46:27 pm »
Vertex arrays are primarily an optimization. If you are happy with the performance of sprites (and the group that's potentially going to play your game is too), you don't need to use them. Still, I'll answer the questions in case you once need them:

1. You don't need a single image that contains all the sprites. However, it's advised to not have one image per sprite, as this may introduce texture switches which can be expensive when used in masses. But again, it's an optimization consideration, don't make your life harder than necessary.

2. The easiest way is to rebuild the vertex array when something changes. I don't know what you mean with "quad pointer"... The texture rectangle in the image file is specified by 4 vertices and their attributes sf::Vertex::texCoords.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Not rendering position and rotation accurately
« Reply #8 on: January 07, 2014, 11:06:46 pm »
Using a the TileMap class actually solves the original problem of this post, as well as being faster, so I will definitely use them if possible
And when I said Quad pointer, I meant 4 vertex pointers (for each vertex of a quad), so the object can change the textureCoordinates to change what is being displayed (animation).

This is what I have as an example to loop over pieces of an animation, all part of a .png (4 states):
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::X))
        {
            textureTileX++;

            if(textureTileX == 4)
                textureTileX = 0;

            quad[0].texCoords = sf::Vector2f(textureTileX * tileSize.x, textureTileY * tileSize.y);
            quad[1].texCoords = sf::Vector2f((textureTileX + 1) * tileSize.x, textureTileY * tileSize.y);
            quad[2].texCoords = sf::Vector2f((textureTileX + 1) * tileSize.x, (textureTileY + 1) * tileSize.y);
            quad[3].texCoords = sf::Vector2f(textureTileX * tileSize.x, (textureTileY + 1) * tileSize.y);
        }

Sorry if I didn't understand, but how do I get data from multiple files, for example:

The file "Gun_Type1.png" would ideally only hold the sprite sheet for that specific entity, while "Armor_Type2.png" would hold its own sprite sheet for a different entity. TileMap only lets me load one texture. How do I get around this?

Would I need multiple sf::VertexArrays and a corresponding sf::Textures in the TileMap, so that each call to         
states.texture = loop over textures;
target.draw(loop over vertices, states);
would result in the correct texture being used, for the correct vertices? That doesn't seem like a good solution but it's the only one I have come up with.
« Last Edit: January 07, 2014, 11:12:48 pm by Strikerklm96 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Not rendering position and rotation accurately
« Reply #9 on: January 07, 2014, 11:10:42 pm »
You need a vertex array for each texture (file), that's why it won't pay off if you don't gather multiple images in one texture.

Yes, you may need something like the quad pointers, although a single index to the first one should be enough. If you recreate the whole vertex array instead of adapting only the relevant parts, you don't need such indirections.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Not rendering position and rotation accurately
« Reply #10 on: January 07, 2014, 11:24:50 pm »
While I was editing my post you replied, sorry about that.
Anyway, wouldn't it be really messy to put 100+ sprites in a single png file?
Or, is there some way I can load the textures into SFML, and combine them there?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Not rendering position and rotation accurately
« Reply #11 on: January 07, 2014, 11:29:51 pm »
You don't necessarily need 100+, you also have to keep in mind that textures cannot have arbitrary size. And there are tools that pack sprites efficiently together.

But as stated, think about these things when performance is a concern, not before.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything