AnswerBun.com

testPoint not working - libgdx

Game Development Asked by ngọc quý nguyễn on January 5, 2022

I’m making a game like angry birds with LibGDX and box2d in which I want to drag a ball (instead of the bird) back before releasing it.

I deploy with testPoint function: when I touch down the screen, I check if the point is within the body of the ball or not but testPoint function never works exactly. Here’s what I’m trying thanks so much:

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    for(Fixture fixture:bird.getBody().getFixtureList()) {
        if(fixture.testPoint(screenX/100, (mapHeight-screenY)/100)) { //100 is scaled parameter
             draggedBird = true;
             return true;
         }
    }
    return false;
}

One Answer

The problem I see is you are dividing the screenX and screenY values by 100 here:

if(fixture.testPoint(screenX/100, (mapHeight-screenY)/100)) { //100 is scaled parameter

screenX and screenY are the values indicating the X and Y coordinates of your touch and by sending a modified value to your testPoint method you're not testing with the right values.

I suggest you send the original values to your testPoint method like so:

fixture.testPoint(screenX, mapHeight - screenY)

A couple of tips:

  • If you need a dragging behaviour check the onDragged method instead of the touchDown one.
  • If you are using an OrthographicCamera for your game (which is recommended) you don't have to worry about wrong screen coordinates since you unproject the values to the correct ones and you don't even have to worry about the Y axis being reversed, here's an example:
OrthographicCamera camera = new OrthographicCamera(gameWidth, gameHeight); // gameWidth and gameHeight can be any float you want

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector3 unprojected = camera.unproject(new Vector3(screenX, screenY, 0f));
float x = unprojected.x;
float y = unprojected.y;
    for(Fixture fixture:bird.getBody().getFixtureList()) {
        if(fixture.testPoint(x, y) {
             draggedBird = true;
             return true;
         }
    }
    return false;
}

Answered by Luis Fernando Frontanilla on January 5, 2022

Add your own answers!

Related Questions

How to move a character from 1 to 3 squares on a grid?

0  Asked on January 23, 2021 by shunkashuu

     

Unity 2017.3.of3 Raycast2D randomly misses objects

1  Asked on January 15, 2021 by hesitatetowonder

     

How to fix the vibration while colliding?

1  Asked on January 13, 2021

   

Render order in 3/4 view game

0  Asked on January 10, 2021 by chuan-li

 

How can I control ads frequency?

1  Asked on January 10, 2021 by ruslan-plastun

   

Attach movement to each list object

1  Asked on January 9, 2021 by mavish

     

How to make a map surface object

1  Asked on January 8, 2021 by anonymous-entity

     

Coding Spelunky Edge Roll Mechanic

0  Asked on January 5, 2021 by austin-weaver

     

Creating a lookup table of datamaps in Harlowe/Twine

0  Asked on January 4, 2021 by the_e

   

Unity 4 – Some Rigidbodies Won’t Fall Asleep

5  Asked on January 3, 2021 by ryan-berserkguard-nrby

       

Unity PackageCache keeps having errors

0  Asked on December 31, 2020 by christopher-perry

 

Phaser Tiled map importing

1  Asked on December 30, 2020 by kabuto178

     

Change the colour of all particles on screen

1  Asked on December 27, 2020 by super-potato

   

How can you make a jump to a point using the Character Controller?

0  Asked on December 25, 2020 by aimon-z

   

Trouble destroying an SDL2 texture from another class

1  Asked on December 21, 2020 by gamer1

 

How to hide objects behind an invisible plane?

1  Asked on December 18, 2020 by hatinacat2000

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir