Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Two Questions. One About Collision of Rectangles , Other about libgdx Logic in  (Read 1171 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-05-20 16:27:06 »

Hi guys.
/edited due to no replies/ Cheesy
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 Cheesy


Thanks guys, sorry for bothering you in any case Cheesy.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
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();
                }



            }
        }


    }
}
Offline 65K
« Reply #1 - Posted 2013-05-20 21:13:01 »

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.

Offline Andre Lopes
« Reply #2 - Posted 2013-05-21 02:46:01 »

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 Cheesy


Btw thankss 65k Cheesy

~~

I will upload the whole file and post the dropbox link, its ok ? Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2013-05-21 02:52:24 »

- 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!

Offline Andre Lopes
« Reply #4 - Posted 2013-05-21 03:12:13 »

Got it. Ty man Cheesy i mean Rebirth, i know you like me to say your full name Tongue

Cheesy

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

https://www.dropbox.com/s/krg0p965m31f8d3/PlaneShooterDebug3.0.zip?v=0rw-
Offline Andre Lopes
« Reply #5 - Posted 2013-05-21 12:15:29 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
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;
    }

   
   
   
}
Offline ReBirth
« Reply #6 - Posted 2013-05-22 02:57:46 »

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.

1  
camera.translate(-gutterWidth, -gutterHeight);

Offline Andre Lopes
« Reply #7 - Posted 2013-05-22 11:18:56 »

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..

Offline Andre Lopes
« Reply #8 - Posted 2013-05-23 13:37:41 »

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 Tongue Cheesy
Offline Phibedy

Senior Member


Medals: 8



« Reply #9 - Posted 2013-05-24 22:41:17 »

if you are using libgdx, why don't you just use box2d too? Just asking, no criticism Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #10 - Posted 2013-05-25 04:26:11 »

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

Offline Phibedy

Senior Member


Medals: 8



« Reply #11 - Posted 2013-05-25 14:14:53 »

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.
Offline Andre Lopes
« Reply #12 - Posted 2013-05-25 16:12:35 »

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

Cheesy
Offline Phibedy

Senior Member


Medals: 8



« Reply #13 - Posted 2013-05-26 12:36:00 »

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.
Offline relminator
« Reply #14 - Posted 2013-05-26 12:40:47 »

Finding the point of contact could be easily done by a simple vector projection.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (13 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (31 views)
2014-08-16 06:45:27

Tekkerue (31 views)
2014-08-16 06:22:17

Tekkerue (19 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!