Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (773)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Box2D] Callback when body/fixture clicked  (Read 901 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Posted 2014-06-07 13:37:19 »

What is the best way to find out if a body or fixture was clicked?

I have this QueryAABB thing setup to simply print "here" when I click on a body, however it seems to be well...very inaccurate.

I can click practically 500 pixels to the right of my body and it says I have clicked on it, anyone got an idea if I am doing this wrong?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   @Override
   public boolean touchDown(int screenX, int screenY, int pointer, int button) {
      Vector3 touchPos = new Vector3(screenX, screenY, 0);
      universe.game.b2dCam.unproject(touchPos);
      universe.bFactory.getWorld().QueryAABB(new QueryCallback() {
         
         @Override
         public boolean reportFixture(Fixture fixture) {
            System.out.println("here");
            return false;
         }
      }, touchPos.x - 1, touchPos.y - 1, touchPos.x + 1, touchPos.y + 1);
      return true;
   }

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Herjan
« Reply #1 - Posted 2014-06-07 14:09:41 »

Give your body an intersect function (ofcourse don't forget to translate its w=width and h=height to the desired lengths);
Create a class for example which holds your body and give it this function.

1  
2  
3  
4  
5  
6  
7  
public boolean intersects(float x, float y){
   Vector2 pos = body.getPosition();
   if( (pos.x - w/2 < x && pos.x + w/2 > x) && (pos.y - h/2 < y && pos.y + h/2 > y) ){
      return true;
   }
   return false;
}


And its as easy as ...
1  
if(Car.intersects(mouse.x, mouse.y){}
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #2 - Posted 2014-06-07 15:51:49 »

Nice and simple, this will suffice until ofc there are too many entities on screen. Thanks.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1850 views)
2018-06-10 19:43:48

EgonOlsen (1879 views)
2018-06-10 19:43:44

EgonOlsen (1245 views)
2018-06-10 19:43:20

DesertCoockie (1680 views)
2018-05-13 18:23:11

nelsongames (1345 views)
2018-04-24 18:15:36

nelsongames (1963 views)
2018-04-24 18:14:32

ivj94 (2736 views)
2018-03-24 14:47:39

ivj94 (1938 views)
2018-03-24 14:46:31

ivj94 (3029 views)
2018-03-24 14:43:53

Solater (1078 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!