简体   繁体   中英

LibGDX Box2d getWorldPoint issue

I've just started developing my new android game using LibGDX and Box2d. I've got a ship class with at the moment contains reference to a body of the ship and array of engines attached to the ship. For debugging purpose i wanted to draw positions of the engines and their forces vectors' peaks (as a red squares) first i tried this code

public void draw(Batch batch, float parentAlpha)
{
    for(AttachedEngine e : Engines)
    {
            Vector2 globalPosition = body.getWorldPoint(e.localPosition);
            Vector2 globalPower = body.getWorldPoint(e.localForce);
            batch.draw(img, Gdx.graphics.getWidth()/2 + globalPosition.x, Gdx.graphics.getHeight()/2 + globalPosition.y, 5, 5);
            batch.draw(img, Gdx.graphics.getWidth()/2 + globalPower.x, Gdx.graphics.getHeight()/2 + globalPower.y, 5, 5);
    }
}

but it causes my squares being in wrong places (doesn't work) then i tried this code

public void draw(Batch batch, float parentAlpha)
{
    for(AttachedEngine e : Engines)
    {
            batch.draw(img, Gdx.graphics.getWidth()/2 + body.getWorldPoint(e.localPosition).x, Gdx.graphics.getHeight()/2 + body.getWorldPoint(e.localPosition).y, 5, 5);
            batch.draw(img, Gdx.graphics.getWidth()/2 + body.getWorldPoint(e.localForce).x, Gdx.graphics.getHeight()/2 + body.getWorldPoint(e.localForce).y, 5, 5);
    }
}

and it worked. Can you guys explain me why are this codes varying? The first solution is more natural for me but i don't know why it is not working. Answer is propably trivial, but i am too confused. This is the first time since a few years of programming that I actually really don't know what's going on, and I can't find solution on stackoverflow so i have to ask you guys

EDIT

It works in this way:

public void draw(Batch batch, float parentAlpha)
{
    for(AttachedEngine e : Engines)
    {
            Vector2 globalPosition = body.getWorldPoint(e.localPosition);
            batch.draw(img, Gdx.graphics.getWidth()/2 + globalPosition.x, Gdx.graphics.getHeight()/2 + globalPosition.y, 5, 5);
            Vector2 globalPower = body.getWorldPoint(e.localForce);
            batch.draw(img, Gdx.graphics.getWidth()/2 + globalPower.x, Gdx.graphics.getHeight()/2 + globalPower.y, 5, 5);
    }
}

also I found out that when I initialize globalPower just after globalPostion (like in the first portion of code) like this

            Vector2 globalPosition = body.getWorldPoint(e.localPosition);
            Vector2 globalPower = body.getWorldPoint(e.localForce);

both of the vect2 contains global power coordinates (they are the same) so I must insert draw between them.

I checked, and the only difference between the two code snippets is that globalPosition and globalPower are pulled out beforehand in the first snippet.

As I see it, that leaves you with only a few possibilities.

  1. By the time draw is called, the values in e.localPosition and/or e.localForce have changed. The only way I can see this as being a possibility is if the libraries you are using are multi-threaded. I'm not familiar with box2d or libgdx so I can't speak to this point.

  2. You have made changes to other parts of your application that you have not copy/pasted here that cause the malfunction.

Really, as far as I can see, those are the only options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM