Two Questions. One About Collision of Rectangles , Other about libgdx Logic in

Hi guys.
/edited due to no replies/ :smiley:
First Question :
I got that class below.
I need to know The (x;y) intersection of them ( the two Rectangles that im checking the collision), so when they do intersect , i can gen an explosion.How can i find the x;y intersection?

Second Question:
How do you guys do when you have a 2D game with several levels, how do you trade the GameView? i mean, the AppListener?
I mean, how do you manage several levels in your game?

Plus, if you could JUDGE my code, telling all the bad things of it so i can go research/learn how to improve it,i would be hAppY :smiley:

Thanks guys, sorry for bothering you in any case :D.

package Collisions;

import Atlas.Explosion;
import Logic.GameUpdate;
import Models.EnemyPlane;
import Models.Missile;
import Models.Plane;
import java.awt.Rectangle;
import java.util.ArrayList;

/**
 *
 * @author André Vinícius Lopes
 */
public class MainCollision {

    private GameUpdate gameupdate;
//
    private ArrayList<EnemyPlane> enemies;
    private ArrayList<Missile> myMissiles;
    private Plane plane;
    private Rectangle ENEMIEPLANE_RECTANGLE;
    private Rectangle MYMISSILE_RECTANGLE;

    public MainCollision(GameUpdate g) {
        gameupdate = g;
        System.gc();
    }

    public void collision() {

        enemies = gameupdate.getEnemies();
        myMissiles = gameupdate.getMyMissiles();
        plane = gameupdate.getShooterGame().getPlane();

        for (int m = 0; m < myMissiles.size(); m++) {
            Missile getMissileM = myMissiles.get(m);
            
            //Detecta Colisao Entre Missel e inimigo.
            MYMISSILE_RECTANGLE = getMissileM.getMissileRepresentativeRectangle();
        
            for (int e = 0; e < enemies.size(); e++) {
                EnemyPlane getEnemieE = enemies.get(e);

                ENEMIEPLANE_RECTANGLE = getEnemieE.getEnemyPlaneRepresentativeRectangle();
            
                if (ENEMIEPLANE_RECTANGLE.intersects(MYMISSILE_RECTANGLE)) 
                {
                    int x;
                    int y;
                    gameupdate.removeMissile(m);
                    gameupdate.removeEnemyPlane(e);
                    //Create Explosion Based on (x;y) Intersection
                    //gameupdate.addNewExplosion(y, y);
                    //Adiciona novo inimigo casa haja colisao.//
                    gameupdate.addNewEnemy();
                }



            }
        }


    }
}

Give meaningful names to classes, methods and variables.
MainCollision suggest a class that represents a collision, while it actually detects collision.
A method collision() does not even suggest anything at all.
What does a GameUpdate do ?
Drop that garbage collector call.
No upper case letters for ordinary variables.
Remove unused code like the plane variable.
Abstraction. If you do not need any special feature of ArrayList, use List instead. The same is true for the methods returning lists of receiving lists as parameters.
Limit responsibilities. The job of a collision detector should be detecting collisions, but not taking actions on it. Just notify someone else about it.

Okey.Will do that 65.000.

But yeah, i still have those two issues to fix.
And, now i have another one.

Whenever i tell spribeBatch to draw the explosion at x,y , it draws totally NOT in the correct location.
For example, when i tell to draw at 0,0 , it draws actually in the middle of the freaking screen hehe.

Anyway, im planning to upload the whole .jar and images ready, with my netbeans code.I know its abusive, but i dont have anyone to ask.
I will post and help others as well, mostly topics are way harder for me to answer but, i can try.

But anyway, if anyone can give me a hand would be AWESOME :smiley:

Btw thankss 65k :smiley:

~~

I will upload the whole file and post the dropbox link, its ok ? :smiley:

  • Don’t give a variable all caps name unless it’s final and has assignment next to it (constant)
  • LibGDX has its own Rectangle class which good for checking collision
  • Stop calling gc(), act like it nexver exists - resource consuming things on libgdx environment can’t be recovered by gc because they’re disposable!

Got it. Ty man :smiley: i mean Rebirth, i know you like me to say your full name :stuck_out_tongue:

:smiley:

Heres the link of the source code and the .jar and the
images that im using, which ARENT MINE, the images i got from google just for testing.

the whole code showing whats happening with the explosion

idk whats up with this
 if i tell it to draw at 0,0 ; It draws in the middle of the screen.

package Atlas;


import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

/**
 *
 * @author André Vinícius Lopes
 */
public class ExplosionModel {

    private final int FRAME_COLS = 8;
    private final int FRAME_ROWS = 6;
    private Animation explosionAnimation;
    private TextureRegion[] explosionsFrames;
    private TextureRegion currentFrame;
    private float stateTime;
    private SpriteManager spriteManager;
    private Texture explosionSheet;

    public ExplosionModel(SpriteManager sm) {
        spriteManager = sm;
        explosionSheet = spriteManager.getExplosion();
        TextureRegion[][] tmp = TextureRegion.split(explosionSheet, explosionSheet.getWidth() / FRAME_COLS, explosionSheet.getHeight() / FRAME_ROWS);

        explosionsFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];

        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                explosionsFrames[index++] = tmp[i][j];
            }
        }
        explosionAnimation = new Animation(0.025f, explosionsFrames);
        stateTime = 0f;

    }

    public int getFRAME_COLS() {
        return FRAME_COLS;
    }

    public int getFRAME_ROWS() {
        return FRAME_ROWS;
    }

    public Animation getExplosionAnimation() {
        return explosionAnimation;
    }

    public TextureRegion[] getExplosionsFrames() {
        return explosionsFrames;
    }

    public TextureRegion getCurrentFrame() {
        return currentFrame;
    }

    public float getStateTime() {
        return stateTime;
    }

    public SpriteManager getSpriteManager() {
        return spriteManager;
    }

    public Texture getExplosionSheet() {
        return explosionSheet;
    }

    
    
    
}

If you copied the working code, yes it’s normal to have 0,0 in the middle of screen. Mostly tutorial I have seen use that.

If you prefer traditional 0,0 left bottom, calculate both gutter’s width and height then translate your camera by their opposite direction.


camera.translate(-gutterWidth, -gutterHeight);

Ah. But if i tell it to draw where the missile collided with the enemy plane, it doesnt work either. It draws way more higher and to the right, than where i say it


Alright, im sending this project to some friends so we can try to figure it out whats happening.

As soon as i get a solution, i will post here so if anyone with same problem than me, can have peace :stuck_out_tongue: :smiley:

if you are using libgdx, why don’t you just use box2d too? Just asking, no criticism :slight_smile:

If all he needs is rectangle collision, box2d is clearly overkill.

If you have a look at his code he also have planes etc. As I assume he needs the rectangle-collision for placing the explosion-animation, so rectangles wouldn’t be that accurate (Of course if his planes are flying rectangles).
I haven’t said that he should use box2d for rectangle-collision, it’s not made for “only collision” as it fails if you just set the position of a body.
It was just a hint, because simulated stuff by box2d is quite efficient, especially because it’s not jogl, but a jni implementation.

Andre Lopes:
You could add a dispose() method, because you hold references to a texture->textureatlas as you don’t have much ram on android (for preventing memory-leaks), but that’s just me.

Ty, do u have a good tutorial or some tips to use box2D because i got lost when i tried it!

:smiley:

iforce is great: http://www.iforce2d.net/b2dtut/introduction
libgdx: http://code.google.com/p/libgdx-users/wiki/box2d

I prefered iforce, it’s not writting in java but the methods are quite the same.

Finding the point of contact could be easily done by a simple vector projection.