How to change `Vector2` 's values in the virtual `render(delta:Float)` method of the Screen interface of com.badlogic.Gdx

22 Views Asked by At

Currently, when the Vector2's instance value is modified either using *= operator in that method, for the first iteration of when the function is being called, it correctly change the value to the value that I want. In other words, it becomes negative. because I do *=-1f.

But, the next frame (or the next iteration of the function being called), it becomes positive again.

If I tried to set the value using = operator, the value stays constant. For example, I want to accelerate my object to the left of the screen. Because the thing is, in libgdx, the center of the screen position value is 0.0f for x coordinate and 0.0f for y coordinate. let say, I want to move to the left, but, the max velocity stays around -10.0f, which is incorrect. It should becomes more and more negative, so that the object still accelerates.

If it is implemented using the *= operator, one frame it becomes negative but another frame it becomes positive. I preferably, wanted to do it using this implementation, but I do not understand the render method. Why does it set the Vector2 value back to its original value, when I have change it in the render(delta:Float) method.

I think my current code implementation is sufficient to move the object to the left with integration of acceleration vector and velocity vector with the time taken to draw the next frame.

Here is the code:

class FirstScreen : Screen {
    val camera: OrthographicCamera by lazy { OrthographicCamera(1400f, 700f) }
    val pen: ShapeRenderer by lazy { ShapeRenderer() }
    var pos:Vector2= Vector2(0f,0f)
    var acc:Vector2= Vector2(5f,5f)
    var vel:Vector2= Vector2(10f,10f)

    override fun show() {
        // Prepare your screen here.
    }

    override fun render(delta: Float) {
        ScreenUtils.clear(0.168627f, 0.356863f, 0.1334f, 1.0f)
        camera.update()
        pen.projectionMatrix = camera.combined
        pen.begin(ShapeRenderer.ShapeType.Filled)
        pen.color = Color(1.0f, 0.125f, 0.125f, 1.0f)
        pen.circle(pos.x,pos.y, 25f, 32)
        pen.end()

        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            //using '=' operator 
            acc.x=-5f
            acc.y=-5f
            vel.x=-10f
            vel.y=-10f
   
            
            //using '*=' operator
            /*acc.x*=-1f
            vel.x*=-1f*/
            
            //this code should be sufficient to move the object to the left even when x=0 
            //or at midpoint of Screen width

            vel.x+=acc.x*delta
            pos.x+=vel.x*delta
            println("Acc X: ${acc.x}")
            println("Vel X: ${vel.x}")
            println("Pos X: ${pos.x}")
        }
    }

What I have done:

tried to google the question of "How to change Vector2 value in render method of libgdx". But no one seems to have the same question and so no solution yet.

Expectation: Hopefully, someone that has more experience can give a hint or two on how to debug this problem.

1

There are 1 best solutions below

0
Smelly Fish On
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
        acc.x=-5f

        vel.x+=acc.x*delta
        if(vel.x>0.0f) {
            vel.x *= -1f
        }
        pos.x+=vel.x*delta
        
        println("Acc X: ${acc.x}")
        println("Vel X: ${vel.x}")
        println("Pos X: ${pos.x}")
    }

After try and error, finally able to make it works again. Because, previously, I did not check whether the velocity vector is a negative value, because it might become negative.

And secondly, the velocity vector cannnot be assigned to certain value because that will make the velocity to have constant value.

But only alter these values after integration of acceleration with respect to time. (for velocity vector manipulation).