Box2D World QueryAABB

Anything libgdx related goes here!

Box2D World QueryAABB

Postby Dissolution2 » Thu Nov 01, 2018 3:49 pm

Trying to figure out an algoritem on how i can get world.QueryAABB fixture report when player is not in the Area - To say, left it.

Image

Sorry about the code i have been testing with multiple world.Query with and without Math.min(x, x2), Math.min(y, y2), Math.max(x, x2), Math.max(y, y2) and other different ways to test my logic

with the System.out.println statment - i can see player that is bits 2 and 4 getting called but not all the time... sometime only 2 or 4 is called when the player is in the Square.

So i'm trying to check :

1. player not in Area
2. player inn Area and is holding with inn the bounds of the Area
3. if the player have left the Area

Working on Enemy AI Steering and Sensors with rayCasting / world.QueryAABB

Any help would be gratefully :)

Code: Select all
public void getObjectsInRange(float x, float y, float x2, float y2) {
      
      world.QueryAABB(new QueryCallback() {
           @Override
           public boolean reportFixture(Fixture fixture) {
              
              System.out.println("Fixture callback : " + fixture.getFilterData().categoryBits + " bodiesWithinArea : " + bodiesWithinArea.size);
              
              if( fixture.getFilterData().categoryBits == 2 ||
                                         fixture.getFilterData().categoryBits == 4  && !((bodiesWithinArea.size) > 0) ) {
                 
                 System.out.println("Player withInn Erea!!");
                 
                 if(!((bodiesWithinArea.size) > 0) ) {
                    bodiesWithinArea.add(fixture.getBody());
                 }
              }

                       if( fixture.getFilterData().categoryBits != 2 && fixture.getFilterData().categoryBits != 4 ) {
                    System.out.println("We are not in Area!!");
              }
                       return true; //keep going to find all fixtures in the query area
           }
       }, Math.min(x, x2), Math.min(y, y2), Math.max(x, x2), Math.max(y, y2));
   }
Dissolution2
 
Posts: 4
Joined: Thu Nov 01, 2018 3:12 pm

Re: Box2D World QueryAABB

Postby evilentity » Thu Nov 01, 2018 5:06 pm

Use a sensor instead?
Looking for a freelancer? PM me!
evilentity
 
Posts: 4555
Joined: Wed Aug 24, 2011 11:37 am

Re: Box2D World QueryAABB

Postby Dissolution2 » Thu Nov 01, 2018 8:38 pm

evilentity wrote:Use a sensor instead?


Yes i have been thinking about it but i don't know... been stuck trying to figure out a good way to use rayCasting / World query AABB

biges Box of Enemy would be L.O.S threshold and the other box closes would be close attack sensor etc.

want my AI to be nice and clean - to be able to use it on other games down the road etc. and when you have a function like that i would like to see it implemented and working as well as can be and not to sure about cpu efficiency

Image
Dissolution2
 
Posts: 4
Joined: Thu Nov 01, 2018 3:12 pm

Re: Box2D World QueryAABB

Postby Dissolution2 » Wed Nov 14, 2018 4:10 pm

Just thought to update what i use for now, if any others are on the same boat !!

Image

This RayCast is in every enemy Class and will be refactored later as i would guess this will have an effect on cpu load!! ?

And beside the RayCasting i have but up as be for Sensors one far off (which i don't know if i use, should have been L.O.S marker) and the close one would be range of close attack Sensor ON BossEnemy's smaller enemy might have othere Sensor like if you jump over them they would change direction and follow player etc....

Code: Select all
RayCastCallback rayCallback = new RayCastCallback() {
      @Override
      public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
         
//Show contact info
//System.out.println("Contact Info : " + fixture.getBody().getUserData() + " at [" + point.x + ";" + point.y + "]" );
         
          if( fixture.getBody().getType() != BodyType.StaticBody ) {
             dist = 1*fraction; // Closes collision to target !!
             collision.set(point);
             EnemyA.this.normal.set(normal).add(point);
             
System.out.println("Closes fixture : " + dist);
             if( fixture.getFilterData().categoryBits == 2 || fixture.getFilterData().categoryBits == 4 ) {
 System.out.println("I See Player!! :" + "[" + point.x + "]" + " "+ "[" + point.y + "]");
                losOnPlayer = true;
             }else {
                losOnPlayer = false;
             }
          }
          return -1; //-1; // Continue with the rest of the fixtures
      }
   };


Any Input would be great ?
Dissolution2
 
Posts: 4
Joined: Thu Nov 01, 2018 3:12 pm

Re: Box2D World QueryAABB

Postby shatterblast » Wed Nov 14, 2018 4:57 pm

I am just throwing a concept out there. If a certain type of enemy just has a short range attack, like maybe sprite to sprite contact, then it would possibly not need the RayCast. It could just home in on the player character's position if the distance between the two sprites becomes close enough, which while sounding similar to the RayCast idea, would act like employing only a single variety. The process might involve a bit of path-finding AI with obstacles though, when either trying to get around one or make contact with another. Separating checks for both the X and Y coordinates might likely function well with path-finding in that case.
shatterblast
 
Posts: 145
Joined: Sun Jul 06, 2014 1:14 pm

Re: Box2D World QueryAABB

Postby Dissolution2 » Sun Nov 18, 2018 11:21 am

shatterblast wrote:I am just throwing a concept out there. If a certain type of enemy just has a short range attack, like maybe sprite to sprite contact, then it would possibly not need the RayCast. It could just home in on the player character's position if the distance between the two sprites becomes close enough, which while sounding similar to the RayCast idea, would act like employing only a single variety. The process might involve a bit of path-finding AI with obstacles though, when either trying to get around one or make contact with another. Separating checks for both the X and Y coordinates might likely function well with path-finding in that case.



Yeah you can do that, and i have that as a very basic AI

Code: Select all
if(enemy.getClass().getSimpleName().equals("enemyName")) {
                     
                     if(enemy.getX() < player.getX() - rangeAdd / Utility.PPM)  // 250
                     enemy.b2body.setActive(true);
                     
                     // then we set steeringBehavior(target,enemy.b2body ...etc
                  // All so if you want the enemy to be active and spawn after the player pass by change + to -
                     
                     if(enemy.getClass().getSimpleName().equals("EnemyKnightDevilDropFront")) {
                        if(enemy.getX() < bubble.getX() + rangeAdd / Utility.PPM) // 250
                        enemy.b2body.setActive(true);
                     }
Dissolution2
 
Posts: 4
Joined: Thu Nov 01, 2018 3:12 pm


Return to Libgdx

Who is online

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