1

I wanted to know the difference of position from one update to another

Relevant code:

 @Override
        protected void onManagedUpdate(float pSecondsElapsed) {
            super.onManagedUpdate(pSecondsElapsed);                

                    Log.d("bar move", "fuel: "+ Float.toString(walkingFuel)) ;
                    if(counter==5) {    // each time is to fast to percive difference?
                         Log.d("bar move", "previous x : "+ Float.toString(previousPosition.x)) ;
                         Log.d("bar move", "current x : "+ Float.toString(PlayerBody.getPosition().x)) ;

                         Log.d("bar move", "fuel consumed: "+ Float.toString( Math.abs(previousPosition.x - PlayerBody.getPosition().x) * 5));
                         walkingFuel += Math.abs(PlayerBody.getPosition().x - previousPosition.x  ) * 5.0f;
                         previousPosition = PlayerBody.getPosition();
                         Log.d("bar move", "current fuel : "+ Float.toString(walkingFuel)) ;

                         counter=0;
                    }
                    counter++; }

I thought that the diference from each manageupdate was to low to percive difference, thats why the counter. Any how, in each maneageUpdate both previous and current are equal.

Here is a bit of my log to make it simpler

fuel: 0.0

previous x : 6.8613663

current x : 6.8613663

fuel consumed: 0.0

current fuel : 0.0

Later

fuel: 0.0

previous x : 7.0288167

current x : 7.0288167

fuel consumed: 0.0

current fuel : 0.0

So, I did something wrong here? If so, whats the right way of doing it?

1 Answer 1

0
previousPosition = PlayerBody.getPosition();

Probably here you just copy the reference to the object but not the value. You need to copy the value. May be something like this:

previousPosition = new Vector2(PlayerBody.getPosition().x, PlayerBody.getPosition().y);

I don't remember what type you should use for storing position in andEngine, so change the type to the type of "previousPosition".

2
  • As an addition, I would like to add, that the better way is to not create new Vectro2 object in every frame. But create one vertor outside the loop and store it in a private field of an object. Then when you want to copy the value, you use "set(Vector2 v)" method of Vector2 class. In this case it will copy values and will not create new objects. And so your program will run faster. Jul 19, 2015 at 16:39
  • Well it really doesnt contribute much to the speed, its a tiny difference. I ll try it anyway.
    – qera hile
    Jul 19, 2015 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.