1
\$\begingroup\$

I'm developing a game in Android using LibGDX (although this is more of a JAVA problem than an engine problem). My aim is to have my player bounce off one spring onto another that will eventually take him over an obstacle like so:

enter image description here

I have a feeling the lag is because im creating new objects in a method that gets called in update(). This is because I had the same issue with coins in the game until I stopped initialising the Coinobject in a method that gets called by update() - now they work smoothly.

However, I'm not sure how to implement the spring collision without initialising Spring objects. Currently, objects are initialised through a text file - however, Spring is only initialised in a Box object rather than the text file. In my update() method I loop through the objects and if an object is a Box which contains a Spring, the following method is called:

public void playerSpringCollision(Box containsSpring) {
        // first spring (on the box)
        Spring boxSpring = containsSpring.getSpring();
        // second spring (on the platform)
        Spring platformSpring = containsSpring.getPartnerPlatform().getSpring();
        // distance in x between first & second spring
        float dx = platformSpring.getxPos()
                + platformSpring.getSprite().getWidth() - boxSpring.getxPos()
                - player.getSprite().getWidth();
        // distance in y between first & second spring
        float dy = platformSpring.getyPos() - boxSpring.getyPos();
        directionToSpring.x = dx;
        directionToSpring.y = dy;
        // if player collides with first spring
        if (player.getRectangle().overlaps(boxSpring.getRectangle())) {
            player.springImpulse(directionToSpring); // send player to platform spring
        }
        // if player collides with second spring
        if (player.getRectangle().overlaps(platformSpring.getRectangle())) {
            directionToObstacle.x = -directionToSpring.x;
            directionToObstacle.y = directionToSpring.y;
            player.springImpulse(directionToObstacle); // send player over obstacle
        }
}

I'm not sure how to do this without initialising Spring and Vector2 objects in the method - any ideas? Highly grateful for any help, thanks.

\$\endgroup\$
1
  • \$\begingroup\$ "I have a feeling". So it drops fps when you jump around? What else happens in update loop? So box has springs already instantiated? getSpring just returns reference? There is some heavy one liners, but nothing too bad. I'm going to be brave and claim, that this is not the problem. Either you are calling this way too often ( like 100 times per box with 1000 boxes) and/or there is something wrong with the update method. \$\endgroup\$ – Katu May 29 '15 at 11:56
1
\$\begingroup\$

For the instanciation inside update method problem i suggest you to use the Pool class for every object in your game that must be instantiated a lot of times. You can follow this guide in particular (in the guide the object is a bullet). The guide also contains suggestions for other memory and lag problems.

As for the way you calculate and execute the jump, i suggest to place the Spring objects on the game level when it is created and not while updating. You can also pre-calculate the direction the Spring will give to the player based on the Spring position.

Then, in the update method you only check if player collides with a spring (you will have to use a "for" loop for checking all Spring objects available) and change direction based on the jump direction stored in a variable inside the Spring.

\$\endgroup\$
0
\$\begingroup\$

I don't see any new Spring calls in the code you posted, if the springs are being instanciated in the getSpring method then you need to ask yourself if you always have to return a new instance.

With Vector2 for example, it comes with a set method, so you can have a temporary (or many) instance in your class that you work with when doing vector math, so you don't do;

private void foo() {
  Vector2 result = new Vector2(vectorA);
  result.add(vectorB);
}

You do;

private final Vector2 result = new Vector2();

private void foo() {
  result.set(vectorA);
  result.add(vectorB);
}

That will take the cost of allocating and instanciating the result only once, and not every time the foo method is called (obviously, if you're doing any parallel stuff, you need to make sure you access the temporaries in a safe way).

Find the places where you create new instances of objects, be it Vector2 or Spring, and think about whether or not you can re-use an existing instance.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.