Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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  
  com.badlogic.gdx.utils.Array OR java ArrayList ? Performance  (Read 888 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-12-13 18:07:19 »

Hey guys!!
I have a question, which should i use in libgdx projects?
 arrows = new com.badlogic.gdx.utils.Array<Arrow>();
or ArrayList from java?

Im having performance issues.. And i think it started after i changed that but idk if i should change everything again...
Online trollwarrior1
« Reply #1 - Posted 2013-12-13 18:16:36 »

Could you explain some more about the performance issues?

I don't know what does the libgdx array does, but if you just want to use it like ArrayList, just use ArrayList.
Offline davedes
« Reply #2 - Posted 2013-12-13 18:21:01 »

Gdx Array exists to replace ArrayList. Same with their other collection utils. They are marginally faster and tend to use less allocations. It's a non-issue on desktop since the performance differences are negligible. On mobile the performance differences are still pretty minor, relatively speaking. In either case it's probably not the cause of your slowdown.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #3 - Posted 2013-12-13 20:58:48 »

Could you explain some more about the performance issues?

I don't know what does the libgdx array does, but if you just want to use it like ArrayList, just use ArrayList.

Alright! I will create a new topic explaining everything Tongue
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #4 - Posted 2013-12-13 22:40:48 »

No don't make a new topic just for that. Just ask here.

Offline HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #5 - Posted 2013-12-13 22:56:58 »

Use Gaplist

Also, to find the source of you performance issues, use JVisualVM.

Offline Andre Lopes
« Reply #6 - Posted 2013-12-13 23:31:01 »

One of the problems that i face is when i shoot arrows, things go way slower.
In desktop its ok, but in my android... jesus... It goes slow.

Video of Profile Analyzer of Netbeans
http://www.youtube.com/watch?v=H8Cbrj2uCIE

And here are the Arrow Classes...

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  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
package br.weapons;

import br.enemies_aux.SmokeEffect;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class Arrow {

    private static int xVertice = -1;

    public int type_Arrow;
    public int posX, posY, shotPosX;
    public int damage;
    public double shotAngle, movingAngle;
    public Sprite sprite;
    public int manaWaste = 0;

    public int shotToQuadrant = -1;

    public double frameTime;

    public Vector3 touch;
    public Vector2 vector;

    public boolean isInsideWindow;

    public boolean hit = false;
    public boolean toRemoveThis = false;
    public SmokeEffect smokeEffect;

    private TiledMapTileLayer.Cell Cell_Temp;

    public Arrow(double posX, double posY, double angle, int quadrant) {
        shotPosX = (int) posX;

        this.posX = (int) posX;
        this.posY = (int) posY;
        this.shotAngle = angle;

        frameTime = 0;
        isInsideWindow = true;

        smokeEffect = new SmokeEffect();
        damage = 0;
        shotToQuadrant = quadrant;
        touch = new Vector3();
        vector = new Vector2();
        //System.out.println("Shot To Quadrant : " + shotToQuadrant);
    }

    public void render(SpriteBatch sb) {
        if (hit) {
            smokeEffect.render(sb, posX, posY);
            if (smokeEffect.isFinished()) {
                toRemoveThis = true;
            }
        } else {
            if (toRemoveThis == false) {
                //System.out.println("Sprite Rotation" + (float) toDegrees(shotAngle));

                sprite.setPosition(posX, posY);
                sprite.draw(sb);

            }
        }
    }

    public void update(TiledMapTileLayer collisionLayer, Camera camera) {
        if (hit == false) {

            lookNearbyCells(collisionLayer);

            posX = (int) ArrowBallistic.getSX(posX, posY, ArrowBallistic.vo, ArrowBallistic.gravity, shotAngle, frameTime);
            posY = (int) ArrowBallistic.getSY(posY, ArrowBallistic.vo, ArrowBallistic.gravity, shotAngle, frameTime);

            //Will Fix this soon.
            //movingAngle = ArrowBallistic.getMovingAngle(shotAngle, shotPosX, posX, shotToQuadrant);

            sprite.setRotation((float) Math.toDegrees(shotAngle));

            frameTime += 0.1;    //0.001

            touch.set(posX, posY, 0);
            camera.project(touch);
            vector.set(touch.x, touch.y);

            isInsideWindow = (vector.x > (-Gdx.graphics.getWidth()) && vector.x < (Gdx.graphics.getWidth() * 2)) && (vector.y > (-Gdx.graphics.getHeight()));

            if (!isInsideWindow) {

                toRemoveThis = true;

            }

        }
    }

    public boolean isIsInsideWindow() {
        return isInsideWindow;
    }

    public void lookNearbyCells(TiledMapTileLayer collisionLayer) {

        boolean blocked = false;
        boolean slope = false;

        for (int i = 0; i < 3; i++) {
            Cell_Temp = collisionLayer.getCell((int) (((posX + ((sprite.getWidth() / 2) * i)) / 16)), (int) (((posY + ((sprite.getHeight() / 2) * i)) / 16)));

            if (Cell_Temp != null) {
                blocked = Cell_Temp.getTile().getProperties().containsKey(ArrowInfo.BLOCKED);
                slope = Cell_Temp.getTile().getProperties().containsKey(ArrowInfo.SLOPED);
                if (blocked || slope) {
                    toRemoveThis = true;
                }

            }

            for (int j = 0; j < 3; j++) {
                Cell_Temp = collisionLayer.getCell((int) (((posX + ((sprite.getWidth() / 2) * i)) / 16)), (int) (((posY + ((sprite.getHeight() / 2) * j)) / 16)));
                if (Cell_Temp != null) {
                    blocked = Cell_Temp.getTile().getProperties().containsKey(ArrowInfo.BLOCKED);
                    slope = Cell_Temp.getTile().getProperties().containsKey(ArrowInfo.SLOPED);

                    if (blocked || slope) {
                        toRemoveThis = true;
                    }
                }
            }
        }
    }

    public boolean isToRemoveThis() {
        return toRemoveThis;
    }

    public void setArrowHitSomething() {
        hit = true;
    }

    public Rectangle getFrameRectangle() {
        Rectangle frameRectangle = new Rectangle();

        frameRectangle.height = sprite.getRegionHeight();
        frameRectangle.width = sprite.getRegionWidth();

        frameRectangle.x = posX;
        frameRectangle.y = posY;

        return frameRectangle;
    }

    public void dispose() {
        smokeEffect.dispose();

    }

}


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  
package br.weapons.arrows;

import br.dir.DirectoriesWeapons;
import br.weapons.Arrow;
import br.weapons.ArrowInfo;
import com.badlogic.gdx.math.Rectangle;
import static java.lang.Math.toDegrees;

public class LightArrow extends Arrow {

    public LightArrow(double posX, double posY, double angle, int quadrant) {
        super(posX, posY, angle, quadrant);
        damage = 20;
        type_Arrow = ArrowInfo.light_Arrow;

        DirectoriesWeapons directoriesWeapons = new DirectoriesWeapons();
        sprite = directoriesWeapons.getLightArrow();

        sprite.setRotation((float) toDegrees(shotAngle));
        directoriesWeapons = null;
    }

    @Override
    public Rectangle getFrameRectangle() {
        Rectangle frameRectangle = new Rectangle();

        frameRectangle.width = 40;
        frameRectangle.height = 6;

        frameRectangle.x = posX;
        frameRectangle.y = posY;

        return frameRectangle;
    }

    @Override
    public void dispose() {
        try {

            sprite.getTexture().dispose();
            smokeEffect.setFinished();
            smokeEffect.dispose();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}



World Update

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  
 public void updateAndRenderArrows(SpriteBatch spriteBatch, float delta) {

        camera = current_Level.camera;
        for (int i = 0; i < arrows.size; i++) {
            Arrow arrow_TEMP = arrows.get(i);

            for (int k = 0; k < enemies.size; k++) {
                if (!arrow_TEMP.hit) {
                    Rectangle arrowRect = arrow_TEMP.getFrameRectangle();
                    Rectangle enemyRect = enemies.get(k).getFrameRectangle();

                    if (arrowRect.overlaps(enemyRect)) {
                        enemies.get(k).beingHit(arrow_TEMP.damage);
                        arrow_TEMP.setArrowHitSomething();
                    }
                }
            }

            if (!arrow_TEMP.isToRemoveThis()) {
                arrow_TEMP.update(collisionLayer, camera);
                arrow_TEMP.render(spriteBatch);

            } else {

                arrows.removeIndex(i);
                arrow_TEMP.dispose();
                arrow_TEMP = null;

            }

        }
    }





Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #7 - Posted 2013-12-13 23:37:45 »

Do you update with the delta time?

Offline Andre Lopes
« Reply #8 - Posted 2013-12-13 23:51:31 »

Do you update with the delta time?

Not really.Should i ? Im really confused, its my first game,so im sorry in advance.
Any suggestions , i will try them!
Should i use delta? How should i insert it? I feel the ArrowBallistic is not perfect plus, the music plays without lag, so im thinking its just the arrows not moving fast enough....


Heres the ArrowBallistic...

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  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
package br.weapons;

/**
 *
 */

public class ArrowBallistic {

    private static int normal = 1; //TEST
    public static double vo = 20 / normal;
    public static double gravity = 2 / normal;

    public static int FIRST_QUADRANT = 1;
    public static int SECOND_QUADRANT = 2;
    public static int THIRD_QUADRANT = 3;
    public static int FOURTH_QUADRANT = 4;

    private static double atan = 0;
    private static double dx, dy;

    public static double getAngle(double posXplayer, double xClick, double posYPlayer, double yClick) {

        if (xClick < posXplayer && yClick < posYPlayer) {
            atan = Math.PI + (Math.atan((posYPlayer - yClick) / (posXplayer - xClick)));
        } else if (xClick >= posXplayer && yClick >= posYPlayer) {

            atan = Math.atan((posYPlayer - yClick) / (posXplayer - xClick));
        } else if (xClick < posXplayer && yClick >= posYPlayer) {

            atan = Math.PI - (Math.atan((yClick - posYPlayer) / (posXplayer - xClick)));

        } else if (xClick > posXplayer && yClick < posYPlayer) {
            atan = (2 * Math.PI) - (Math.atan((yClick - posYPlayer) / (posXplayer - xClick)));
        } else {
            atan = 0;
        }

        return atan;
    }

    public static double getSX(double xo, double yo, double vo, double gravity, double angle, double time) {
        dx = (xo) + vo * Math.cos(angle) * time;

        return dx;
    }

    public static double getSY(double yo, double vo, double gravity, double angle, double time) {

        dy = yo + vo * (Math.sin(angle) * time) - ((gravity / 2) * (time * time));

        return dy;
    }

    public static double getMovingAngle(double angle, double xo, double sx, int shotToQuadrant) {

        double tanO = 0;

        if (shotToQuadrant == FIRST_QUADRANT) {
            tanO = Math.atan(Math.tan(angle) - (gravity / ((vo * vo) * (Math.cos(angle) * Math.cos(angle)))) * (sx - xo));
        } else if (shotToQuadrant == SECOND_QUADRANT) {
            tanO = Math.atan(Math.PI / 2 + (Math.tan(angle) - (gravity / ((vo * vo) * (Math.cos(angle) * Math.cos(angle)))) * (sx - xo)));
        } else if (shotToQuadrant == THIRD_QUADRANT) {
            tanO = Math.atan(Math.PI + (Math.tan(angle) - (gravity / ((vo * vo) * (Math.cos(angle) * Math.cos(angle)))) * (sx - xo)));
        } else if (shotToQuadrant == FOURTH_QUADRANT) {
            tanO = Math.atan(Math.PI * 2 + (Math.tan(angle) - (gravity / ((vo * vo) * (Math.cos(angle) * Math.cos(angle)))) * (sx - xo)));
        }

        //Change quadrant Calculations
        if (shotToQuadrant == FIRST_QUADRANT && sx <= getXVertice(angle, gravity, vo, xo, sx)) {
            shotToQuadrant = FOURTH_QUADRANT;
        } else if (shotToQuadrant == SECOND_QUADRANT && sx <= getXVertice(angle, gravity, vo, xo, sx)) {
            shotToQuadrant = THIRD_QUADRANT;
        }

        return tanO;
       
       
       
    }

    public static double getXVertice(double angle, double gravity, double velocity, double xo, double dx) {
        double xv = 0;
        double b = 0;
        double a2 = 0;

        //-b
        b = -((xo * gravity) / ((velocity * velocity) * (Math.cos(angle) * Math.cos(angle))));

        //2a
        a2 = 2 * (-gravity / ((2 * (velocity * velocity)) * Math.cos(angle) * Math.cos(angle)));

        xv = b / a2;

        return xv;
    }

}
Offline 65K
« Reply #9 - Posted 2013-12-14 09:18:22 »

Like already mentioned, the kind of collection class is not the problem.

It its probably not the cause of bad performance, but you create way too many rectangles for no reason.

Method lookNearbyCells() is unclear and probably not efficient. Why inspect all surrounding cells each and every time ? Method name does not imply what it does.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #10 - Posted 2013-12-14 13:09:15 »

Like already mentioned, the kind of collection class is not the problem.

It its probably not the cause of bad performance, but you create way too many rectangles for no reason.

Method lookNearbyCells() is unclear and probably not efficient. Why inspect all surrounding cells each and every time ? Method name does not imply what it does.

Well because i must see if the arrow is hitting a wall Smiley

How should i do it then? Plus the rectangles... How should i make them?
Offline Andre Lopes
« Reply #11 - Posted 2013-12-14 18:22:31 »

The fps stays in 40~~

Heres how im loading the .tmx

1  
2  
3  
4  
TmxMapLoader.Parameters params = new TmxMapLoader.Parameters();
        params.textureMagFilter = Texture.TextureFilter.Nearest;
        params.textureMinFilter = Texture.TextureFilter.Nearest;
        map = new TmxMapLoader().load("imagens/Levels/One/LevelOne.tmx", params);


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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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