How to Calculate Correct Impulse Vector[SOLVED]

Anything libgdx related goes here!

How to Calculate Correct Impulse Vector[SOLVED]

Postby lastpeony » Sun Sep 01, 2019 11:19 am

I am trying to calculate correct impulse vector with respect to touch point and center of body.

Impulse should be applied to center of body.

I guess the problem is simple calculate 3rd point on same line given 2 points.

Image

(distance between C and B should be equal to B and A)

So with the new calculated point we can find impulse vector and apply(?)

What i actually want to do is: First we touch on our object,then while still touching to the screen we move our finger on screen so that we can aim(meanwhile 3rd point is calculated) and when we release our finger impulse should be applied.

This is what i wrote so far but body moves to wrong direction:
Code: Select all
class MyGdxGame() : ApplicationAdapter() {

    val PPM = 32f
    val SCALE = 2f
    val TIMESTEP = 60f
    private  var positionX = 0f
    private  var positionY = 0f
    private var impulseVectorX =0f
    private var impulseVectorY = 0f
    private lateinit var world : World
    private lateinit var player:Player
    private lateinit var playerTexture: Texture
    private lateinit var b2dr: Box2DDebugRenderer

    lateinit var batch: SpriteBatch
    lateinit var camera:OrthographicCamera

    var playerIsTouched:Boolean = false


    override fun create() {
        var w = Gdx.graphics.width
        var h = Gdx.graphics.height
        camera = OrthographicCamera()
        camera.setToOrtho(false, w/SCALE, h/SCALE);

        world = World(Vector2(0f,0f),false)

        Gdx.app.log("camerax", (w/2f).toString());
        Gdx.app.log("cameray", (h/2f).toString());

        batch = SpriteBatch()
        b2dr = Box2DDebugRenderer()
        playerTexture= Texture("player.png")

        player = Player(world,150f,175f,32f)


        Gdx.input.inputProcessor = object : InputAdapter() {

            override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
              //converting touch coordinates to our actual coordinates.
                var touchPointScreen:Vector3 = Vector3(screenX.toFloat(),screenY.toFloat(),0f)
                camera.unproject(touchPointScreen)
                var touchPointBox2D:Vector2  = Vector2(touchPointScreen.x / PPM,touchPointScreen.y / PPM)


                if (player.body.fixtureList.first().testPoint(touchPointBox2D) && playerIsTouched == false){
                    Gdx.app.log("touched once", "player touched");
                     playerIsTouched = true
                    impulseVectorX = 0f;
                    impulseVectorY = 0f;
                    return true
                }

            return false
            }

            override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {

                if(playerIsTouched){
                    var impulseVector:Vector2 = Vector2(impulseVectorX,impulseVectorY)
                    Gdx.app.log("impulsevector",impulseVector.toString())
                    player.body.applyLinearImpulse(impulseVector,player.body.worldCenter, true)
                    playerIsTouched = false


                }






                return super.touchUp(screenX, screenY, pointer, button)
            }

            override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
                var touchPointScreen:Vector3 = Vector3(screenX.toFloat(),screenY.toFloat(),0f)
                camera.unproject(touchPointScreen)


                var playerCenterX = player.body.position.x
                var playerCenterY = player.body.position.y
                var touchPointX = touchPointScreen.x / PPM
                var touchPointY = touchPointScreen.y / PPM
                var distanceBetweenTouchAndPlayer = sqrt((playerCenterY-touchPointY).pow(2) + (playerCenterX-touchPointX).pow(2))


                if(playerIsTouched && distanceBetweenTouchAndPlayer <= 4.5){
                  //  Gdx.app.log("touch is dragging", "draggign touch");

                    var touchPointToPlayerCenterVector: Vector2 = Vector2(playerCenterX-touchPointX,playerCenterY-touchPointY)
                    var newPointHolder:Vector2=Vector2(playerCenterX+touchPointToPlayerCenterVector.x,playerCenterY+touchPointToPlayerCenterVector.y)




                    impulseVectorX = newPointHolder.x
                    impulseVectorY = newPointHolder.y

                    Gdx.app.log("screen touch point",touchPointX.toString()+" "+touchPointY.toString())
                    Gdx.app.log("center of mass",playerCenterX.toString() +" "+ playerCenterY.toString())
                    Gdx.app.log("new point",impulseVectorX.toString() +", "+impulseVectorY.toString())


                    return true

                }

                return super.touchDragged(screenX, screenY, pointer)
            }

        }
    }

    override fun render() {
        world.step(TIMESTEP, 6, 2);
        camera.update()
        batch.projectionMatrix.set(camera.combined)

        Gdx.gl.glClearColor(1f, 0f, 0f, 1f)
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
        batch.begin()
        batch.draw(playerTexture,player.body.position.x*PPM - (64f/2),player.body.position.y*PPM-(64f/2),64f,64f);

        batch.end()
        b2dr.render(world,camera.combined.scl(PPM))

    }

    override fun dispose() {

        world.dispose()
        b2dr.dispose()
        batch.dispose()

    }




}

Please check fun touchDragged() i guess something going wrong there ?
Last edited by lastpeony on Wed Sep 04, 2019 7:22 pm, edited 1 time in total.
lastpeony
 
Posts: 5
Joined: Fri Aug 30, 2019 8:07 pm

Re: How to Calculate Correct Impulse Vector with respect to

Postby shatterblast » Mon Sep 02, 2019 5:28 pm

More detail would be helpful, like... What does "wrong direction" mean?

Also, the link to your picture is broken.

You might try using float or int instead of var or val.
shatterblast
 
Posts: 414
Joined: Sun Jul 06, 2014 1:14 pm

Re: How to Calculate Correct Impulse Vector[SOLVED]

Postby lastpeony » Wed Sep 04, 2019 7:22 pm

i solved my problem guys . just more focus and more work did it some of the calculations were wrong.
lastpeony
 
Posts: 5
Joined: Fri Aug 30, 2019 8:07 pm


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot] and 1 guest